]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
* BufferView::updateMetrics(): split up the method in two for the SinglePar case.
[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/utility.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 /// Scrollbar Parameters.
53 struct ScrollbarParameters
54 {
55         void reset(int h = 0, int p = 0, int l = 0)
56         {
57                 height = h;
58                 position = p;
59                 lineScrollHeight = l;
60         }
61
62         /// Total document height in pixels.
63         int height;
64         /// Current position in the document in pixels.
65         int position;
66         /// Line-scroll amount in pixels.
67         int lineScrollHeight;
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 : boost::noncopyable {
83 public:
84         ///
85         BufferView(Buffer & buffer);
86
87         ~BufferView();
88
89         /// return the buffer being viewed.
90         Buffer & buffer();
91         Buffer const & buffer() const;
92
93         /// perform pending metrics updates.
94         /** \c Update::FitCursor means first to do a FitCursor, and to
95          * force an update if screen position changes.
96          * \c Update::Force means to force an update in any case.
97          * \retval true if a screen redraw is needed
98          */
99         bool update(Update::flags flags = Update::FitCursor | Update::Force);
100
101         /// move the screen to fit the cursor.
102         /// Only to be called with good y coordinates (after a bv::metrics)
103         bool fitCursor();
104         /// reset the scrollbar to reflect current view position.
105         void updateScrollbar();
106         /// return the Scrollbar Parameters.
107         ScrollbarParameters const & scrollbarParameters() const;
108
109         /// Save the current position as bookmark.
110         /// if idx == 0, save to temp_bookmark
111         void saveBookmark(unsigned int idx);
112         /// goto a specified position, try top_id first, and then bottom_pit.
113         /// \return true if success
114         bool moveToPosition(
115                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
116                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
117                 int top_id, ///< Paragraph ID, \sa Paragraph
118                 pos_type top_pos ///< Position in the \c Paragraph
119                 );
120         /// return the current change at the cursor.
121         Change const getCurrentChange() const;
122
123         /// move cursor to the named label.
124         void gotoLabel(docstring const & label);
125
126         /// set the cursor based on the given TeX source row.
127         void setCursorFromRow(int row);
128
129         /// center the document view around the cursor.
130         void center();
131         /// scroll down document by the given number of pixels.
132         void scrollDown(int pixels);
133         /// scroll up document by the given number of pixels.
134         void scrollUp(int pixels);
135         /// scroll document by the given number of pixels.
136         void scroll(int pixels);
137         /// Scroll the view by a number of pixels.
138         void scrollDocView(int pixels);
139         /// Set the cursor position based on the scrollbar one.
140         void setCursorFromScrollbar();
141
142         /// return the pixel width of the document view.
143         int workWidth() const;
144         /// return the pixel height of the document view.
145         int workHeight() const;
146
147         /// translate and insert a character, using the correct keymap.
148         void translateAndInsert(char_type c, Text * t, Cursor & cur);
149
150         /// return true for events that will handle.
151         FuncStatus getStatus(FuncRequest const & cmd);
152         /// execute the given function.
153         /// \return the Update::flags for further metrics update.
154         Update::flags dispatch(FuncRequest const & argument);
155
156         /// request an X11 selection.
157         /// \return the selected string.
158         docstring const requestSelection();
159         /// clear the X11 selection.
160         void clearSelection();
161
162         /// resize the BufferView.
163         /// \sa WorkArea
164         void resize(int width, int height);
165
166         /// dispatch method helper for \c WorkArea
167         /// \sa WorkArea
168         /// \retval true if a redraw is needed
169         bool workAreaDispatch(FuncRequest const & ev);
170
171         /// access to anchor.
172         pit_type anchor_ref() const;
173
174         /// access to full cursor.
175         Cursor & cursor();
176         /// access to full cursor.
177         Cursor const & cursor() const;
178         /// sets cursor.
179         /// This will also open all relevant collapsable insets.
180         void setCursor(DocIterator const &);
181         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
182         /// \retval true if an update was needed.
183         bool checkDepm(Cursor & cur, Cursor & old);
184         /// sets cursor.
185         /// This is used when handling LFUN_MOUSE_PRESS.
186         bool mouseSetCursor(Cursor & cur);
187
188         /// sets the selection.
189         /* When \c backwards == false, set anchor
190          * to \c cur and cursor to \c cur + \c length. When \c
191          * backwards == true, set anchor to \c cur and cursor to \c
192          * cur + \c length.
193          */
194         void putSelectionAt(DocIterator const & cur,
195                 int length, bool backwards);
196
197         /// return the internal \c ViewMetricsInfo.
198         /// This is used specifically by the \c Workrea.
199         /// \sa WorkArea
200         /// \sa ViewMetricsInfo
201         ViewMetricsInfo const & viewMetricsInfo();
202         /// update the internal \c ViewMetricsInfo.
203         /// \param singlepar indicates wether
204         void updateMetrics(bool singlepar = false);
205
206         ///
207         TextMetrics const & textMetrics(Text const * t) const;
208         TextMetrics & textMetrics(Text const * t);
209         ///
210         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
211
212         ///
213         CoordCache & coordCache() {
214                 return coord_cache_;
215         }
216         ///
217         CoordCache const & coordCache() const {
218                 return coord_cache_;
219         }
220
221         ///
222         void draw(frontend::Painter & pain);
223
224         /// get this view's keyboard map handler.
225         Intl & getIntl() { return *intl_.get(); }
226         ///
227         Intl const & getIntl() const { return *intl_.get(); }
228
229         /// This signal is emitted when some message shows up.
230         boost::signal<void(docstring)> message;
231
232         /// This signal is emitted when some dialog needs to be shown.
233         boost::signal<void(std::string name)> showDialog;
234
235         /// This signal is emitted when some dialog needs to be shown with
236         /// some data.
237         boost::signal<void(std::string name,
238                 std::string data)> showDialogWithData;
239
240         /// This signal is emitted when some inset dialogs needs to be shown.
241         boost::signal<void(std::string name, std::string data,
242                 Inset * inset)> showInsetDialog;
243
244         /// This signal is emitted when some dialogs needs to be updated.
245         boost::signal<void(std::string name,
246                 std::string data)> updateDialog;
247
248         /// This signal is emitted when the layout at the cursor is changed.
249         boost::signal<void(docstring layout)> layoutChanged;
250
251 private:
252         /// Update current paragraph metrics.
253         /// \return true if no further update is needed.
254         bool singleParUpdate();
255         ///
256         bool multiParSel();
257
258         /// Search recursively for the the innermost inset that covers (x, y) position.
259         /// \retval 0 if no inset is found.
260         Inset const * getCoveringInset(
261                 Text const & text, //< The Text where we start searching.
262                 int x, //< x-coordinate on screen
263                 int y  //< y-coordinate on screen
264                 );
265
266         ///
267         int width_;
268         ///
269         int height_;
270         ///
271         ScrollbarParameters scrollbarParameters_;
272
273         ///
274         ViewMetricsInfo metrics_info_;
275         ///
276         CoordCache coord_cache_;
277         ///
278         Buffer & buffer_;
279
280         /// Estimated average par height for scrollbar.
281         int wh_;
282         ///
283         void menuInsertLyXFile(std::string const & filen);
284
285         /// this is used to handle XSelection events in the right manner.
286         struct {
287                 CursorSlice cursor;
288                 CursorSlice anchor;
289                 bool set;
290         } xsel_cache_;
291         ///
292         Cursor cursor_;
293         ///
294         bool multiparsel_cache_;
295         ///
296         pit_type anchor_ref_;
297         ///
298         int offset_ref_;
299         ///
300         void updateOffsetRef();
301         ///
302         bool need_centering_;
303
304         /// keyboard mapping object.
305         boost::scoped_ptr<Intl> const intl_;
306
307         /// last visited inset (kept to send setMouseHover(false) )
308         Inset * last_inset_;
309
310         /// A map from a Text to the associated text metrics
311         typedef std::map<Text const *, TextMetrics> TextMetricsCache;
312         mutable TextMetricsCache text_metrics_;
313 };
314
315 /// some space for drawing the 'nested' markers (in pixel)
316 inline int nestMargin() { return 15; }
317
318 /// margin for changebar
319 inline int changebarMargin() { return 12; }
320
321 /// right margin
322 inline int rightMargin() { return 10; }
323
324 } // namespace lyx
325
326 #endif // BUFFERVIEW_H