]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
* Painter.h:
[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         /// The total document height in pixels
55         int height;
56         /// The current position in the document, in pixels
57         int position;
58         /// the line-scroll amount, in pixels
59         int lineScrollHeight;
60 };
61
62 /**
63  * A buffer view encapsulates a view onto a particular
64  * buffer, and allows access to operate upon it. A view
65  * is a sliding window of the entire document rendering.
66  *
67  * Eventually we will allow several views onto a single
68  * buffer, but not yet.
69  */
70 class BufferView : boost::noncopyable {
71 public:
72         BufferView();
73
74         ~BufferView();
75
76         /// set the buffer we are viewing
77         void setBuffer(Buffer * b);
78         /// return the buffer being viewed
79         Buffer * buffer() const;
80
81         /// resize event has happened
82         void resize();
83
84         /// reload the contained buffer
85         void reload();
86         /// load a buffer into the view
87         bool loadLyXFile(std::string const & name, bool tolastfiles = true);
88
89         /** perform pending painting updates. \c fitcursor means first
90          *  to do a fitcursor, and to force an update if screen
91          *  position changes. \c forceupdate means to force an update
92          *  in any case.
93          * \return true if a full updateMetrics() is needed.
94          */
95         bool update(Update::flags flags = Update::FitCursor | Update::Force);
96
97         /// move the screen to fit the cursor. Only to be called with
98         /// good y coordinates (after a bv::metrics)
99         bool fitCursor();
100         /// reset the scrollbar to reflect current view position
101         void updateScrollbar();
102         /// return the Scrollbar Parameters
103         ScrollbarParameters const & scrollbarParameters() const;
104
105         /// Save the current position as bookmark i
106         void savePosition(unsigned int i);
107         /// Restore the position from bookmark i
108         void restorePosition(unsigned int i);
109         /// does the given bookmark have a saved position ?
110         bool isSavedPosition(unsigned int i);
111         /// save bookmarks to .lyx/session
112         void saveSavedPositions();
113
114         /// return the current change at the cursor
115         Change const getCurrentChange() const;
116
117         /// return the lyxtext we are using
118         LyXText * getLyXText();
119
120         /// return the lyxtext we are using
121         LyXText const * getLyXText() const;
122
123         /// move cursor to the named label
124         void gotoLabel(docstring const & label);
125
126         /// set the cursor based on the given TeX source row
127         void setCursorFromRow(int row);
128
129         /// center the document view around the cursor
130         void center();
131         /// scroll document by the given number of lines of default height
132         void scroll(int lines);
133         /// Scroll the view by a number of pixels
134         void scrollDocView(int pixels);
135         /// Set the cursor position based on the scrollbar one.
136         void setCursorFromScrollbar();
137
138         /// return the pixel width of the document view
139         int workWidth() const;
140         /// return the pixel height of the document view
141         int workHeight() const;
142
143         /// switch between primary and secondary keymaps for RTL entry
144         void switchKeyMap();
145
146         /// return true for events that will handle
147         FuncStatus getStatus(FuncRequest const & cmd);
148         /// execute the given function
149         bool dispatch(FuncRequest const & argument);
150
151         ///
152         docstring const requestSelection();
153         ///
154         void clearSelection();
155
156         ///
157         void workAreaResize(int width, int height);
158
159         /// a function should be executed from the workarea
160         bool workAreaDispatch(FuncRequest const & ev);
161
162         /// access to anchor
163         pit_type anchor_ref() const;
164
165         /// access to full cursor
166         LCursor & cursor();
167         /// access to full cursor
168         LCursor const & cursor() const;
169         /// sets cursor and open all relevant collapsable insets.
170         void setCursor(DocIterator const &);
171         /// sets cursor; this is used when handling LFUN_MOUSE_PRESS.
172         void mouseSetCursor(LCursor & cur);
173
174         /* Sets the selection. When \c backwards == false, set anchor
175          * to \c cur and cursor to \c cur + \c length. When \c
176          * backwards == true, set anchor to \c cur and cursor to \c
177          * cur + \c length.
178          */
179         void putSelectionAt(DocIterator const & cur,
180                 int length, bool backwards);
181         ///
182         ViewMetricsInfo const & viewMetricsInfo();
183         ///
184         void updateMetrics(bool singlepar = false);
185
186         ///
187         CoordCache & coordCache() {
188                 return coord_cache_;
189         }
190         ///
191         CoordCache const & coordCache() const {
192                 return coord_cache_;
193         }
194         /// get this view's keyboard map handler
195         Intl & getIntl() { return *intl_.get(); }
196         ///
197         Intl const & getIntl() const { return *intl_.get(); }
198
199         /// This signal is emitted when some message shows up.
200         boost::signal<void(docstring)> message;
201
202         /// This signal is emitted when some dialog needs to be shown.
203         boost::signal<void(std::string name)> showDialog;
204
205         /// This signal is emitted when some dialog needs to be shown with
206         /// some data
207         boost::signal<void(std::string name,
208                 std::string data)> showDialogWithData;
209
210         /// This signal is emitted when some inset dialogs needs to be shown.
211         boost::signal<void(std::string name, std::string data,
212                 InsetBase * inset)> showInsetDialog;
213
214         /// This signal is emitted when some dialogs needs to be updated.
215         boost::signal<void(std::string name,
216                 std::string data)> updateDialog;
217
218         /// This signal is emitted when the layout at the cursor is changed.
219         boost::signal<void(std::string layout)> layoutChanged;
220
221 private:
222         ///
223         bool multiParSel();
224         ///
225         int width_;
226         ///
227         int height_;
228         ///
229         ScrollbarParameters scrollbarParameters_;
230
231         ///
232         ViewMetricsInfo metrics_info_;
233         CoordCache coord_cache_;
234         ///
235         Buffer * buffer_;
236
237         /// Estimated average par height for scrollbar
238         int wh_;
239         ///
240         class Position {
241         public:
242                 /// Filename
243                 std::string filename;
244                 /// Cursor paragraph Id
245                 int par_id;
246                 /// Cursor position
247                 pos_type par_pos;
248                 ///
249                 Position() : par_id(0), par_pos(0) {}
250                 ///
251                 Position(std::string const & f, int id, pos_type pos)
252                         : filename(f), par_id(id), par_pos(pos) {}
253         };
254         ///
255         std::vector<Position> saved_positions;
256         ///
257         void menuInsertLyXFile(std::string const & filen);
258
259         /// this is used to handle XSelection events in the right manner
260         struct {
261                 CursorSlice cursor;
262                 CursorSlice anchor;
263                 bool set;
264         } xsel_cache_;
265         ///
266         LCursor cursor_;
267         ///
268         bool multiparsel_cache_;
269         ///
270         pit_type anchor_ref_;
271         ///
272         int offset_ref_;
273
274         /// keyboard mapping object
275         boost::scoped_ptr<Intl> const intl_;
276 };
277
278
279 } // namespace lyx
280
281 #endif // BUFFERVIEW_H