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