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