]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
5ef1af2db148ff51a77a68a01b9fa011a12627d1
[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 "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 Cursor;
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         /// load a buffer into the view.
97         bool loadLyXFile(support::FileName const & name, bool tolastfiles = true);
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 the bottom_pit and top_id of the new paragraph
120         boost::tuple<pit_type, pos_type, int> 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 document by the given number of lines of default height.
138         void scroll(int lines);
139         /// Scroll the view by a number of pixels.
140         void scrollDocView(int pixels);
141         /// Set the cursor position based on the scrollbar one.
142         void setCursorFromScrollbar();
143
144         /// return the pixel width of the document view.
145         int workWidth() const;
146         /// return the pixel height of the document view.
147         int workHeight() const;
148
149         /// switch between primary and secondary keymaps for RTL entry.
150         void switchKeyMap();
151
152         /// return true for events that will handle.
153         FuncStatus getStatus(FuncRequest const & cmd);
154         /// execute the given function.
155         /// \return the Update::flags for further metrics update.
156         Update::flags dispatch(FuncRequest const & argument);
157
158         /// request an X11 selection.
159         /// \return the selected string.
160         docstring const requestSelection();
161         /// clear the X11 selection.
162         void clearSelection();
163
164         /// resize method helper for \c WorkArea
165         /// \sa WorkArea
166         /// \sa resise
167         void workAreaResize(int width, int height);
168
169         /// dispatch method helper for \c WorkArea
170         /// \sa WorkArea
171         /// \retval true if a redraw is needed
172         bool workAreaDispatch(FuncRequest const & ev);
173
174         /// access to anchor.
175         pit_type anchor_ref() const;
176
177         /// access to full cursor.
178         Cursor & cursor();
179         /// access to full cursor.
180         Cursor const & cursor() const;
181         /// sets cursor.
182         /// This will also open all relevant collapsable insets.
183         void setCursor(DocIterator const &);
184         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
185         /// \retval true if an update was needed.
186         bool checkDepm(Cursor & cur, Cursor & old);
187         /// sets cursor.
188         /// This is used when handling LFUN_MOUSE_PRESS.
189         bool mouseSetCursor(Cursor & cur);
190
191         /// sets the selection.
192         /* When \c backwards == false, set anchor
193          * to \c cur and cursor to \c cur + \c length. When \c
194          * backwards == true, set anchor to \c cur and cursor to \c
195          * cur + \c length.
196          */
197         void putSelectionAt(DocIterator const & cur,
198                 int length, bool backwards);
199
200         /// return the internal \c ViewMetricsInfo.
201         /// This is used specifically by the \c Workrea.
202         /// \sa WorkArea
203         /// \sa ViewMetricsInfo
204         ViewMetricsInfo const & viewMetricsInfo();
205         /// update the internal \c ViewMetricsInfo.
206         /// \param singlepar indicates wether
207         void updateMetrics(bool singlepar = false);
208
209         ///
210         TextMetrics const & textMetrics(LyXText const * t) const;
211         TextMetrics & textMetrics(LyXText const * t);
212         ///
213         ParagraphMetrics const & parMetrics(LyXText const *, pit_type) const;
214
215         ///
216         CoordCache & coordCache() {
217                 return coord_cache_;
218         }
219         ///
220         CoordCache const & coordCache() const {
221                 return coord_cache_;
222         }
223         /// get this view's keyboard map handler.
224         Intl & getIntl() { return *intl_.get(); }
225         ///
226         Intl const & getIntl() const { return *intl_.get(); }
227
228         /// This signal is emitted when some message shows up.
229         boost::signal<void(docstring)> message;
230
231         /// This signal is emitted when some dialog needs to be shown.
232         boost::signal<void(std::string name)> showDialog;
233
234         /// This signal is emitted when some dialog needs to be shown with
235         /// some data.
236         boost::signal<void(std::string name,
237                 std::string data)> showDialogWithData;
238
239         /// This signal is emitted when some inset dialogs needs to be shown.
240         boost::signal<void(std::string name, std::string data,
241                 InsetBase * inset)> showInsetDialog;
242
243         /// This signal is emitted when some dialogs needs to be updated.
244         boost::signal<void(std::string name,
245                 std::string data)> updateDialog;
246
247         /// This signal is emitted when the layout at the cursor is changed.
248         boost::signal<void(std::string layout)> layoutChanged;
249
250 private:
251         ///
252         bool multiParSel();
253
254         /// Search recursively for the the innermost inset that covers (x, y) position.
255         /// \retval 0 if no inset is found.
256         InsetBase const * getCoveringInset(
257                 LyXText const & text, //< The LyXText where we start searching.
258                 int x, //< x-coordinate on screen
259                 int y  //< y-coordinate on screen
260                 );
261
262         ///
263         int width_;
264         ///
265         int height_;
266         ///
267         ScrollbarParameters scrollbarParameters_;
268
269         ///
270         ViewMetricsInfo metrics_info_;
271         ///
272         CoordCache coord_cache_;
273         ///
274         Buffer * buffer_;
275
276         /// Estimated average par height for scrollbar.
277         int wh_;
278         ///
279         void menuInsertLyXFile(std::string const & filen);
280
281         /// this is used to handle XSelection events in the right manner.
282         struct {
283                 CursorSlice cursor;
284                 CursorSlice anchor;
285                 bool set;
286         } xsel_cache_;
287         ///
288         Cursor cursor_;
289         ///
290         bool multiparsel_cache_;
291         ///
292         pit_type anchor_ref_;
293         ///
294         int offset_ref_;
295
296         /// keyboard mapping object.
297         boost::scoped_ptr<Intl> const intl_;
298
299         /// last visited inset (kept to send setMouseHover(false) )
300         InsetBase * last_inset_;
301
302         /// A map from a LyXText to the associated text metrics
303         typedef std::map<LyXText const *, TextMetrics> TextMetricsCache;
304         mutable TextMetricsCache text_metrics_;
305 };
306
307
308 } // namespace lyx
309
310 #endif // BUFFERVIEW_H