]> git.lyx.org Git - features.git/blob - src/BufferView.h
Fix bug 3092, use both pit and id to keep track of bookmarks.
[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 "coordcache.h"
19 #include "cursor.h"
20 #include "metricsinfo.h"
21 #include "TextMetrics.h"
22 #include "UpdateFlags.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 LCursor;
45 class LyXText;
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         BufferView();
83
84         ~BufferView();
85
86         /// set the buffer we are viewing.
87         /// \todo FIXME: eventually, we will create a new BufferView
88         /// when switching Buffers, so this method should go.
89         void setBuffer(Buffer * b);
90         /// return the buffer being viewed.
91         Buffer * buffer() const;
92
93         /// resize the BufferView.
94         void resize();
95
96         /// redisplay the referenced buffer.
97         void reload();
98         /// load a buffer into the view.
99         bool loadLyXFile(support::FileName const & name, bool tolastfiles = true);
100
101         /// perform pending metrics updates.
102         /** \c Update::FitCursor means first to do a FitCursor, and to
103          * force an update if screen position changes.
104          * \c Update::Force means to force an update in any case.
105          * \retval true if a screen redraw is needed
106          */
107         bool update(Update::flags flags = Update::FitCursor | Update::Force);
108
109         /// move the screen to fit the cursor.
110         /// Only to be called with good y coordinates (after a bv::metrics)
111         bool fitCursor();
112         /// reset the scrollbar to reflect current view position.
113         void updateScrollbar();
114         /// return the Scrollbar Parameters.
115         ScrollbarParameters const & scrollbarParameters() const;
116
117         /// Save the current position as bookmark.
118         /// if persistent=false, save to temp_bookmark
119         void saveBookmark(bool persistent);
120         /// goto a specified position, try par_id first, and then par_pit
121         /// return the par_pit and par_id of the new paragraph
122         boost::tuple<pit_type, int> moveToPosition(
123                 pit_type par_pit, ///< Paragraph pit, used when par_id is zero or invalid.
124                 int par_id, ///< Paragraph ID, \sa Paragraph
125                 pos_type par_pos ///< Position in the \c Paragraph
126                 );
127         /// return the current change at the cursor.
128         Change const getCurrentChange() const;
129
130         /// return the lyxtext we are using.
131         LyXText * getLyXText();
132
133         /// return the lyxtext we are using.
134         LyXText const * getLyXText() const;
135
136         /// move cursor to the named label.
137         void gotoLabel(docstring const & label);
138
139         /// set the cursor based on the given TeX source row.
140         void setCursorFromRow(int row);
141
142         /// center the document view around the cursor.
143         void center();
144         /// scroll document by the given number of lines of default height.
145         void scroll(int lines);
146         /// Scroll the view by a number of pixels.
147         void scrollDocView(int pixels);
148         /// Set the cursor position based on the scrollbar one.
149         void setCursorFromScrollbar();
150
151         /// return the pixel width of the document view.
152         int workWidth() const;
153         /// return the pixel height of the document view.
154         int workHeight() const;
155
156         /// switch between primary and secondary keymaps for RTL entry.
157         void switchKeyMap();
158
159         /// return true for events that will handle.
160         FuncStatus getStatus(FuncRequest const & cmd);
161         /// execute the given function.
162         bool dispatch(FuncRequest const & argument);
163
164         /// request an X11 selection.
165         /// \return the selected string.
166         docstring const requestSelection();
167         /// clear the X11 selection.
168         void clearSelection();
169
170         /// resize method helper for \c WorkArea
171         /// \sa WorkArea
172         /// \sa resise
173         void workAreaResize(int width, int height);
174
175         /// dispatch method helper for \c WorkArea
176         /// \sa WorkArea
177         /// \retval true if a redraw is needed
178         bool workAreaDispatch(FuncRequest const & ev);
179
180         /// access to anchor.
181         pit_type anchor_ref() const;
182
183         /// access to full cursor.
184         LCursor & cursor();
185         /// access to full cursor.
186         LCursor const & cursor() const;
187         /// sets cursor.
188         /// This will also open all relevant collapsable insets.
189         void setCursor(DocIterator const &);
190         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
191         /// \retval true if an update was needed.
192         bool checkDepm(LCursor & cur, LCursor & old);
193         /// sets cursor.
194         /// This is used when handling LFUN_MOUSE_PRESS.
195         bool mouseSetCursor(LCursor & cur);
196
197         /// sets the selection.
198         /* When \c backwards == false, set anchor
199          * to \c cur and cursor to \c cur + \c length. When \c
200          * backwards == true, set anchor to \c cur and cursor to \c
201          * cur + \c length.
202          */
203         void putSelectionAt(DocIterator const & cur,
204                 int length, bool backwards);
205
206         /// return the internal \c ViewMetricsInfo.
207         /// This is used specifically by the \c Workrea.
208         /// \sa WorkArea
209         /// \sa ViewMetricsInfo
210         ViewMetricsInfo const & viewMetricsInfo();
211         /// update the internal \c ViewMetricsInfo.
212         /// \param singlepar indicates wether
213         void updateMetrics(bool singlepar = false);
214
215         ///
216         TextMetrics const & textMetrics(LyXText const * t) const;
217         TextMetrics & textMetrics(LyXText const * t);
218         ///
219         ParagraphMetrics const & parMetrics(LyXText const *, pit_type) const;
220
221         ///
222         CoordCache & coordCache() {
223                 return coord_cache_;
224         }
225         ///
226         CoordCache const & coordCache() const {
227                 return coord_cache_;
228         }
229         /// get this view's keyboard map handler.
230         Intl & getIntl() { return *intl_.get(); }
231         ///
232         Intl const & getIntl() const { return *intl_.get(); }
233
234         /// This signal is emitted when some message shows up.
235         boost::signal<void(docstring)> message;
236
237         /// This signal is emitted when some dialog needs to be shown.
238         boost::signal<void(std::string name)> showDialog;
239
240         /// This signal is emitted when some dialog needs to be shown with
241         /// some data.
242         boost::signal<void(std::string name,
243                 std::string data)> showDialogWithData;
244
245         /// This signal is emitted when some inset dialogs needs to be shown.
246         boost::signal<void(std::string name, std::string data,
247                 InsetBase * inset)> showInsetDialog;
248
249         /// This signal is emitted when some dialogs needs to be updated.
250         boost::signal<void(std::string name,
251                 std::string data)> updateDialog;
252
253         /// This signal is emitted when the layout at the cursor is changed.
254         boost::signal<void(std::string layout)> layoutChanged;
255
256 private:
257         ///
258         bool multiParSel();
259         ///
260         int width_;
261         ///
262         int height_;
263         ///
264         ScrollbarParameters scrollbarParameters_;
265
266         ///
267         ViewMetricsInfo metrics_info_;
268         ///
269         CoordCache coord_cache_;
270         ///
271         Buffer * buffer_;
272
273         /// Estimated average par height for scrollbar.
274         int wh_;
275         ///
276         void menuInsertLyXFile(std::string const & filen);
277
278         /// this is used to handle XSelection events in the right manner.
279         struct {
280                 CursorSlice cursor;
281                 CursorSlice anchor;
282                 bool set;
283         } xsel_cache_;
284         ///
285         LCursor cursor_;
286         ///
287         bool multiparsel_cache_;
288         ///
289         pit_type anchor_ref_;
290         ///
291         int offset_ref_;
292
293         /// keyboard mapping object.
294         boost::scoped_ptr<Intl> const intl_;
295
296         /// last visited inset (kept to send setMouseHover(false) )
297         InsetBase * last_inset_;
298
299         /// A map from a LyXText to the associated text metrics
300         typedef std::map<LyXText const *, TextMetrics> TextMetricsCache;
301         mutable TextMetricsCache text_metrics_;
302 };
303
304
305 } // namespace lyx
306
307 #endif // BUFFERVIEW_H