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