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