]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
514534739be0504241cbf983d4844f3aac52eea9
[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 painting updates.
99         /** \c fitcursor means first
100          *  to do a fitcursor, and to force an update if screen
101          *  position changes. \c forceupdate means to force an update
102          *  in any case.
103          * \retval (false, xxx) if no redraw is required
104          * \retval (true, true) if a single paragraph redraw is needed
105          * \retval (true, false) if a full redraw is needed
106          */
107         std::pair<bool, 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.
121         void moveToPosition(
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         bool dispatch(FuncRequest const & argument);
161
162         /// request an X11 selection.
163         /// \return the selected string.
164         docstring const requestSelection();
165         /// clear the X11 selection.
166         void clearSelection();
167
168         /// resize method helper for \c WorkArea
169         /// \sa WorkArea
170         /// \sa resise
171         void workAreaResize(int width, int height);
172
173         /// dispatch method helper for \c WorkArea
174         /// \sa WorkArea
175         /// \retval (false, xxx) if no redraw is required
176         /// \retval (true, true) if a single paragraph redraw is needed
177         /// \retval (true, false) if a full redraw is needed
178         std::pair<bool, 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         /// sets cursor.
191         /// This is used when handling LFUN_MOUSE_PRESS.
192         void mouseSetCursor(LCursor & cur);
193
194         /// sets the selection.
195         /* When \c backwards == false, set anchor
196          * to \c cur and cursor to \c cur + \c length. When \c
197          * backwards == true, set anchor to \c cur and cursor to \c
198          * cur + \c length.
199          */
200         void putSelectionAt(DocIterator const & cur,
201                 int length, bool backwards);
202
203         /// return the internal \c ViewMetricsInfo.
204         /// This is used specifically by the \c Workrea.
205         /// \sa WorkArea
206         /// \sa ViewMetricsInfo
207         ViewMetricsInfo const & viewMetricsInfo();
208         /// update the internal \c ViewMetricsInfo.
209         /// \param singlepar indicates wether
210         void updateMetrics(bool singlepar = false);
211
212         ///
213         CoordCache & coordCache() {
214                 return coord_cache_;
215         }
216         ///
217         CoordCache const & coordCache() const {
218                 return coord_cache_;
219         }
220         /// get this view's keyboard map handler.
221         Intl & getIntl() { return *intl_.get(); }
222         ///
223         Intl const & getIntl() const { return *intl_.get(); }
224
225         /// This signal is emitted when some message shows up.
226         boost::signal<void(docstring)> message;
227
228         /// This signal is emitted when some dialog needs to be shown.
229         boost::signal<void(std::string name)> showDialog;
230
231         /// This signal is emitted when some dialog needs to be shown with
232         /// some data.
233         boost::signal<void(std::string name,
234                 std::string data)> showDialogWithData;
235
236         /// This signal is emitted when some inset dialogs needs to be shown.
237         boost::signal<void(std::string name, std::string data,
238                 InsetBase * inset)> showInsetDialog;
239
240         /// This signal is emitted when some dialogs needs to be updated.
241         boost::signal<void(std::string name,
242                 std::string data)> updateDialog;
243
244         /// This signal is emitted when the layout at the cursor is changed.
245         boost::signal<void(std::string layout)> layoutChanged;
246
247 private:
248         ///
249         bool multiParSel();
250         ///
251         int width_;
252         ///
253         int height_;
254         ///
255         ScrollbarParameters scrollbarParameters_;
256
257         ///
258         ViewMetricsInfo metrics_info_;
259         ///
260         CoordCache coord_cache_;
261         ///
262         Buffer * buffer_;
263
264         /// Estimated average par height for scrollbar.
265         int wh_;
266         ///
267         void menuInsertLyXFile(std::string const & filen);
268
269         /// this is used to handle XSelection events in the right manner.
270         struct {
271                 CursorSlice cursor;
272                 CursorSlice anchor;
273                 bool set;
274         } xsel_cache_;
275         ///
276         LCursor cursor_;
277         ///
278         bool multiparsel_cache_;
279         ///
280         pit_type anchor_ref_;
281         ///
282         int offset_ref_;
283
284         /// keyboard mapping object.
285         boost::scoped_ptr<Intl> const intl_;
286 };
287
288
289 } // namespace lyx
290
291 #endif // BUFFERVIEW_H