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