]> git.lyx.org Git - features.git/blob - src/BufferView.h
Polish the Toc and labels updating when loading a child document.
[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 "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         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         /// load a buffer into the view.
97   /// returns the buffer or 0 if not loaded
98         Buffer * loadLyXFile(support::FileName const & name, bool auto_open = false);
99
100         /// perform pending metrics updates.
101         /** \c Update::FitCursor means first to do a FitCursor, and to
102          * force an update if screen position changes.
103          * \c Update::Force means to force an update in any case.
104          * \retval true if a screen redraw is needed
105          */
106         bool update(Update::flags flags = Update::FitCursor | Update::Force);
107
108         /// move the screen to fit the cursor.
109         /// Only to be called with good y coordinates (after a bv::metrics)
110         bool fitCursor();
111         /// reset the scrollbar to reflect current view position.
112         void updateScrollbar();
113         /// return the Scrollbar Parameters.
114         ScrollbarParameters const & scrollbarParameters() const;
115
116         /// Save the current position as bookmark.
117         /// if idx == 0, save to temp_bookmark
118         void saveBookmark(unsigned int idx);
119         /// goto a specified position, try top_id first, and then bottom_pit
120         /// return the bottom_pit and top_id of the new paragraph
121         boost::tuple<pit_type, pos_type, int> moveToPosition(
122                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
123                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
124                 int top_id, ///< Paragraph ID, \sa Paragraph
125                 pos_type top_pos ///< Position in the \c Paragraph
126                 );
127         /// return the current change at the cursor.
128         Change const getCurrentChange() const;
129
130         /// move cursor to the named label.
131         void gotoLabel(docstring const & label);
132
133         /// set the cursor based on the given TeX source row.
134         void setCursorFromRow(int row);
135
136         /// center the document view around the cursor.
137         void center();
138         /// scroll document by the given number of lines of default height.
139         void scroll(int lines);
140         /// Scroll the view by a number of pixels.
141         void scrollDocView(int pixels);
142         /// Set the cursor position based on the scrollbar one.
143         void setCursorFromScrollbar();
144
145         /// return the pixel width of the document view.
146         int workWidth() const;
147         /// return the pixel height of the document view.
148         int workHeight() const;
149
150         /// translate and insert a character, using the correct keymap.
151         void translateAndInsert(char_type c, Text * t, Cursor & cur);
152
153         /// return true for events that will handle.
154         FuncStatus getStatus(FuncRequest const & cmd);
155         /// execute the given function.
156         /// \return the Update::flags for further metrics update.
157         Update::flags dispatch(FuncRequest const & argument);
158
159         /// request an X11 selection.
160         /// \return the selected string.
161         docstring const requestSelection();
162         /// clear the X11 selection.
163         void clearSelection();
164
165         /// resize method helper for \c WorkArea
166         /// \sa WorkArea
167         /// \sa resise
168         void workAreaResize(int width, int height);
169
170         /// dispatch method helper for \c WorkArea
171         /// \sa WorkArea
172         /// \retval true if a redraw is needed
173         bool workAreaDispatch(FuncRequest const & ev);
174
175         /// access to anchor.
176         pit_type anchor_ref() const;
177
178         /// access to full cursor.
179         Cursor & cursor();
180         /// access to full cursor.
181         Cursor const & cursor() const;
182         /// sets cursor.
183         /// This will also open all relevant collapsable insets.
184         void setCursor(DocIterator const &);
185         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
186         /// \retval true if an update was needed.
187         bool checkDepm(Cursor & cur, Cursor & old);
188         /// sets cursor.
189         /// This is used when handling LFUN_MOUSE_PRESS.
190         bool mouseSetCursor(Cursor & cur);
191
192         /// sets the selection.
193         /* When \c backwards == false, set anchor
194          * to \c cur and cursor to \c cur + \c length. When \c
195          * backwards == true, set anchor to \c cur and cursor to \c
196          * cur + \c length.
197          */
198         void putSelectionAt(DocIterator const & cur,
199                 int length, bool backwards);
200
201         /// return the internal \c ViewMetricsInfo.
202         /// This is used specifically by the \c Workrea.
203         /// \sa WorkArea
204         /// \sa ViewMetricsInfo
205         ViewMetricsInfo const & viewMetricsInfo();
206         /// update the internal \c ViewMetricsInfo.
207         /// \param singlepar indicates wether
208         void updateMetrics(bool singlepar = false);
209
210         ///
211         TextMetrics const & textMetrics(Text const * t) const;
212         TextMetrics & textMetrics(Text const * t);
213         ///
214         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
215
216         ///
217         CoordCache & coordCache() {
218                 return coord_cache_;
219         }
220         ///
221         CoordCache const & coordCache() const {
222                 return coord_cache_;
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(std::string layout)> layoutChanged;
250
251 private:
252         ///
253         bool multiParSel();
254
255         /// Search recursively for the the innermost inset that covers (x, y) position.
256         /// \retval 0 if no inset is found.
257         Inset const * getCoveringInset(
258                 Text const & text, //< The Text where we start searching.
259                 int x, //< x-coordinate on screen
260                 int y  //< y-coordinate on screen
261                 );
262
263         ///
264         int width_;
265         ///
266         int height_;
267         ///
268         ScrollbarParameters scrollbarParameters_;
269
270         ///
271         ViewMetricsInfo metrics_info_;
272         ///
273         CoordCache coord_cache_;
274         ///
275         Buffer * buffer_;
276
277         /// Estimated average par height for scrollbar.
278         int wh_;
279         ///
280         void menuInsertLyXFile(std::string const & filen);
281
282         /// this is used to handle XSelection events in the right manner.
283         struct {
284                 CursorSlice cursor;
285                 CursorSlice anchor;
286                 bool set;
287         } xsel_cache_;
288         ///
289         Cursor cursor_;
290         ///
291         bool multiparsel_cache_;
292         ///
293         pit_type anchor_ref_;
294         ///
295         int offset_ref_;
296
297         /// keyboard mapping object.
298         boost::scoped_ptr<Intl> const intl_;
299
300         /// last visited inset (kept to send setMouseHover(false) )
301         Inset * last_inset_;
302
303         /// A map from a Text to the associated text metrics
304         typedef std::map<Text const *, TextMetrics> TextMetricsCache;
305         mutable TextMetricsCache text_metrics_;
306 };
307
308
309 } // namespace lyx
310
311 #endif // BUFFERVIEW_H