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