]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
formatting
[lyx.git] / src / BufferView.h
1 // -*- C++ -*-
2 /**
3  * \file BufferView.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Alfredo Braustein
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  * \author Jürgen Vigna
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef BUFFER_VIEW_H
16 #define BUFFER_VIEW_H
17
18 #include "coordcache.h"
19 #include "cursor.h"
20 #include "metricsinfo.h"
21 #include "TextMetrics.h"
22 #include "UpdateFlags.h"
23
24 #include "support/types.h"
25
26 #include <boost/utility.hpp>
27 #include <boost/signal.hpp>
28
29 #include <utility>
30 #include <string>
31
32
33 namespace lyx {
34
35 namespace support { class FileName; }
36
37 class Buffer;
38 class Change;
39 class DocIterator;
40 class FuncRequest;
41 class FuncStatus;
42 class Intl;
43 class LCursor;
44 class LyXText;
45 class ParIterator;
46 class ParagraphMetrics;
47 class ViewMetricsInfo;
48
49 /// Scrollbar Parameters.
50 struct ScrollbarParameters
51 {
52         void reset(int h = 0, int p = 0, int l = 0)
53         {
54                 height = h;
55                 position = p;
56                 lineScrollHeight = l;
57         }
58
59         /// Total document height in pixels.
60         int height;
61         /// Current position in the document in pixels.
62         int position;
63         /// Line-scroll amount in pixels.
64         int lineScrollHeight;
65 };
66
67 /// Screen view of a Buffer.
68 /**
69  * A BufferView encapsulates a view onto a particular
70  * buffer, and allows access to operate upon it. A view
71  * is a sliding window of the entire document rendering.
72  * It is the official interface between the LyX core and
73  * the frontend WorkArea.
74  * 
75  * \sa WorkArea
76  * \sa Buffer
77  * \sa CoordCache
78  */
79 class BufferView : boost::noncopyable {
80 public:
81         BufferView();
82
83         ~BufferView();
84
85         /// set the buffer we are viewing.
86         /// \todo FIXME: eventually, we will create a new BufferView
87         /// when switching Buffers, so this method should go.
88         void setBuffer(Buffer * b);
89         /// return the buffer being viewed.
90         Buffer * buffer() const;
91
92         /// resize the BufferView.
93         void resize();
94
95         /// redisplay the referenced buffer.
96         void reload();
97         /// load a buffer into the view.
98         bool loadLyXFile(support::FileName const & name, bool tolastfiles = true);
99
100         /// perform pending metrics updates.
101         /** \c Update::FitCursor means first to do a FitCursor, and to
102          * force an update if screen position changes.
103          * \c Update::Force means to force an update in any case.
104          * \retval true if a screen redraw is needed
105          */
106         bool update(Update::flags flags = Update::FitCursor | Update::Force);
107
108         /// move the screen to fit the cursor.
109         /// Only to be called with good y coordinates (after a bv::metrics)
110         bool fitCursor();
111         /// reset the scrollbar to reflect current view position.
112         void updateScrollbar();
113         /// return the Scrollbar Parameters.
114         ScrollbarParameters const & scrollbarParameters() const;
115
116         /// Save the current position as bookmark.
117         /// if persistent=false, save to temp_bookmark
118         void saveBookmark(bool persistent);
119         /// goto a specified position.
120         void moveToPosition(
121                 int par_id, ///< Paragraph ID, \sa Paragraph
122                 pos_type par_pos ///< Position in the \c Paragraph
123                 );
124         /// return the current change at the cursor.
125         Change const getCurrentChange() const;
126
127         /// return the lyxtext we are using.
128         LyXText * getLyXText();
129
130         /// return the lyxtext we are using.
131         LyXText const * getLyXText() const;
132
133         /// move cursor to the named label.
134         void gotoLabel(docstring const & label);
135
136         /// set the cursor based on the given TeX source row.
137         void setCursorFromRow(int row);
138
139         /// center the document view around the cursor.
140         void center();
141         /// scroll document by the given number of lines of default height.
142         void scroll(int lines);
143         /// Scroll the view by a number of pixels.
144         void scrollDocView(int pixels);
145         /// Set the cursor position based on the scrollbar one.
146         void setCursorFromScrollbar();
147
148         /// return the pixel width of the document view.
149         int workWidth() const;
150         /// return the pixel height of the document view.
151         int workHeight() const;
152
153         /// switch between primary and secondary keymaps for RTL entry.
154         void switchKeyMap();
155
156         /// return true for events that will handle.
157         FuncStatus getStatus(FuncRequest const & cmd);
158         /// execute the given function.
159         bool dispatch(FuncRequest const & argument);
160
161         /// request an X11 selection.
162         /// \return the selected string.
163         docstring const requestSelection();
164         /// clear the X11 selection.
165         void clearSelection();
166
167         /// resize method helper for \c WorkArea
168         /// \sa WorkArea
169         /// \sa resise
170         void workAreaResize(int width, int height);
171
172         /// dispatch method helper for \c WorkArea
173         /// \sa WorkArea
174         /// \retval true if a redraw is needed
175         bool workAreaDispatch(FuncRequest const & ev);
176
177         /// access to anchor.
178         pit_type anchor_ref() const;
179
180         /// access to full cursor.
181         LCursor & cursor();
182         /// access to full cursor.
183         LCursor const & cursor() const;
184         /// sets cursor.
185         /// This will also open all relevant collapsable insets.
186         void setCursor(DocIterator const &);
187         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
188         /// \retval true if an update was needed.
189         bool checkDepm(LCursor & cur, LCursor & old);
190         /// sets cursor.
191         /// This is used when handling LFUN_MOUSE_PRESS.
192         bool mouseSetCursor(LCursor & cur);
193
194         /// sets the selection.
195         /* When \c backwards == false, set anchor
196          * to \c cur and cursor to \c cur + \c length. When \c
197          * backwards == true, set anchor to \c cur and cursor to \c
198          * cur + \c length.
199          */
200         void putSelectionAt(DocIterator const & cur,
201                 int length, bool backwards);
202
203         /// return the internal \c ViewMetricsInfo.
204         /// This is used specifically by the \c Workrea.
205         /// \sa WorkArea
206         /// \sa ViewMetricsInfo
207         ViewMetricsInfo const & viewMetricsInfo();
208         /// update the internal \c ViewMetricsInfo.
209         /// \param singlepar indicates wether
210         void updateMetrics(bool singlepar = false);
211
212         ///
213         TextMetrics const & textMetrics(LyXText const * t) const;
214         TextMetrics & textMetrics(LyXText const * t);
215         ///
216         ParagraphMetrics const & parMetrics(LyXText const *, pit_type) const;
217
218         ///
219         CoordCache & coordCache() {
220                 return coord_cache_;
221         }
222         ///
223         CoordCache const & coordCache() const {
224                 return coord_cache_;
225         }
226         /// get this view's keyboard map handler.
227         Intl & getIntl() { return *intl_.get(); }
228         ///
229         Intl const & getIntl() const { return *intl_.get(); }
230
231         /// This signal is emitted when some message shows up.
232         boost::signal<void(docstring)> message;
233
234         /// This signal is emitted when some dialog needs to be shown.
235         boost::signal<void(std::string name)> showDialog;
236
237         /// This signal is emitted when some dialog needs to be shown with
238         /// some data.
239         boost::signal<void(std::string name,
240                 std::string data)> showDialogWithData;
241
242         /// This signal is emitted when some inset dialogs needs to be shown.
243         boost::signal<void(std::string name, std::string data,
244                 InsetBase * inset)> showInsetDialog;
245
246         /// This signal is emitted when some dialogs needs to be updated.
247         boost::signal<void(std::string name,
248                 std::string data)> updateDialog;
249
250         /// This signal is emitted when the layout at the cursor is changed.
251         boost::signal<void(std::string layout)> layoutChanged;
252
253 private:
254         ///
255         bool multiParSel();
256         ///
257         int width_;
258         ///
259         int height_;
260         ///
261         ScrollbarParameters scrollbarParameters_;
262
263         ///
264         ViewMetricsInfo metrics_info_;
265         ///
266         CoordCache coord_cache_;
267         ///
268         Buffer * buffer_;
269
270         /// Estimated average par height for scrollbar.
271         int wh_;
272         ///
273         void menuInsertLyXFile(std::string const & filen);
274
275         /// this is used to handle XSelection events in the right manner.
276         struct {
277                 CursorSlice cursor;
278                 CursorSlice anchor;
279                 bool set;
280         } xsel_cache_;
281         ///
282         LCursor cursor_;
283         ///
284         bool multiparsel_cache_;
285         ///
286         pit_type anchor_ref_;
287         ///
288         int offset_ref_;
289
290         /// keyboard mapping object.
291         boost::scoped_ptr<Intl> const intl_;
292
293         /// last visited inset (kept to send setMouseHover(false) )
294         InsetBase * last_inset_;
295
296         /// A map from a LyXText to the associated text metrics
297         typedef std::map<LyXText const *, TextMetrics> TextMetricsCache;
298         mutable TextMetricsCache text_metrics_;
299 };
300
301
302 } // namespace lyx
303
304 #endif // BUFFERVIEW_H