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