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