]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
boost/utility -> boost/noncopyable
[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 "CoordCache.h"
19 #include "Cursor.h"
20 #include "MetricsInfo.h"
21 #include "TextMetrics.h"
22 #include "update_flags.h"
23
24 #include "support/types.h"
25
26 #include <boost/tuple/tuple.hpp>
27 #include <boost/noncopyable.hpp>
28 #include <boost/signal.hpp>
29
30 #include <utility>
31 #include <string>
32
33
34 namespace lyx {
35
36 namespace support { class FileName; }
37
38 namespace frontend { class Painter; }
39
40 class Buffer;
41 class Change;
42 class DocIterator;
43 class FuncRequest;
44 class FuncStatus;
45 class Intl;
46 class Cursor;
47 class Text;
48 class ParIterator;
49 class ParagraphMetrics;
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         bool update(Update::flags flags = Update::FitCursor | Update::Force);
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         /// \retval true if a redraw is needed
175         bool workAreaDispatch(FuncRequest const & ev);
176
177         /// access to anchor.
178         pit_type anchor_ref() const;
179
180         ///
181         CursorStatus cursorStatus(DocIterator const & dit) const;
182         /// access to full cursor.
183         Cursor & cursor();
184         /// access to full cursor.
185         Cursor const & cursor() const;
186         /// sets cursor.
187         /// This will also open all relevant collapsable insets.
188         void setCursor(DocIterator const &);
189         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
190         /// \retval true if an update was needed.
191         bool checkDepm(Cursor & cur, Cursor & old);
192         /// sets cursor.
193         /// This is used when handling LFUN_MOUSE_PRESS.
194         bool mouseSetCursor(Cursor & cur);
195
196         /// sets the selection.
197         /* When \c backwards == false, set anchor
198          * to \c cur and cursor to \c cur + \c length. When \c
199          * backwards == true, set anchor to \c cur and cursor to \c
200          * cur + \c length.
201          */
202         void putSelectionAt(DocIterator const & cur,
203                 int length, bool backwards);
204
205         /// return the internal \c ViewMetricsInfo.
206         /// This is used specifically by the \c Workrea.
207         /// \sa WorkArea
208         /// \sa ViewMetricsInfo
209         ViewMetricsInfo const & viewMetricsInfo();
210         /// update the internal \c ViewMetricsInfo.
211         /// \param singlepar indicates wether
212         void updateMetrics(bool singlepar = false);
213
214         ///
215         TextMetrics const & textMetrics(Text const * t) const;
216         TextMetrics & textMetrics(Text const * t);
217         ///
218         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
219
220         ///
221         CoordCache & coordCache() { return coord_cache_; }
222         ///
223         CoordCache const & coordCache() const { return coord_cache_; }
224
225         ///
226         Point getPos(DocIterator const & dit, bool boundary) const;
227
228
229         ///
230         void draw(frontend::Painter & pain);
231
232         /// get this view's keyboard map handler.
233         Intl & getIntl() { return *intl_.get(); }
234         ///
235         Intl const & getIntl() const { return *intl_.get(); }
236
237         /// This signal is emitted when some message shows up.
238         boost::signal<void(docstring)> message;
239
240         /// This signal is emitted when some dialog needs to be shown.
241         boost::signal<void(std::string name)> showDialog;
242
243         /// This signal is emitted when some dialog needs to be shown with
244         /// some data.
245         boost::signal<void(std::string name,
246                 std::string data)> showDialogWithData;
247
248         /// This signal is emitted when some inset dialogs needs to be shown.
249         boost::signal<void(std::string name, std::string data,
250                 Inset * inset)> showInsetDialog;
251
252         /// This signal is emitted when some dialogs needs to be updated.
253         boost::signal<void(std::string name,
254                 std::string data)> updateDialog;
255
256         /// This signal is emitted when the layout at the cursor is changed.
257         boost::signal<void(docstring layout)> layoutChanged;
258
259 private:
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         bool multiParSel();
267
268         /// Search recursively for the the innermost inset that covers (x, y) position.
269         /// \retval 0 if no inset is found.
270         Inset const * getCoveringInset(
271                 Text const & text, //< The Text where we start searching.
272                 int x, //< x-coordinate on screen
273                 int y  //< y-coordinate on screen
274                 );
275
276         ///
277         int width_;
278         ///
279         int height_;
280         ///
281         ScrollbarParameters scrollbarParameters_;
282
283         ///
284         ViewMetricsInfo metrics_info_;
285         ///
286         CoordCache coord_cache_;
287         ///
288         Buffer & buffer_;
289
290         /// Estimated average par height for scrollbar.
291         int wh_;
292         ///
293         void menuInsertLyXFile(std::string const & filen);
294
295         /// this is used to handle XSelection events in the right manner.
296         struct {
297                 CursorSlice cursor;
298                 CursorSlice anchor;
299                 bool set;
300         } xsel_cache_;
301         ///
302         Cursor cursor_;
303         ///
304         bool multiparsel_cache_;
305         ///
306         pit_type anchor_ref_;
307         ///
308         int offset_ref_;
309         ///
310         void updateOffsetRef();
311         ///
312         bool need_centering_;
313
314         /// keyboard mapping object.
315         boost::scoped_ptr<Intl> const intl_;
316
317         /// last visited inset (kept to send setMouseHover(false) )
318         Inset * last_inset_;
319
320         /// A map from a Text to the associated text metrics
321         typedef std::map<Text const *, TextMetrics> TextMetricsCache;
322         mutable TextMetricsCache text_metrics_;
323 };
324
325 /// some space for drawing the 'nested' markers (in pixel)
326 inline int nestMargin() { return 15; }
327
328 /// margin for changebar
329 inline int changebarMargin() { return 12; }
330
331 /// right margin
332 inline int rightMargin() { return 10; }
333
334 } // namespace lyx
335
336 #endif // BUFFERVIEW_H