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