]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
the delegate patch
[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 "update_flags.h"
23
24 #include "support/types.h"
25
26 #include <boost/noncopyable.hpp>
27
28 #include <utility>
29 #include <string>
30
31
32 namespace lyx {
33
34 namespace support { class FileName; }
35
36 namespace frontend { class Painter; }
37 namespace frontend { class GuiBufferViewDelegate; }
38
39 class Buffer;
40 class Change;
41 class DocIterator;
42 class FuncRequest;
43 class FuncStatus;
44 class Intl;
45 class Cursor;
46 class Text;
47 class ParIterator;
48 class ParagraphMetrics;
49 class ViewMetricsInfo;
50  
51 enum CursorStatus {
52         CUR_INSIDE,
53         CUR_ABOVE,
54         CUR_BELOW
55 };
56
57 /// Scrollbar Parameters.
58 struct ScrollbarParameters
59 {
60         void reset(int h = 0, int p = 0, int l = 0)
61         {
62                 height = h;
63                 position = p;
64                 lineScrollHeight = l;
65         }
66
67         /// Total document height in pixels.
68         int height;
69         /// Current position in the document in pixels.
70         int position;
71         /// Line-scroll amount in pixels.
72         int lineScrollHeight;
73 };
74
75 /// Screen view of a Buffer.
76 /**
77  * A BufferView encapsulates a view onto a particular
78  * buffer, and allows access to operate upon it. A view
79  * is a sliding window of the entire document rendering.
80  * It is the official interface between the LyX core and
81  * the frontend WorkArea.
82  *
83  * \sa WorkArea
84  * \sa Buffer
85  * \sa CoordCache
86  */
87 class BufferView : boost::noncopyable {
88 public:
89         ///
90         BufferView(Buffer & buffer);
91         ///
92         ~BufferView();
93
94         /// return the buffer being viewed.
95         Buffer & buffer();
96         Buffer const & buffer() const;
97
98         /// perform pending metrics updates.
99         /** \c Update::FitCursor means first to do a FitCursor, and to
100          * force an update if screen position changes.
101          * \c Update::Force means to force an update in any case.
102          * \retval true if a screen redraw is needed
103          */
104         bool update(Update::flags flags = Update::FitCursor | Update::Force);
105
106         /// move the screen to fit the cursor.
107         /// Only to be called with good y coordinates (after a bv::metrics)
108         bool fitCursor();
109         /// reset the scrollbar to reflect current view position.
110         void updateScrollbar();
111         /// return the Scrollbar Parameters.
112         ScrollbarParameters const & scrollbarParameters() const;
113
114         /// Save the current position as bookmark.
115         /// if idx == 0, save to temp_bookmark
116         void saveBookmark(unsigned int idx);
117         /// goto a specified position, try top_id first, and then bottom_pit.
118         /// \return true if success
119         bool moveToPosition(
120                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
121                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
122                 int top_id, ///< Paragraph ID, \sa Paragraph
123                 pos_type top_pos ///< Position in the \c Paragraph
124                 );
125         /// return the current change at the cursor.
126         Change const getCurrentChange() const;
127
128         /// move cursor to the named label.
129         void gotoLabel(docstring const & label);
130
131         /// set the cursor based on the given TeX source row.
132         void setCursorFromRow(int row);
133
134         /// center the document view around the cursor.
135         void center();
136         /// scroll down document by the given number of pixels.
137         void scrollDown(int pixels);
138         /// scroll up document by the given number of pixels.
139         void scrollUp(int pixels);
140         /// scroll document by the given number of pixels.
141         void scroll(int pixels);
142         /// Scroll the view by a number of pixels.
143         void scrollDocView(int pixels);
144         /// Set the cursor position based on the scrollbar one.
145         void setCursorFromScrollbar();
146
147         /// return the pixel width of the document view.
148         int workWidth() const;
149         /// return the pixel height of the document view.
150         int workHeight() const;
151
152         /// translate and insert a character, using the correct keymap.
153         void translateAndInsert(char_type c, Text * t, Cursor & cur);
154
155         /// return true for events that will handle.
156         FuncStatus getStatus(FuncRequest const & cmd);
157         /// execute the given function.
158         /// \return the Update::flags for further metrics update.
159         Update::flags 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 the BufferView.
168         /// \sa WorkArea
169         void resize(int width, int height);
170
171         /// dispatch method helper for \c WorkArea
172         /// \sa WorkArea
173         /// \retval true if a redraw is needed
174         bool workAreaDispatch(FuncRequest const & ev);
175
176         /// access to anchor.
177         pit_type anchor_ref() const;
178
179         ///
180         CursorStatus cursorStatus(DocIterator const & dit) const;
181         /// access to full cursor.
182         Cursor & cursor();
183         /// access to full cursor.
184         Cursor const & cursor() const;
185         /// sets cursor.
186         /// This will also open all relevant collapsable insets.
187         void setCursor(DocIterator const &);
188         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
189         /// \retval true if an update was needed.
190         bool checkDepm(Cursor & cur, Cursor & old);
191         /// sets cursor.
192         /// This is used when handling LFUN_MOUSE_PRESS.
193         bool mouseSetCursor(Cursor & cur);
194
195         /// sets the selection.
196         /* When \c backwards == false, set anchor
197          * to \c cur and cursor to \c cur + \c length. When \c
198          * backwards == true, set anchor to \c cur and cursor to \c
199          * cur + \c length.
200          */
201         void putSelectionAt(DocIterator const & cur,
202                 int length, bool backwards);
203
204         /// return the internal \c ViewMetricsInfo.
205         /// This is used specifically by the \c Workrea.
206         /// \sa WorkArea
207         /// \sa ViewMetricsInfo
208         ViewMetricsInfo const & viewMetricsInfo();
209         /// update the internal \c ViewMetricsInfo.
210         /// \param singlepar indicates wether
211         void updateMetrics(bool singlepar = false);
212
213         ///
214         TextMetrics const & textMetrics(Text const * t) const;
215         TextMetrics & textMetrics(Text const * t);
216         ///
217         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
218
219         ///
220         CoordCache & coordCache() { return coord_cache_; }
221         ///
222         CoordCache const & coordCache() const { return coord_cache_; }
223
224         ///
225         Point getPos(DocIterator const & dit, bool boundary) const;
226
227
228         ///
229         void draw(frontend::Painter & pain);
230
231         /// get this view's keyboard map handler.
232         Intl & getIntl() { return *intl_.get(); }
233         ///
234         Intl const & getIntl() const { return *intl_.get(); }
235
236         //
237         // Messages to the GUI
238         //
239         /// This signal is emitted when some message shows up.
240         void message(docstring const & msg);
241
242         /// This signal is emitted when some dialog needs to be shown.
243         void showDialog(std::string const & name);
244
245         /// This signal is emitted when some dialog needs to be shown with
246         /// some data.
247         void showDialogWithData(std::string const & name, std::string const & data);
248
249         /// This signal is emitted when some inset dialogs needs to be shown.
250         void showInsetDialog(std::string const & name, std::string const & data,
251                 Inset * inset);
252
253         /// This signal is emitted when some dialogs needs to be updated.
254         void updateDialog(std::string const & name, std::string const & data);
255
256         ///
257         void setGuiDelegate(frontend::GuiBufferViewDelegate *);
258
259 private:
260         // the position relative to (0, baseline) of outermost paragraph
261         Point coordOffset(DocIterator const & dit, bool boundary) const;
262         /// Update current paragraph metrics.
263         /// \return true if no further update is needed.
264         bool singleParUpdate();
265         ///
266         bool multiParSel();
267
268         /// Search recursively for the the innermost inset that covers (x, y) position.
269         /// \retval 0 if no inset is found.
270         Inset const * getCoveringInset(
271                 Text const & text, //< The Text where we start searching.
272                 int x, //< x-coordinate on screen
273                 int y  //< y-coordinate on screen
274                 );
275
276         ///
277         int width_;
278         ///
279         int height_;
280         ///
281         ScrollbarParameters scrollbarParameters_;
282
283         ///
284         ViewMetricsInfo metrics_info_;
285         ///
286         CoordCache coord_cache_;
287         ///
288         Buffer & buffer_;
289
290         /// Estimated average par height for scrollbar.
291         int wh_;
292         ///
293         void menuInsertLyXFile(std::string const & filen);
294
295         /// this is used to handle XSelection events in the right manner.
296         struct {
297                 CursorSlice cursor;
298                 CursorSlice anchor;
299                 bool set;
300         } xsel_cache_;
301         ///
302         Cursor cursor_;
303         ///
304         bool multiparsel_cache_;
305         ///
306         pit_type anchor_ref_;
307         ///
308         int offset_ref_;
309         ///
310         void updateOffsetRef();
311         ///
312         bool need_centering_;
313
314         /// keyboard mapping object.
315         boost::scoped_ptr<Intl> const intl_;
316
317         /// last visited inset (kept to send setMouseHover(false) )
318         Inset * last_inset_;
319
320         /// A map from a Text to the associated text metrics
321         typedef std::map<Text const *, TextMetrics> TextMetricsCache;
322         mutable TextMetricsCache text_metrics_;
323
324         // Whom to notify. Not owned, so don't delete.
325         frontend::GuiBufferViewDelegate * gui_;
326 };
327
328 /// some space for drawing the 'nested' markers (in pixel)
329 inline int nestMargin() { return 15; }
330
331 /// margin for changebar
332 inline int changebarMargin() { return 12; }
333
334 /// right margin
335 inline int rightMargin() { return 10; }
336
337 } // namespace lyx
338
339 #endif // BUFFERVIEW_H