]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
Fix compiler warning about unused string consts
[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         ///
343         void draw(frontend::Painter & pain, bool paint_caret);
344
345         /// get this view's keyboard map handler.
346         Intl & getIntl();
347         ///
348         Intl const & getIntl() const;
349
350         //
351         // Messages to the GUI
352         //
353         /// This signal is emitted when some message shows up.
354         void message(docstring const & msg);
355
356         /// This signal is emitted when some dialog needs to be shown.
357         void showDialog(std::string const & name);
358
359         /// This signal is emitted when some dialog needs to be shown with
360         /// some data.
361         void showDialog(std::string const & name, std::string const & data,
362                 Inset * inset = nullptr);
363
364         /// This signal is emitted when some dialogs needs to be updated.
365         void updateDialog(std::string const & name, std::string const & data);
366
367         ///
368         void setGuiDelegate(frontend::GuiBufferViewDelegate *);
369
370         ///
371         docstring contentsOfPlaintextFile(support::FileName const & f);
372         // Insert plain text file (if filename is empty, prompt for one)
373         void insertPlaintextFile(support::FileName const & f, bool asParagraph);
374         ///
375         void insertLyXFile(support::FileName const & f, bool const ignorelang = false);
376         /// save temporary bookmark for jump back navigation
377         void bookmarkEditPosition();
378         /// Find and return the inset associated with given dialog name.
379         Inset * editedInset(std::string const & name) const;
380         /// Associate an inset associated with given dialog name.
381         void editInset(std::string const & name, Inset * inset);
382         ///
383         void clearLastInset(Inset * inset) const;
384         /// Is the mouse hovering a clickable inset or element?
385         bool clickableInset() const;
386         ///
387         void makeDocumentClass();
388         /// Are we currently performing a selection with the mouse?
389         bool mouseSelecting() const;
390
391 private:
392         /// noncopyable
393         BufferView(BufferView const &);
394         void operator=(BufferView const &);
395
396         /// the position relative to (0, baseline) of outermost paragraph
397         Point coordOffset(DocIterator const & dit) const;
398         /// Update current paragraph metrics.
399         /// \return true if no further update is needed.
400         bool singleParUpdate();
401         /// do the work for the public updateMetrics()
402         void updateMetrics(Update::flags & update_flags);
403
404         // Set the row on which the cursor lives.
405         void setCurrentRowSlice(CursorSlice const & rowSlice);
406
407         // Check whether the row where the cursor lives needs to be scrolled.
408         // Update the drawing strategy if needed.
409         void checkCursorScrollOffset();
410
411         /// The minimal size of the document that is visible. Used
412         /// when it is allowed to scroll below the document.
413         int minVisiblePart();
414
415         /// Search recursively for the innermost inset that covers (x, y) position.
416         /// \retval 0 if no inset is found.
417         Inset const * getCoveringInset(
418                 Text const & text, //< The Text where we start searching.
419                 int x, //< x-coordinate on screen
420                 int y  //< y-coordinate on screen
421                 ) const;
422
423         /// Update the hovering status of the insets. This is called when
424         /// either the screen is updated or when the buffer has scolled.
425         void updateHoveredInset() const;
426
427         ///
428         void updateDocumentClass(DocumentClassConstPtr olddc);
429         ///
430         int width_;
431         ///
432         int height_;
433         ///
434         bool full_screen_;
435         ///
436         Buffer & buffer_;
437
438         struct Private;
439         Private * const d;
440 };
441
442 /// some space for drawing the 'nested' markers (in pixel)
443 inline int nestMargin() { return 15; }
444
445 /// margin for changebar
446 inline int changebarMargin() { return 12; }
447
448 } // namespace lyx
449
450 #endif // BUFFERVIEW_H