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