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