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