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