]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
7042440fdb6f3f2bcae67b98c469542853f4015d
[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 <string>
28
29
30 namespace lyx {
31
32 class Buffer;
33 class Change;
34 class DocIterator;
35 class FuncRequest;
36 class FuncStatus;
37 class Intl;
38 class Language;
39 class LCursor;
40 class LyXText;
41 class ParIterator;
42 class ViewMetricsInfo;
43
44 /// Scrollbar Parameters.
45 struct ScrollbarParameters
46 {
47         void reset(int h = 0, int p = 0, int l = 0)
48         {
49                 height = h;
50                 position = p;
51                 lineScrollHeight = l;
52         }
53
54         /// Total document height in pixels.
55         int height;
56         /// Current position in the document in pixels.
57         int position;
58         /// Line-scroll amount in pixels.
59         int lineScrollHeight;
60 };
61
62 /// Screen view of a Buffer.
63 /**
64  * A BufferView encapsulates a view onto a particular
65  * buffer, and allows access to operate upon it. A view
66  * is a sliding window of the entire document rendering.
67  * It is the official interface between the LyX core and
68  * the frontend WorkArea.
69  * 
70  * \sa WorkArea
71  * \sa Buffer
72  * \sa CoordCache
73  */
74 class BufferView : boost::noncopyable {
75 public:
76         BufferView();
77
78         ~BufferView();
79
80         /// set the buffer we are viewing.
81         /// \todo FIXME: eventually, we will create a new BufferView
82         /// when switching Buffers, so this method should go.
83         void setBuffer(Buffer * b);
84         /// return the buffer being viewed.
85         Buffer * buffer() const;
86
87         /// resize the BufferView.
88         void resize();
89
90         /// redisplay the referenced buffer.
91         void reload();
92         /// load a buffer into the view.
93         bool loadLyXFile(std::string const & name, bool tolastfiles = true);
94
95         /// perform pending painting updates.
96         /** \c fitcursor means first
97          *  to do a fitcursor, and to force an update if screen
98          *  position changes. \c forceupdate means to force an update
99          *  in any case.
100          * \return true if a full updateMetrics() is needed.
101          */
102         bool update(Update::flags flags = Update::FitCursor | Update::Force);
103
104         /// move the screen to fit the cursor.
105         /// Only to be called with good y coordinates (after a bv::metrics)
106         bool fitCursor();
107         /// reset the scrollbar to reflect current view position.
108         void updateScrollbar();
109         /// return the Scrollbar Parameters.
110         ScrollbarParameters const & scrollbarParameters() const;
111
112         /// Save the current position as bookmark.
113         /// if persistent=false, save to temp_bookmark
114         void saveBookmark(bool persistent);
115         /// goto a specified position.
116         void moveToPosition(
117                 int par_id, ///< Paragraph ID, \sa Paragraph
118                 pos_type par_pos ///< Position in the \c Paragraph
119                 );
120         /// return the current change at the cursor.
121         Change const getCurrentChange() const;
122
123         /// return the lyxtext we are using.
124         LyXText * getLyXText();
125
126         /// return the lyxtext we are using.
127         LyXText const * getLyXText() 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         bool dispatch(FuncRequest const & argument);
156
157         /// request an X11 selection.
158         /// \return the selected string.
159         docstring const requestSelection();
160         /// clear the X11 selection.
161         void clearSelection();
162
163         /// resize method helper for \c WorkArea
164         /// \sa WorkArea
165         /// \sa resise
166         void workAreaResize(int width, int height);
167
168         /// dispatch method helper for \c WorkArea
169         /// \sa WorkArea
170         /// \return true if a full redraw is needed
171         bool workAreaDispatch(FuncRequest const & ev);
172
173         /// access to anchor.
174         pit_type anchor_ref() const;
175
176         /// access to full cursor.
177         LCursor & cursor();
178         /// access to full cursor.
179         LCursor const & cursor() const;
180         /// sets cursor.
181         /// This will also open all relevant collapsable insets.
182         void setCursor(DocIterator const &);
183         /// sets cursor.
184         /// This is used when handling LFUN_MOUSE_PRESS.
185         void mouseSetCursor(LCursor & cur);
186
187         /// sets the selection.
188         /* When \c backwards == false, set anchor
189          * to \c cur and cursor to \c cur + \c length. When \c
190          * backwards == true, set anchor to \c cur and cursor to \c
191          * cur + \c length.
192          */
193         void putSelectionAt(DocIterator const & cur,
194                 int length, bool backwards);
195
196         /// return the internal \c ViewMetricsInfo.
197         /// This is used specifically by the \c Workrea.
198         /// \sa WorkArea
199         /// \sa ViewMetricsInfo
200         ViewMetricsInfo const & viewMetricsInfo();
201         /// update the internal \c ViewMetricsInfo.
202         /// \param singlepar indicates wether
203         void updateMetrics(bool singlepar = false);
204
205         ///
206         CoordCache & coordCache() {
207                 return coord_cache_;
208         }
209         ///
210         CoordCache const & coordCache() const {
211                 return coord_cache_;
212         }
213         /// get this view's keyboard map handler.
214         Intl & getIntl() { return *intl_.get(); }
215         ///
216         Intl const & getIntl() const { return *intl_.get(); }
217
218         /// This signal is emitted when some message shows up.
219         boost::signal<void(docstring)> message;
220
221         /// This signal is emitted when some dialog needs to be shown.
222         boost::signal<void(std::string name)> showDialog;
223
224         /// This signal is emitted when some dialog needs to be shown with
225         /// some data.
226         boost::signal<void(std::string name,
227                 std::string data)> showDialogWithData;
228
229         /// This signal is emitted when some inset dialogs needs to be shown.
230         boost::signal<void(std::string name, std::string data,
231                 InsetBase * inset)> showInsetDialog;
232
233         /// This signal is emitted when some dialogs needs to be updated.
234         boost::signal<void(std::string name,
235                 std::string data)> updateDialog;
236
237         /// This signal is emitted when the layout at the cursor is changed.
238         boost::signal<void(std::string layout)> layoutChanged;
239
240 private:
241         ///
242         bool multiParSel();
243         ///
244         int width_;
245         ///
246         int height_;
247         ///
248         ScrollbarParameters scrollbarParameters_;
249
250         ///
251         ViewMetricsInfo metrics_info_;
252         ///
253         CoordCache coord_cache_;
254         ///
255         Buffer * buffer_;
256
257         /// Estimated average par height for scrollbar.
258         int wh_;
259         ///
260         void menuInsertLyXFile(std::string const & filen);
261
262         /// this is used to handle XSelection events in the right manner.
263         struct {
264                 CursorSlice cursor;
265                 CursorSlice anchor;
266                 bool set;
267         } xsel_cache_;
268         ///
269         LCursor cursor_;
270         ///
271         bool multiparsel_cache_;
272         ///
273         pit_type anchor_ref_;
274         ///
275         int offset_ref_;
276
277         /// keyboard mapping object.
278         boost::scoped_ptr<Intl> const intl_;
279 };
280
281
282 } // namespace lyx
283
284 #endif // BUFFERVIEW_H