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