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