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