]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
Fix PageUp/Down select when at the top/bottom of a document.
[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 "update_flags.h"
19
20 #include "support/strfwd.h"
21 #include "support/types.h"
22
23 namespace lyx {
24
25 namespace support { class FileName; }
26
27 namespace frontend { class Painter; }
28 namespace frontend { class GuiBufferViewDelegate; }
29
30 class Buffer;
31 class Change;
32 class CoordCache;
33 class Cursor;
34 class DocIterator;
35 class FuncRequest;
36 class FuncStatus;
37 class Intl;
38 class Inset;
39 class Menu;
40 class ParIterator;
41 class ParagraphMetrics;
42 class Point;
43 class Text;
44 class TextMetrics;
45
46 enum CursorStatus {
47         CUR_INSIDE,
48         CUR_ABOVE,
49         CUR_BELOW
50 };
51
52 /// Scrollbar Parameters.
53 struct ScrollbarParameters
54 {
55         /// Minimum scrollbar position in pixels.
56         int min;
57         /// Maximum scrollbar position in pixels.
58         int max;
59         /// Current position in the document in pixels.
60         int position;
61         /// Line-scroll amount in pixels.
62         int single_step;
63         /// Page-scroll amount in pixels.
64         int page_step;
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 {
80 public:
81         ///
82         explicit BufferView(Buffer & buffer);
83         ///
84         ~BufferView();
85
86         /// return the buffer being viewed.
87         Buffer & buffer();
88         Buffer const & buffer() const;
89
90         ///
91         void setFullScreen(bool full_screen) { full_screen_ = full_screen; }
92
93         /// right margin
94         int rightMargin() const;
95
96         /// left margin
97         int leftMargin() const;
98
99         /// \return true if the BufferView is at the top of the document.
100         bool isTopScreen() const;
101
102         /// \return true if the BufferView is at the bottom of the document.
103         bool isBottomScreen() const;
104
105         /// perform pending metrics updates.
106         /** \c Update::FitCursor means first to do a FitCursor, and to
107          * force an update if screen position changes.
108          * \c Update::Force means to force an update in any case.
109          * \retval true if a screen redraw is needed
110          */
111         void processUpdateFlags(Update::flags flags);
112
113         /// move the screen to fit the cursor.
114         /// Only to be called with good y coordinates (after a bv::metrics)
115         bool fitCursor();
116         /// reset the scrollbar to reflect current view position.
117         void updateScrollbar();
118         /// return the Scrollbar Parameters.
119         ScrollbarParameters const & scrollbarParameters() const;
120         /// \return Tool tip for the given position.
121         docstring toolTip(int x, int y) const;
122         /// \return the context menu for the given position.
123         docstring contextMenu(int x, int y) const;
124
125         /// Save the current position as bookmark.
126         /// if idx == 0, save to temp_bookmark
127         void saveBookmark(unsigned int idx);
128         /// goto a specified position, try top_id first, and then bottom_pit.
129         /// \return true if success
130         bool moveToPosition(
131                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
132                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
133                 int top_id, ///< Paragraph ID, \sa Paragraph
134                 pos_type top_pos ///< Position in the \c Paragraph
135                 );
136         /// return the current change at the cursor.
137         Change const getCurrentChange() const;
138
139         /// move cursor to the named label.
140         void gotoLabel(docstring const & label);
141
142         /// set the cursor based on the given TeX source row.
143         void setCursorFromRow(int row);
144
145         /// Ensure that the BufferView cursor is visible.
146         /// This method will automatically scroll and update the BufferView
147         /// if needed.
148         void showCursor();
149         /// Ensure the passed cursor \p dit is visible.
150         /// This method will automatically scroll and update the BufferView
151         /// if needed.
152         void showCursor(DocIterator const & dit);
153         /// LFUN_SCROLL Helper.
154         void lfunScroll(FuncRequest const & cmd);
155         /// scroll down document by the given number of pixels.
156         void scrollDown(int pixels);
157         /// scroll up document by the given number of pixels.
158         void scrollUp(int pixels);
159         /// scroll document by the given number of pixels.
160         void scroll(int pixels);
161         /// Scroll the view by a number of pixels.
162         void scrollDocView(int pixels);
163         /// Set the cursor position based on the scrollbar one.
164         void setCursorFromScrollbar();
165
166         /// return the pixel width of the document view.
167         int workWidth() const;
168         /// return the pixel height of the document view.
169         int workHeight() const;
170
171         /// return the inline completion postfix.
172         docstring const & inlineCompletion() const;
173         /// return the number of unique characters in the inline completion.
174         size_t const & inlineCompletionUniqueChars() const;
175         /// return the position in the buffer of the inline completion postfix.
176         DocIterator const & inlineCompletionPos() const;
177         /// set the inline completion postfix and its position in the buffer.
178         /// Updates the updateFlags in \c cur.
179         void setInlineCompletion(Cursor & cur, DocIterator const & pos,
180                 docstring const & completion, size_t uniqueChars = 0);
181
182         /// translate and insert a character, using the correct keymap.
183         void translateAndInsert(char_type c, Text * t, Cursor & cur);
184
185         /// return true for events that will handle.
186         FuncStatus getStatus(FuncRequest const & cmd);
187         /// execute the given function.
188         /// \return true if the function has been processed.
189         bool dispatch(FuncRequest const & argument);
190
191         /// request an X11 selection.
192         /// \return the selected string.
193         docstring const requestSelection();
194         /// clear the X11 selection.
195         void clearSelection();
196
197         /// resize the BufferView.
198         /// \sa WorkArea
199         void resize(int width, int height);
200
201         /// dispatch method helper for \c WorkArea
202         /// \sa WorkArea
203         void mouseEventDispatch(FuncRequest const & ev);
204
205         /// access to anchor.
206         pit_type anchor_ref() const;
207
208         ///
209         CursorStatus cursorStatus(DocIterator const & dit) const;
210         /// access to full cursor.
211         Cursor & cursor();
212         /// access to full cursor.
213         Cursor const & cursor() const;
214         /// sets cursor.
215         /// This will also open all relevant collapsable insets.
216         void setCursor(DocIterator const &);
217         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
218         /// \retval true if an update was needed.
219         bool checkDepm(Cursor & cur, Cursor & old);
220         /// sets cursor.
221         /// This is used when handling LFUN_MOUSE_PRESS.
222         bool mouseSetCursor(Cursor & cur, bool select = false);
223
224         /// sets the selection.
225         /* When \c backwards == false, set anchor
226          * to \c cur and cursor to \c cur + \c length. When \c
227          * backwards == true, set anchor to \c cur and cursor to \c
228          * cur + \c length.
229          */
230         void putSelectionAt(DocIterator const & cur,
231                 int length, bool backwards);
232
233         /// update the internal \c ViewMetricsInfo.
234         void updateMetrics();
235
236         ///
237         TextMetrics const & textMetrics(Text const * t) const;
238         TextMetrics & textMetrics(Text const * t);
239         ///
240         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
241
242         ///
243         CoordCache & coordCache();
244         ///
245         CoordCache const & coordCache() const;
246
247         ///
248         Point getPos(DocIterator const & dit, bool boundary) const;
249
250
251         ///
252         void draw(frontend::Painter & pain);
253
254         /// get this view's keyboard map handler.
255         Intl & getIntl();
256         ///
257         Intl const & getIntl() const;
258
259         //
260         // Messages to the GUI
261         //
262         /// This signal is emitted when some message shows up.
263         void message(docstring const & msg);
264
265         /// This signal is emitted when some dialog needs to be shown.
266         void showDialog(std::string const & name);
267
268         /// This signal is emitted when some dialog needs to be shown with
269         /// some data.
270         void showDialog(std::string const & name, std::string const & data,
271                 Inset * inset = 0);
272
273         /// This signal is emitted when some dialogs needs to be updated.
274         void updateDialog(std::string const & name, std::string const & data);
275
276         ///
277         void setGuiDelegate(frontend::GuiBufferViewDelegate *);
278
279         ///
280         docstring contentsOfPlaintextFile(support::FileName const & f);
281         // Insert plain text file (if filename is empty, prompt for one)
282         void insertPlaintextFile(support::FileName const & f, bool asParagraph);
283         ///
284         void insertLyXFile(support::FileName const & f);
285
286 private:
287         /// noncopyable
288         BufferView(BufferView const &);
289         void operator=(BufferView const &);
290
291         // the position relative to (0, baseline) of outermost paragraph
292         Point coordOffset(DocIterator const & dit, bool boundary) const;
293         /// Update current paragraph metrics.
294         /// \return true if no further update is needed.
295         bool singleParUpdate();
296
297         /// Search recursively for the the innermost inset that covers (x, y) position.
298         /// \retval 0 if no inset is found.
299         Inset const * getCoveringInset(
300                 Text const & text, //< The Text where we start searching.
301                 int x, //< x-coordinate on screen
302                 int y  //< y-coordinate on screen
303                 ) const;
304
305         ///
306         int width_;
307         ///
308         int height_;
309         ///
310         bool full_screen_;
311         ///
312         Buffer & buffer_;
313
314         struct Private;
315         Private * const d;
316 };
317
318 /// some space for drawing the 'nested' markers (in pixel)
319 inline int nestMargin() { return 15; }
320
321 /// margin for changebar
322 inline int changebarMargin() { return 12; }
323
324 } // namespace lyx
325
326 #endif // BUFFERVIEW_H