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