]> git.lyx.org Git - features.git/blob - src/BufferView.h
Fix compilation for clang 3.7 with libc++
[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 "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         /// \return the clickable math inset for the given position
168         Inset const * clickableMathInset(InsetMathNest const * inset,
169                 CoordCache::Insets const & inset_cache, int x, int y) const;
170
171         /// Save the current position as bookmark.
172         /// if idx == 0, save to temp_bookmark
173         void saveBookmark(unsigned int idx);
174         /// goto a specified position, try top_id first, and then bottom_pit.
175         /// \return true if success
176         bool moveToPosition(
177                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
178                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
179                 int top_id, ///< Paragraph ID, \sa Paragraph
180                 pos_type top_pos ///< Position in the \c Paragraph
181                 );
182         /// return the current change at the cursor.
183         Change const getCurrentChange() const;
184
185         /// move cursor to the named label.
186         void gotoLabel(docstring const & label);
187
188         /// set the cursor based on the given TeX source row.
189         bool setCursorFromRow(int row);
190         /// set the cursor based on the given start and end TextEntries.
191         bool setCursorFromEntries(TexRow::TextEntry start, TexRow::TextEntry end);
192
193         /// set cursor to the given inset. Return true if found.
194         bool setCursorFromInset(Inset const *);
195         /// Recenters the BufferView such that the passed cursor
196         /// is in the center.
197         void recenter();
198         /// Ensure that the BufferView cursor is visible.
199         /// This method will automatically scroll and update the BufferView
200         /// (metrics+drawing) if needed.
201         void showCursor();
202         /// Ensure the passed cursor \p dit is visible.
203         /// This method will automatically scroll and update the BufferView
204         /// (metrics+drawing) if needed.
205         /// \param recenter Whether the cursor should be centered on screen
206         void showCursor(DocIterator const & dit, bool recenter,
207                 bool update);
208         /// Scroll to the cursor.
209         void scrollToCursor();
210         /// Scroll to the cursor.
211         /// \param recenter Whether the cursor should be centered on screen
212         bool scrollToCursor(DocIterator const & dit, bool recenter);
213         /// scroll down document by the given number of pixels.
214         int scrollDown(int pixels);
215         /// scroll up document by the given number of pixels.
216         int scrollUp(int pixels);
217         /// scroll document by the given number of pixels.
218         int scroll(int pixels);
219         /// Scroll the view by a number of pixels.
220         void scrollDocView(int pixels, bool update);
221         /// Set the cursor position based on the scrollbar one.
222         void setCursorFromScrollbar();
223
224         /// return the pixel width of the document view.
225         int workWidth() const;
226         /// return the pixel height of the document view.
227         int workHeight() const;
228
229         /// return the inline completion postfix.
230         docstring const & inlineCompletion() const;
231         /// return the number of unique characters in the inline completion.
232         size_t inlineCompletionUniqueChars() const;
233         /// return the position in the buffer of the inline completion postfix.
234         DocIterator const & inlineCompletionPos() const;
235         /// make sure inline completion position is OK
236         void resetInlineCompletionPos();
237         /// set the inline completion postfix and its position in the buffer.
238         /// Updates the updateFlags in \c cur.
239         void setInlineCompletion(Cursor const & cur, DocIterator const & pos,
240                 docstring const & completion, size_t uniqueChars = 0);
241
242         /// translate and insert a character, using the correct keymap.
243         void translateAndInsert(char_type c, Text * t, Cursor & cur);
244
245         /// \return true if we've made a decision
246         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
247         /// execute the given function.
248         void dispatch(FuncRequest const & cmd, DispatchResult & dr);
249
250         /// request an X11 selection.
251         /// \return the selected string.
252         docstring requestSelection();
253         /// clear the X11 selection.
254         void clearSelection();
255
256         /// resize the BufferView.
257         /// \sa WorkArea
258         void resize(int width, int height);
259
260         /// dispatch method helper for \c WorkArea
261         /// \sa WorkArea
262         void mouseEventDispatch(FuncRequest const & ev);
263
264         ///
265         CursorStatus cursorStatus(DocIterator const & dit) const;
266         /// access to full cursor.
267         Cursor & cursor();
268         /// access to full cursor.
269         Cursor const & cursor() const;
270         /// sets cursor.
271         /// This will also open all relevant collapsible insets.
272         void setCursor(DocIterator const &);
273         /// set the selection up to dit.
274         void setCursorSelectionTo(DocIterator const & dit);
275         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
276         /// \retval true if an update was needed.
277         bool checkDepm(Cursor & cur, Cursor & old);
278         /// sets cursor.
279         /// This is used when handling LFUN_MOUSE_PRESS.
280         bool mouseSetCursor(Cursor & cur, bool select = false);
281
282         /// sets the selection.
283         /* When \c backwards == false, set anchor
284          * to \c cur and cursor to \c cur + \c length. When \c
285          * backwards == true, set anchor to \c cur and cursor to \c
286          * cur + \c length.
287          */
288         void putSelectionAt(DocIterator const & cur,
289                 int length, bool backwards);
290
291         /// selects the item at cursor if its paragraph is empty.
292         bool selectIfEmpty(DocIterator & cur);
293
294         /// update the internal \c ViewMetricsInfo.
295         void updateMetrics();
296
297         // this is the "nodraw" drawing stage: only set the positions of the
298         // insets in metrics cache.
299         void updatePosCache();
300
301         ///
302         TextMetrics const & textMetrics(Text const * t) const;
303         TextMetrics & textMetrics(Text const * t);
304         ///
305         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
306
307         ///
308         CoordCache & coordCache();
309         ///
310         CoordCache const & coordCache() const;
311
312         ///
313         MathRow const & mathRow(MathData const * cell) const;
314         ///
315         void setMathRow(MathData const * cell, MathRow const & mrow);
316
317         ///
318         Point getPos(DocIterator const & dit) const;
319         /// is the paragraph of the cursor visible ?
320         bool paragraphVisible(DocIterator const & dit) const;
321         /// is the caret currently visible in the view
322         bool caretInView() const;
323         /// get the position and height of the caret
324         void caretPosAndDim(Point & p, Dimension & dim) const;
325         /// compute the shape of the caret
326         void buildCaretGeometry(bool complet);
327         /// the shape of the caret
328         frontend::CaretGeometry const & caretGeometry() const;
329
330         ///
331         void draw(frontend::Painter & pain, bool paint_caret);
332
333         /// get this view's keyboard map handler.
334         Intl & getIntl();
335         ///
336         Intl const & getIntl() const;
337
338         //
339         // Messages to the GUI
340         //
341         /// This signal is emitted when some message shows up.
342         void message(docstring const & msg);
343
344         /// This signal is emitted when some dialog needs to be shown.
345         void showDialog(std::string const & name);
346
347         /// This signal is emitted when some dialog needs to be shown with
348         /// some data.
349         void showDialog(std::string const & name, std::string const & data,
350                 Inset * inset = nullptr);
351
352         /// This signal is emitted when some dialogs needs to be updated.
353         void updateDialog(std::string const & name, std::string const & data);
354
355         ///
356         void setGuiDelegate(frontend::GuiBufferViewDelegate *);
357
358         ///
359         docstring contentsOfPlaintextFile(support::FileName const & f);
360         // Insert plain text file (if filename is empty, prompt for one)
361         void insertPlaintextFile(support::FileName const & f, bool asParagraph);
362         ///
363         void insertLyXFile(support::FileName const & f, bool const ignorelang = false);
364         /// save temporary bookmark for jump back navigation
365         void bookmarkEditPosition();
366         /// Find and return the inset associated with given dialog name.
367         Inset * editedInset(std::string const & name) const;
368         /// Associate an inset associated with given dialog name.
369         void editInset(std::string const & name, Inset * inset);
370         ///
371         void clearLastInset(Inset * inset) const;
372         /// Is the mouse hovering a clickable inset or element?
373         bool clickableInset() const;
374         ///
375         void makeDocumentClass();
376
377 private:
378         /// noncopyable
379         BufferView(BufferView const &);
380         void operator=(BufferView const &);
381
382         /// the position relative to (0, baseline) of outermost paragraph
383         Point coordOffset(DocIterator const & dit) const;
384         /// Update current paragraph metrics.
385         /// \return true if no further update is needed.
386         bool singleParUpdate();
387         /// do the work for the public updateMetrics()
388         void updateMetrics(Update::flags & update_flags);
389
390         // Set the row on which the cursor lives.
391         void setCurrentRowSlice(CursorSlice const & rowSlice);
392
393         // Check whether the row where the cursor lives needs to be scrolled.
394         // Update the drawing strategy if needed.
395         void checkCursorScrollOffset();
396
397         /// The minimal size of the document that is visible. Used
398         /// when it is allowed to scroll below the document.
399         int minVisiblePart();
400
401         /// Search recursively for the innermost inset that covers (x, y) position.
402         /// \retval 0 if no inset is found.
403         Inset const * getCoveringInset(
404                 Text const & text, //< The Text where we start searching.
405                 int x, //< x-coordinate on screen
406                 int y  //< y-coordinate on screen
407                 ) const;
408
409         /// Update the hovering status of the insets. This is called when
410         /// either the screen is updated or when the buffer has scolled.
411         void updateHoveredInset() const;
412
413         ///
414         void updateDocumentClass(DocumentClassConstPtr olddc);
415         ///
416         int width_;
417         ///
418         int height_;
419         ///
420         bool full_screen_;
421         ///
422         Buffer & buffer_;
423
424         struct Private;
425         Private * const d;
426 };
427
428 /// some space for drawing the 'nested' markers (in pixel)
429 inline int nestMargin() { return 15; }
430
431 /// margin for changebar
432 inline int changebarMargin() { return 12; }
433
434 } // namespace lyx
435
436 #endif // BUFFERVIEW_H