]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
Move private part of BufferView to a private implementation.
[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 "Dimension.h"
19 #include "update_flags.h"
20
21 #include "support/docstring.h"
22 #include "support/types.h"
23
24 #include <boost/noncopyable.hpp>
25
26 #include <utility>
27 #include <string>
28
29
30 namespace lyx {
31
32 namespace support { class FileName; }
33
34 namespace frontend { class Painter; }
35 namespace frontend { class GuiBufferViewDelegate; }
36
37 class Buffer;
38 class Change;
39 class CoordCache;
40 class Cursor;
41 class DocIterator;
42 class FuncRequest;
43 class FuncStatus;
44 class Intl;
45 class Inset;
46 class ParIterator;
47 class ParagraphMetrics;
48 class Text;
49 class TextMetrics;
50 class ViewMetricsInfo;
51
52 enum CursorStatus {
53         CUR_INSIDE,
54         CUR_ABOVE,
55         CUR_BELOW
56 };
57
58 /// Scrollbar Parameters.
59 struct ScrollbarParameters
60 {
61         void reset(int h = 0, int p = 0, int l = 0)
62         {
63                 height = h;
64                 position = p;
65                 lineScrollHeight = l;
66         }
67
68         /// Total document height in pixels.
69         int height;
70         /// Current position in the document in pixels.
71         int position;
72         /// Line-scroll amount in pixels.
73         int lineScrollHeight;
74 };
75
76 /// Screen view of a Buffer.
77 /**
78  * A BufferView encapsulates a view onto a particular
79  * buffer, and allows access to operate upon it. A view
80  * is a sliding window of the entire document rendering.
81  * It is the official interface between the LyX core and
82  * the frontend WorkArea.
83  *
84  * \sa WorkArea
85  * \sa Buffer
86  * \sa CoordCache
87  */
88 class BufferView : boost::noncopyable {
89 public:
90         ///
91         BufferView(Buffer & buffer);
92         ///
93         ~BufferView();
94
95         /// return the buffer being viewed.
96         Buffer & buffer();
97         Buffer const & buffer() 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
115         /// Save the current position as bookmark.
116         /// if idx == 0, save to temp_bookmark
117         void saveBookmark(unsigned int idx);
118         /// goto a specified position, try top_id first, and then bottom_pit.
119         /// \return true if success
120         bool moveToPosition(
121                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
122                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
123                 int top_id, ///< Paragraph ID, \sa Paragraph
124                 pos_type top_pos ///< Position in the \c Paragraph
125                 );
126         /// return the current change at the cursor.
127         Change const getCurrentChange() const;
128
129         /// move cursor to the named label.
130         void gotoLabel(docstring const & label);
131
132         /// set the cursor based on the given TeX source row.
133         void setCursorFromRow(int row);
134
135         /// center the document view around the cursor.
136         void center();
137         /// scroll down document by the given number of pixels.
138         void scrollDown(int pixels);
139         /// scroll up document by the given number of pixels.
140         void scrollUp(int pixels);
141         /// scroll document by the given number of pixels.
142         void scroll(int pixels);
143         /// Scroll the view by a number of pixels.
144         void scrollDocView(int pixels);
145         /// Set the cursor position based on the scrollbar one.
146         void setCursorFromScrollbar();
147
148         /// return the pixel width of the document view.
149         int workWidth() const;
150         /// return the pixel height of the document view.
151         int workHeight() const;
152
153         /// translate and insert a character, using the correct keymap.
154         void translateAndInsert(char_type c, Text * t, Cursor & cur);
155
156         /// return true for events that will handle.
157         FuncStatus getStatus(FuncRequest const & cmd);
158         /// execute the given function.
159         /// \return the Update::flags for further metrics update.
160         Update::flags dispatch(FuncRequest const & argument);
161
162         /// request an X11 selection.
163         /// \return the selected string.
164         docstring const requestSelection();
165         /// clear the X11 selection.
166         void clearSelection();
167
168         /// resize the BufferView.
169         /// \sa WorkArea
170         void resize(int width, int height);
171
172         /// dispatch method helper for \c WorkArea
173         /// \sa WorkArea
174         void mouseEventDispatch(FuncRequest const & ev);
175
176         /// access to anchor.
177         pit_type anchor_ref() const;
178
179         ///
180         CursorStatus cursorStatus(DocIterator const & dit) const;
181         /// access to full cursor.
182         Cursor & cursor();
183         /// access to full cursor.
184         Cursor const & cursor() const;
185         /// sets cursor.
186         /// This will also open all relevant collapsable insets.
187         void setCursor(DocIterator const &);
188         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
189         /// \retval true if an update was needed.
190         bool checkDepm(Cursor & cur, Cursor & old);
191         /// sets cursor.
192         /// This is used when handling LFUN_MOUSE_PRESS.
193         bool mouseSetCursor(Cursor & cur);
194
195         /// sets the selection.
196         /* When \c backwards == false, set anchor
197          * to \c cur and cursor to \c cur + \c length. When \c
198          * backwards == true, set anchor to \c cur and cursor to \c
199          * cur + \c length.
200          */
201         void putSelectionAt(DocIterator const & cur,
202                 int length, bool backwards);
203
204         /// return the internal \c ViewMetricsInfo.
205         /// This is used specifically by the \c Workrea.
206         /// \sa WorkArea
207         /// \sa ViewMetricsInfo
208         ViewMetricsInfo const & viewMetricsInfo();
209         /// update the internal \c ViewMetricsInfo.
210         /// \param singlepar indicates wether
211         void updateMetrics(bool singlepar = false);
212
213         ///
214         TextMetrics const & textMetrics(Text const * t) const;
215         TextMetrics & textMetrics(Text const * t);
216         ///
217         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
218
219         ///
220         CoordCache & coordCache();
221         ///
222         CoordCache const & coordCache() const;
223
224         ///
225         Point getPos(DocIterator const & dit, bool boundary) const;
226
227
228         ///
229         void draw(frontend::Painter & pain);
230
231         /// get this view's keyboard map handler.
232         Intl & getIntl();
233         ///
234         Intl const & getIntl() const;
235
236         //
237         // Messages to the GUI
238         //
239         /// This signal is emitted when some message shows up.
240         void message(docstring const & msg);
241
242         /// This signal is emitted when some dialog needs to be shown.
243         void showDialog(std::string const & name);
244
245         /// This signal is emitted when some dialog needs to be shown with
246         /// some data.
247         void showDialogWithData(std::string const & name, std::string const & data);
248
249         /// This signal is emitted when some inset dialogs needs to be shown.
250         void showInsetDialog(std::string const & name, std::string const & data,
251                 Inset * inset);
252
253         /// This signal is emitted when some dialogs needs to be updated.
254         void updateDialog(std::string const & name, std::string const & data);
255
256         ///
257         void setGuiDelegate(frontend::GuiBufferViewDelegate *);
258
259         ///
260         docstring contentsOfPlaintextFile(std::string const & f, bool asParagraph);
261         // Insert plain text file (if filename is empty, prompt for one)
262         void insertPlaintextFile(std::string const & fileName, bool asParagraph);
263
264 private:
265         // the position relative to (0, baseline) of outermost paragraph
266         Point coordOffset(DocIterator const & dit, bool boundary) const;
267         /// Update current paragraph metrics.
268         /// \return true if no further update is needed.
269         bool singleParUpdate();
270         ///
271         bool multiParSel();
272
273         /// Search recursively for the the innermost inset that covers (x, y) position.
274         /// \retval 0 if no inset is found.
275         Inset const * getCoveringInset(
276                 Text const & text, //< The Text where we start searching.
277                 int x, //< x-coordinate on screen
278                 int y  //< y-coordinate on screen
279                 );
280
281         ///
282         int width_;
283         ///
284         int height_;
285         ///
286         Buffer & buffer_;
287
288         ///
289         void menuInsertLyXFile(std::string const & filen);
290
291         void updateOffsetRef();
292
293         struct BufferViewPrivate;
294         BufferViewPrivate & d;
295 };
296
297 /// some space for drawing the 'nested' markers (in pixel)
298 inline int nestMargin() { return 15; }
299
300 /// margin for changebar
301 inline int changebarMargin() { return 12; }
302
303 /// right margin
304 inline int rightMargin() { return 10; }
305
306 } // namespace lyx
307
308 #endif // BUFFERVIEW_H