]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
BufferView.h/C: use member variable last_inset_ instead of static variable in workAre...
[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 "UpdateFlags.h"
22 #include "support/types.h"
23
24 #include <boost/utility.hpp>
25 #include <boost/signal.hpp>
26
27 #include <utility>
28 #include <string>
29
30
31 namespace lyx {
32
33 namespace support { class FileName; }
34
35 class Buffer;
36 class Change;
37 class DocIterator;
38 class FuncRequest;
39 class FuncStatus;
40 class Intl;
41 class Language;
42 class LCursor;
43 class LyXText;
44 class ParIterator;
45 class ViewMetricsInfo;
46
47 /// Scrollbar Parameters.
48 struct ScrollbarParameters
49 {
50         void reset(int h = 0, int p = 0, int l = 0)
51         {
52                 height = h;
53                 position = p;
54                 lineScrollHeight = l;
55         }
56
57         /// Total document height in pixels.
58         int height;
59         /// Current position in the document in pixels.
60         int position;
61         /// Line-scroll amount in pixels.
62         int lineScrollHeight;
63 };
64
65 /// Screen view of a Buffer.
66 /**
67  * A BufferView encapsulates a view onto a particular
68  * buffer, and allows access to operate upon it. A view
69  * is a sliding window of the entire document rendering.
70  * It is the official interface between the LyX core and
71  * the frontend WorkArea.
72  * 
73  * \sa WorkArea
74  * \sa Buffer
75  * \sa CoordCache
76  */
77 class BufferView : boost::noncopyable {
78 public:
79         BufferView();
80
81         ~BufferView();
82
83         /// set the buffer we are viewing.
84         /// \todo FIXME: eventually, we will create a new BufferView
85         /// when switching Buffers, so this method should go.
86         void setBuffer(Buffer * b);
87         /// return the buffer being viewed.
88         Buffer * buffer() const;
89
90         /// resize the BufferView.
91         void resize();
92
93         /// redisplay the referenced buffer.
94         void reload();
95         /// load a buffer into the view.
96         bool loadLyXFile(support::FileName const & name, bool tolastfiles = true);
97
98         /// perform pending metrics updates.
99         /** \c Update::FitCursor means first to do a FitCursor, and to
100          * force an update if screen position changes.
101          * \c Update::Force means to force an update in any case.
102          * \retval true if a screen redraw is needed
103          */
104         bool update(Update::flags flags = Update::FitCursor | Update::Force);
105
106         /// move the screen to fit the cursor.
107         /// Only to be called with good y coordinates (after a bv::metrics)
108         bool fitCursor();
109         /// reset the scrollbar to reflect current view position.
110         void updateScrollbar();
111         /// return the Scrollbar Parameters.
112         ScrollbarParameters const & scrollbarParameters() const;
113
114         /// Save the current position as bookmark.
115         /// if persistent=false, save to temp_bookmark
116         void saveBookmark(bool persistent);
117         /// goto a specified position.
118         void moveToPosition(
119                 int par_id, ///< Paragraph ID, \sa Paragraph
120                 pos_type par_pos ///< Position in the \c Paragraph
121                 );
122         /// return the current change at the cursor.
123         Change const getCurrentChange() const;
124
125         /// return the lyxtext we are using.
126         LyXText * getLyXText();
127
128         /// return the lyxtext we are using.
129         LyXText const * getLyXText() const;
130
131         /// move cursor to the named label.
132         void gotoLabel(docstring const & label);
133
134         /// set the cursor based on the given TeX source row.
135         void setCursorFromRow(int row);
136
137         /// center the document view around the cursor.
138         void center();
139         /// scroll document by the given number of lines of default height.
140         void scroll(int lines);
141         /// Scroll the view by a number of pixels.
142         void scrollDocView(int pixels);
143         /// Set the cursor position based on the scrollbar one.
144         void setCursorFromScrollbar();
145
146         /// return the pixel width of the document view.
147         int workWidth() const;
148         /// return the pixel height of the document view.
149         int workHeight() const;
150
151         /// switch between primary and secondary keymaps for RTL entry.
152         void switchKeyMap();
153
154         /// return true for events that will handle.
155         FuncStatus getStatus(FuncRequest const & cmd);
156         /// execute the given function.
157         bool 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         LCursor & cursor();
180         /// access to full cursor.
181         LCursor const & cursor() const;
182         /// sets cursor.
183         /// This will also open all relevant collapsable insets.
184         void setCursor(DocIterator const &);
185         /// sets cursor.
186         /// This is used when handling LFUN_MOUSE_PRESS.
187         bool mouseSetCursor(LCursor & cur);
188
189         /// sets the selection.
190         /* When \c backwards == false, set anchor
191          * to \c cur and cursor to \c cur + \c length. When \c
192          * backwards == true, set anchor to \c cur and cursor to \c
193          * cur + \c length.
194          */
195         void putSelectionAt(DocIterator const & cur,
196                 int length, bool backwards);
197
198         /// return the internal \c ViewMetricsInfo.
199         /// This is used specifically by the \c Workrea.
200         /// \sa WorkArea
201         /// \sa ViewMetricsInfo
202         ViewMetricsInfo const & viewMetricsInfo();
203         /// update the internal \c ViewMetricsInfo.
204         /// \param singlepar indicates wether
205         void updateMetrics(bool singlepar = false);
206
207         ///
208         CoordCache & coordCache() {
209                 return coord_cache_;
210         }
211         ///
212         CoordCache const & coordCache() const {
213                 return coord_cache_;
214         }
215         /// get this view's keyboard map handler.
216         Intl & getIntl() { return *intl_.get(); }
217         ///
218         Intl const & getIntl() const { return *intl_.get(); }
219
220         /// This signal is emitted when some message shows up.
221         boost::signal<void(docstring)> message;
222
223         /// This signal is emitted when some dialog needs to be shown.
224         boost::signal<void(std::string name)> showDialog;
225
226         /// This signal is emitted when some dialog needs to be shown with
227         /// some data.
228         boost::signal<void(std::string name,
229                 std::string data)> showDialogWithData;
230
231         /// This signal is emitted when some inset dialogs needs to be shown.
232         boost::signal<void(std::string name, std::string data,
233                 InsetBase * inset)> showInsetDialog;
234
235         /// This signal is emitted when some dialogs needs to be updated.
236         boost::signal<void(std::string name,
237                 std::string data)> updateDialog;
238
239         /// This signal is emitted when the layout at the cursor is changed.
240         boost::signal<void(std::string layout)> layoutChanged;
241
242 private:
243         ///
244         bool multiParSel();
245         ///
246         int width_;
247         ///
248         int height_;
249         ///
250         ScrollbarParameters scrollbarParameters_;
251
252         ///
253         ViewMetricsInfo metrics_info_;
254         ///
255         CoordCache coord_cache_;
256         ///
257         Buffer * buffer_;
258
259         /// Estimated average par height for scrollbar.
260         int wh_;
261         ///
262         void menuInsertLyXFile(std::string const & filen);
263
264         /// this is used to handle XSelection events in the right manner.
265         struct {
266                 CursorSlice cursor;
267                 CursorSlice anchor;
268                 bool set;
269         } xsel_cache_;
270         ///
271         LCursor cursor_;
272         ///
273         bool multiparsel_cache_;
274         ///
275         pit_type anchor_ref_;
276         ///
277         int offset_ref_;
278
279         /// keyboard mapping object.
280         boost::scoped_ptr<Intl> const intl_;
281
282         /// last visited inset (kept to send setMouseHover(false)
283         InsetBase * last_inset_;
284 };
285
286
287 } // namespace lyx
288
289 #endif // BUFFERVIEW_H