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