]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
7de6985d44001d7e3c94d902671c975188398cbe
[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 true if the BufferView is at the top of the document.
118         bool isTopScreen() const;
119
120         /// \return true if the BufferView is at the bottom of the document.
121         bool isBottomScreen() const;
122
123         /// perform pending metrics updates.
124         /** \c Update::FitCursor means first to do a FitCursor, and to
125          * force an update if screen position changes.
126          * \c Update::Force means to force an update in any case.
127          * \retval true if a screen redraw is needed
128          */
129         void processUpdateFlags(Update::flags flags);
130
131         /// return true if one shall move the screen to fit the cursor.
132         /// Only to be called with good y coordinates (after a bv::metrics)
133         bool needsFitCursor() const;
134
135         /// returns true if this row needs to be repainted (to erase caret)
136         bool needRepaint(Text const * text, Row const & row) const;
137
138         // Returns the amount of horizontal scrolling applied to the
139         // top-level row where the cursor lies
140         int horizScrollOffset() const;
141         // Returns the amount of horizontal scrolling applied to the
142         // row of text starting at (pit, pos)
143         int horizScrollOffset(Text const * text,
144                               pit_type pit, pos_type pos) const;
145
146         // Returns true if the row of text starting at (pit, pos) was scrolled
147         // at the last draw event.
148         bool hadHorizScrollOffset(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         /// access to anchor.
254         pit_type anchor_ref() const;
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 collapsable 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         Point getPos(DocIterator const & dit) const;
306         /// is the paragraph of the cursor visible ?
307         bool paragraphVisible(DocIterator const & dit) const;
308         /// is the cursor currently visible in the view
309         bool cursorInView(Point const & p, int h) const;
310         /// get the position and height of the caret
311         void caretPosAndHeight(Point & p, int & h) const;
312
313         ///
314         void draw(frontend::Painter & pain, bool paint_caret);
315
316         /// get this view's keyboard map handler.
317         Intl & getIntl();
318         ///
319         Intl const & getIntl() const;
320
321         //
322         // Messages to the GUI
323         //
324         /// This signal is emitted when some message shows up.
325         void message(docstring const & msg);
326
327         /// This signal is emitted when some dialog needs to be shown.
328         void showDialog(std::string const & name);
329
330         /// This signal is emitted when some dialog needs to be shown with
331         /// some data.
332         void showDialog(std::string const & name, std::string const & data,
333                 Inset * inset = 0);
334
335         /// This signal is emitted when some dialogs needs to be updated.
336         void updateDialog(std::string const & name, std::string const & data);
337
338         ///
339         void setGuiDelegate(frontend::GuiBufferViewDelegate *);
340
341         ///
342         docstring contentsOfPlaintextFile(support::FileName const & f);
343         // Insert plain text file (if filename is empty, prompt for one)
344         void insertPlaintextFile(support::FileName const & f, bool asParagraph);
345         ///
346         void insertLyXFile(support::FileName const & f);
347         /// save temporary bookmark for jump back navigation
348         void bookmarkEditPosition();
349         /// Find and return the inset associated with given dialog name.
350         Inset * editedInset(std::string const & name) const;
351         /// Associate an inset associated with given dialog name.
352         void editInset(std::string const & name, Inset * inset);
353         ///
354         void clearLastInset(Inset * inset) const;
355         /// Is the mouse hovering a clickable inset or element?
356         bool clickableInset() const;
357         ///
358         void makeDocumentClass();
359
360 private:
361         /// noncopyable
362         BufferView(BufferView const &);
363         void operator=(BufferView const &);
364
365         /// the position relative to (0, baseline) of outermost paragraph
366         Point coordOffset(DocIterator const & dit) const;
367         /// Update current paragraph metrics.
368         /// \return true if no further update is needed.
369         bool singleParUpdate();
370
371         // Set the row on which the cursor lives.
372         void setCurrentRowSlice(CursorSlice const & rowSlice);
373
374         // Check whether the row where the cursor lives needs to be scrolled.
375         // Update the drawing strategy if needed.
376         void checkCursorScrollOffset();
377
378         /// The minimal size of the document that is visible. Used
379         /// when it is allowed to scroll below the document.
380         int minVisiblePart();
381
382         /// Search recursively for the innermost inset that covers (x, y) position.
383         /// \retval 0 if no inset is found.
384         Inset const * getCoveringInset(
385                 Text const & text, //< The Text where we start searching.
386                 int x, //< x-coordinate on screen
387                 int y  //< y-coordinate on screen
388                 ) const;
389
390         /// Update the hovering status of the insets. This is called when
391         /// either the screen is updated or when the buffer has scolled.
392         void updateHoveredInset() const;
393
394         ///
395         void updateDocumentClass(DocumentClassConstPtr olddc);
396         ///
397         int width_;
398         ///
399         int height_;
400         ///
401         bool full_screen_;
402         ///
403         Buffer & buffer_;
404
405         struct Private;
406         Private * const d;
407 };
408
409 /// some space for drawing the 'nested' markers (in pixel)
410 inline int nestMargin() { return 15; }
411
412 /// margin for changebar
413 inline int changebarMargin() { return 12; }
414
415 } // namespace lyx
416
417 #endif // BUFFERVIEW_H