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