]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
Small API cleanup: as BufferView already use Buffer::changed() make it use it some...
[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         void mouseEventDispatch(FuncRequest const & ev);
174
175         /// access to anchor.
176         pit_type anchor_ref() const;
177
178         ///
179         CursorStatus cursorStatus(DocIterator const & dit) const;
180         /// access to full cursor.
181         Cursor & cursor();
182         /// access to full cursor.
183         Cursor 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(Cursor & cur, Cursor & old);
190         /// sets cursor.
191         /// This is used when handling LFUN_MOUSE_PRESS.
192         bool mouseSetCursor(Cursor & 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(Text const * t) const;
214         TextMetrics & textMetrics(Text const * t);
215         ///
216         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
217
218         ///
219         CoordCache & coordCache() { return coord_cache_; }
220         ///
221         CoordCache const & coordCache() const { return coord_cache_; }
222
223         ///
224         Point getPos(DocIterator const & dit, bool boundary) const;
225
226
227         ///
228         void draw(frontend::Painter & pain);
229
230         /// get this view's keyboard map handler.
231         Intl & getIntl() { return *intl_.get(); }
232         ///
233         Intl const & getIntl() const { return *intl_.get(); }
234
235         //
236         // Messages to the GUI
237         //
238         /// This signal is emitted when some message shows up.
239         void message(docstring const & msg);
240
241         /// This signal is emitted when some dialog needs to be shown.
242         void showDialog(std::string const & name);
243
244         /// This signal is emitted when some dialog needs to be shown with
245         /// some data.
246         void showDialogWithData(std::string const & name, std::string const & data);
247
248         /// This signal is emitted when some inset dialogs needs to be shown.
249         void showInsetDialog(std::string const & name, std::string const & data,
250                 Inset * inset);
251
252         /// This signal is emitted when some dialogs needs to be updated.
253         void updateDialog(std::string const & name, std::string const & data);
254
255         ///
256         void setGuiDelegate(frontend::GuiBufferViewDelegate *);
257
258         ///
259         docstring contentsOfPlaintextFile(std::string const & f, bool asParagraph);
260         // Insert plain text file (if filename is empty, prompt for one)
261         void insertPlaintextFile(std::string const & fileName, bool asParagraph);
262 private:
263         // the position relative to (0, baseline) of outermost paragraph
264         Point coordOffset(DocIterator const & dit, bool boundary) const;
265         /// Update current paragraph metrics.
266         /// \return true if no further update is needed.
267         bool singleParUpdate();
268         ///
269         bool multiParSel();
270
271         /// Search recursively for the the innermost inset that covers (x, y) position.
272         /// \retval 0 if no inset is found.
273         Inset const * getCoveringInset(
274                 Text const & text, //< The Text where we start searching.
275                 int x, //< x-coordinate on screen
276                 int y  //< y-coordinate on screen
277                 );
278
279         ///
280         int width_;
281         ///
282         int height_;
283         ///
284         ScrollbarParameters scrollbarParameters_;
285
286         ///
287         ViewMetricsInfo metrics_info_;
288         ///
289         CoordCache coord_cache_;
290         ///
291         Buffer & buffer_;
292
293         /// Estimated average par height for scrollbar.
294         int wh_;
295         ///
296         void menuInsertLyXFile(std::string const & filen);
297
298         /// this is used to handle XSelection events in the right manner.
299         struct {
300                 CursorSlice cursor;
301                 CursorSlice anchor;
302                 bool set;
303         } xsel_cache_;
304         ///
305         Cursor cursor_;
306         ///
307         bool multiparsel_cache_;
308         ///
309         pit_type anchor_ref_;
310         ///
311         int offset_ref_;
312         ///
313         void updateOffsetRef();
314         ///
315         bool need_centering_;
316
317         /// keyboard mapping object.
318         boost::scoped_ptr<Intl> const intl_;
319
320         /// last visited inset (kept to send setMouseHover(false) )
321         Inset * last_inset_;
322
323         /// A map from a Text to the associated text metrics
324         typedef std::map<Text const *, TextMetrics> TextMetricsCache;
325         mutable TextMetricsCache text_metrics_;
326
327         // Whom to notify. Not owned, so don't delete.
328         frontend::GuiBufferViewDelegate * gui_;
329 };
330
331 /// some space for drawing the 'nested' markers (in pixel)
332 inline int nestMargin() { return 15; }
333
334 /// margin for changebar
335 inline int changebarMargin() { return 12; }
336
337 /// right margin
338 inline int rightMargin() { return 10; }
339
340 } // namespace lyx
341
342 #endif // BUFFERVIEW_H