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