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