]> git.lyx.org Git - features.git/blob - src/BufferView.h
Helpers to set selections to arbitrary DocIterators
[features.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 "DocumentClassPtr.h"
19 #include "update_flags.h"
20
21 #include "support/strfwd.h"
22 #include "support/types.h"
23
24 namespace lyx {
25
26 namespace support { class FileName; }
27
28 namespace frontend { class Painter; }
29 namespace frontend { class GuiBufferViewDelegate; }
30
31 class Buffer;
32 class Change;
33 class CoordCache;
34 class Cursor;
35 class CursorSlice;
36 class DispatchResult;
37 class DocIterator;
38 class DocumentClass;
39 class FuncRequest;
40 class FuncStatus;
41 class Intl;
42 class Inset;
43 class PainterInfo;
44 class ParIterator;
45 class ParagraphMetrics;
46 class Point;
47 class TexRow;
48 class Text;
49 class TextMetrics;
50
51 enum CursorStatus {
52         CUR_INSIDE,
53         CUR_ABOVE,
54         CUR_BELOW
55 };
56
57 /// Scrollbar Parameters.
58 struct ScrollbarParameters
59 {
60         // These parameters are normalized against the screen geometry and pixel
61         // coordinates. Position 0 corresponds to the top the the screen.
62         ScrollbarParameters()
63                 : min(0), max(0), single_step(1), page_step(1)
64         {}
65         /// Minimum scrollbar position in pixels.
66         int min;
67         /// Maximum scrollbar position in pixels.
68         int max;
69         /// Line-scroll amount in pixels.
70         int single_step;
71         /// Page-scroll amount in pixels.
72         int page_step;
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 {
88 public:
89         ///
90         explicit BufferView(Buffer & buffer);
91         ///
92         ~BufferView();
93
94         /// return the buffer being viewed.
95         Buffer & buffer();
96         Buffer const & buffer() const;
97
98         ///
99         void setFullScreen(bool full_screen) { full_screen_ = full_screen; }
100
101         /// right margin
102         int rightMargin() const;
103
104         /// left margin
105         int leftMargin() const;
106
107         /// \return true if the BufferView is at the top of the document.
108         bool isTopScreen() const;
109
110         /// \return true if the BufferView is at the bottom of the document.
111         bool isBottomScreen() const;
112
113         /// perform pending metrics updates.
114         /** \c Update::FitCursor means first to do a FitCursor, and to
115          * force an update if screen position changes.
116          * \c Update::Force means to force an update in any case.
117          * \retval true if a screen redraw is needed
118          */
119         void processUpdateFlags(Update::flags flags);
120
121         /// return true if one shall move the screen to fit the cursor.
122         /// Only to be called with good y coordinates (after a bv::metrics)
123         bool needsFitCursor() const;
124
125         // Returns the amount of horizontal scrolling applied to the
126         // top-level row where the cursor lies
127         int horizScrollOffset() const;
128         // Returns the amount of horizontal scrolling applied to the
129         // row of text starting at (pit, pos)
130         int horizScrollOffset(Text const * text,
131                               pit_type pit, pos_type pos) const;
132
133         // Returns true if the row of text starting at (pit, pos) was scrolled
134         // at the last draw event.
135         bool hadHorizScrollOffset(Text const * text,
136                               pit_type pit, pos_type pos) const;
137
138         /// reset the scrollbar to reflect current view position.
139         void updateScrollbar();
140         /// return the Scrollbar Parameters.
141         ScrollbarParameters const & scrollbarParameters() const;
142         /// \return Tool tip for the given position.
143         docstring toolTip(int x, int y) const;
144         /// \return the context menu for the given position.
145         std::string contextMenu(int x, int y) const;
146
147         /// Save the current position as bookmark.
148         /// if idx == 0, save to temp_bookmark
149         void saveBookmark(unsigned int idx);
150         /// goto a specified position, try top_id first, and then bottom_pit.
151         /// \return true if success
152         bool moveToPosition(
153                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
154                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
155                 int top_id, ///< Paragraph ID, \sa Paragraph
156                 pos_type top_pos ///< Position in the \c Paragraph
157                 );
158         /// return the current change at the cursor.
159         Change const getCurrentChange() const;
160
161         /// move cursor to the named label.
162         void gotoLabel(docstring const & label);
163
164         /// set the cursor based on the given TeX source row.
165         void setCursorFromRow(int row);
166         ///
167         void setCursorFromRow(int row, TexRow const & texrow);
168
169         /// set cursor to the given inset. Return true if found.
170         bool setCursorFromInset(Inset const *);
171         /// Recenters the BufferView such that the passed cursor
172         /// is in the center.
173         void recenter();
174         /// Ensure that the BufferView cursor is visible.
175         /// This method will automatically scroll and update the BufferView
176         /// (metrics+drawing) if needed.
177         void showCursor();
178         /// Ensure the passed cursor \p dit is visible.
179         /// This method will automatically scroll and update the BufferView
180         /// (metrics+drawing) if needed.
181         /// \param recenter Whether the cursor should be centered on screen
182         void showCursor(DocIterator const & dit, bool recenter,
183                 bool update);
184         /// Scroll to the cursor.
185         void scrollToCursor();
186         /// Scroll to the cursor.
187         /// \param recenter Whether the cursor should be centered on screen
188         bool scrollToCursor(DocIterator const & dit, bool recenter);
189         /// scroll down document by the given number of pixels.
190         int scrollDown(int pixels);
191         /// scroll up document by the given number of pixels.
192         int scrollUp(int pixels);
193         /// scroll document by the given number of pixels.
194         int scroll(int pixels);
195         /// Scroll the view by a number of pixels.
196         void scrollDocView(int pixels, bool update);
197         /// Set the cursor position based on the scrollbar one.
198         void setCursorFromScrollbar();
199
200         /// return the pixel width of the document view.
201         int workWidth() const;
202         /// return the pixel height of the document view.
203         int workHeight() const;
204
205         /// return the inline completion postfix.
206         docstring const & inlineCompletion() const;
207         /// return the number of unique characters in the inline completion.
208         size_t const & inlineCompletionUniqueChars() const;
209         /// return the position in the buffer of the inline completion postfix.
210         DocIterator const & inlineCompletionPos() const;
211         /// make sure inline completion position is OK
212         void resetInlineCompletionPos();
213         /// set the inline completion postfix and its position in the buffer.
214         /// Updates the updateFlags in \c cur.
215         void setInlineCompletion(Cursor const & cur, DocIterator const & pos,
216                 docstring const & completion, size_t uniqueChars = 0);
217
218         /// translate and insert a character, using the correct keymap.
219         void translateAndInsert(char_type c, Text * t, Cursor & cur);
220
221         /// \return true if we've made a decision
222         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
223         /// execute the given function.
224         void dispatch(FuncRequest const & cmd, DispatchResult & dr);
225
226         /// request an X11 selection.
227         /// \return the selected string.
228         docstring const requestSelection();
229         /// clear the X11 selection.
230         void clearSelection();
231
232         /// resize the BufferView.
233         /// \sa WorkArea
234         void resize(int width, int height);
235
236         /// dispatch method helper for \c WorkArea
237         /// \sa WorkArea
238         void mouseEventDispatch(FuncRequest const & ev);
239
240         /// access to anchor.
241         pit_type anchor_ref() const;
242
243         ///
244         CursorStatus cursorStatus(DocIterator const & dit) const;
245         /// access to full cursor.
246         Cursor & cursor();
247         /// access to full cursor.
248         Cursor const & cursor() const;
249         /// sets cursor.
250         /// This will also open all relevant collapsable insets.
251         void setCursor(DocIterator const &);
252         /// set the selection up to dit.
253         void setCursorSelectionTo(DocIterator const & dit);
254         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
255         /// \retval true if an update was needed.
256         bool checkDepm(Cursor & cur, Cursor & old);
257         /// sets cursor.
258         /// This is used when handling LFUN_MOUSE_PRESS.
259         bool mouseSetCursor(Cursor & cur, bool select = false);
260
261         /// sets the selection.
262         /* When \c backwards == false, set anchor
263          * to \c cur and cursor to \c cur + \c length. When \c
264          * backwards == true, set anchor to \c cur and cursor to \c
265          * cur + \c length.
266          */
267         void putSelectionAt(DocIterator const & cur,
268                 int length, bool backwards);
269
270         /// selects the item at cursor if its paragraph is empty.
271         bool selectIfEmpty(DocIterator & cur);
272
273         /// update the internal \c ViewMetricsInfo.
274         void updateMetrics();
275
276         ///
277         TextMetrics const & textMetrics(Text const * t) const;
278         TextMetrics & textMetrics(Text const * t);
279         ///
280         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
281
282         ///
283         CoordCache & coordCache();
284         ///
285         CoordCache const & coordCache() const;
286
287         ///
288         Point getPos(DocIterator const & dit) const;
289         /// is the paragraph of the cursor visible ?
290         bool paragraphVisible(DocIterator const & dit) const;
291         /// is the cursor currently visible in the view
292         bool cursorInView(Point const & p, int h) const;
293         /// get the position and height of the cursor
294         void cursorPosAndHeight(Point & p, int & h) const;
295
296
297         ///
298         void draw(frontend::Painter & pain);
299
300         /// get this view's keyboard map handler.
301         Intl & getIntl();
302         ///
303         Intl const & getIntl() const;
304
305         //
306         // Messages to the GUI
307         //
308         /// This signal is emitted when some message shows up.
309         void message(docstring const & msg);
310
311         /// This signal is emitted when some dialog needs to be shown.
312         void showDialog(std::string const & name);
313
314         /// This signal is emitted when some dialog needs to be shown with
315         /// some data.
316         void showDialog(std::string const & name, std::string const & data,
317                 Inset * inset = 0);
318
319         /// This signal is emitted when some dialogs needs to be updated.
320         void updateDialog(std::string const & name, std::string const & data);
321
322         ///
323         void setGuiDelegate(frontend::GuiBufferViewDelegate *);
324
325         ///
326         docstring contentsOfPlaintextFile(support::FileName const & f);
327         // Insert plain text file (if filename is empty, prompt for one)
328         void insertPlaintextFile(support::FileName const & f, bool asParagraph);
329         ///
330         void insertLyXFile(support::FileName const & f);
331         /// save temporary bookmark for jump back navigation
332         void bookmarkEditPosition();
333         /// Find and return the inset associated with given dialog name.
334         Inset * editedInset(std::string const & name) const;
335         /// Associate an inset associated with given dialog name.
336         void editInset(std::string const & name, Inset * inset);
337         ///
338         void clearLastInset(Inset * inset) const;
339         /// Is the mouse hovering a clickable inset or element?
340         bool clickableInset() const;
341         ///
342         void makeDocumentClass();
343
344 private:
345         /// noncopyable
346         BufferView(BufferView const &);
347         void operator=(BufferView const &);
348
349         /// the position relative to (0, baseline) of outermost paragraph
350         Point coordOffset(DocIterator const & dit) const;
351         /// Update current paragraph metrics.
352         /// \return true if no further update is needed.
353         bool singleParUpdate();
354
355         // Set the row on which the cursor lives.
356         void setCurrentRowSlice(CursorSlice const & rowSlice);
357
358         // Check whether the row where the cursor lives needs to be scrolled.
359         // Update the drawing strategy if needed.
360         void checkCursorScrollOffset(PainterInfo & pi);
361
362         /// The minimal size of the document that is visible. Used
363         /// when it is allowed to scroll below the document.
364         int minVisiblePart();
365
366         /// Search recursively for the innermost inset that covers (x, y) position.
367         /// \retval 0 if no inset is found.
368         Inset const * getCoveringInset(
369                 Text const & text, //< The Text where we start searching.
370                 int x, //< x-coordinate on screen
371                 int y  //< y-coordinate on screen
372                 ) const;
373
374         /// Update the hovering status of the insets. This is called when
375         /// either the screen is updated or when the buffer has scolled.
376         void updateHoveredInset() const;
377
378         ///
379         void updateDocumentClass(DocumentClassConstPtr olddc);
380         ///
381         int width_;
382         ///
383         int height_;
384         ///
385         bool full_screen_;
386         ///
387         Buffer & buffer_;
388
389         struct Private;
390         Private * const d;
391 };
392
393 /// some space for drawing the 'nested' markers (in pixel)
394 inline int nestMargin() { return 15; }
395
396 /// margin for changebar
397 inline int changebarMargin() { return 12; }
398
399 } // namespace lyx
400
401 #endif // BUFFERVIEW_H