]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
c03a8873873dc2b897c7b176f79d7a8c044c0bd3
[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         /// Save the current position as bookmark i
131         void savePosition(unsigned int i);
132         /// Restore the position from bookmark i
133         void restorePosition(unsigned int i);
134         /// does the given bookmark have a saved position ?
135         bool isSavedPosition(unsigned int i);
136         /// save bookmarks to .lyx/session
137         void saveSavedPositions();
138
139         /// return the current change at the cursor
140         Change const getCurrentChange();
141
142         /// return the lyxtext we are using
143         LyXText * getLyXText();
144
145         /// return the lyxtext we are using
146         LyXText const * getLyXText() const;
147
148         /// move cursor to the named label
149         void gotoLabel(std::string const & label);
150
151         /// set the cursor based on the given TeX source row
152         void setCursorFromRow(int row);
153
154         /// center the document view around the cursor
155         void center();
156         /// scroll document by the given number of lines of default height
157         void scroll(int lines);
158         /// Scroll the view by a number of pixels
159         void scrollDocView(int pixels);
160         /// Set the cursor position based on the scrollbar one.
161         void setCursorFromScrollbar();
162
163         /// return the pixel width of the document view
164         int workWidth() const;
165         /// return the pixel height of the document view
166         int workHeight() const;
167
168         /// switch between primary and secondary keymaps for RTL entry
169         void switchKeyMap();
170
171         /// return true for events that will handle
172         FuncStatus getStatus(FuncRequest const & cmd);
173         /// execute the given function
174         bool dispatch(FuncRequest const & argument);
175
176         ///
177         lyx::docstring const requestSelection();
178         ///
179         void clearSelection();
180
181         ///
182         void workAreaResize(int width, int height);
183
184         /// a function should be executed from the workarea
185         bool workAreaDispatch(FuncRequest const & ev);
186
187         /// clear the X selection
188         void unsetXSel();
189
190         /// access to offset
191         int offset_ref() const;
192         /// access to anchor
193         lyx::pit_type anchor_ref() const;
194
195         /// access to full cursor
196         LCursor & cursor();
197         /// access to full cursor
198         LCursor const & cursor() const;
199         /// sets cursor and open all relevant collapsable insets.
200         void setCursor(DocIterator const &);
201         /// sets cursor; this is used when handling LFUN_MOUSE_PRESS.
202         void mouseSetCursor(LCursor & cur);
203
204         /* Sets the selection. When \c backwards == false, set anchor
205          * to \c cur and cursor to \c cur + \c length. When \c
206          * backwards == true, set anchor to \c cur and cursor to \c
207          * cur + \c length.
208          */
209         void putSelectionAt(DocIterator const & cur,
210                 int length, bool backwards);
211         ///
212         ViewMetricsInfo const & viewMetricsInfo();
213         ///
214         void updateMetrics(bool singlepar = false);
215
216         /// This signal is emitted when some message shows up.
217         boost::signal<void(lyx::docstring)> message;
218
219 private:
220         ///
221         bool multiParSel();
222         ///
223         int width_;
224         ///
225         int height_;
226         ///
227         ScrollbarParameters scrollbarParameters_;
228
229         /// track changes for the document
230         void trackChanges();
231
232         ///
233         ViewMetricsInfo metrics_info_;
234
235         ///
236         LyXView * owner_;
237         ///
238         Buffer * buffer_;
239
240         /// Estimated average par height for scrollbar
241         int wh_;
242         ///
243         class Position {
244         public:
245                 /// Filename
246                 std::string filename;
247                 /// Cursor paragraph Id
248                 int par_id;
249                 /// Cursor position
250                 lyx::pos_type par_pos;
251                 ///
252                 Position() : par_id(0), par_pos(0) {}
253                 ///
254                 Position(std::string const & f, int id, lyx::pos_type pos)
255                         : filename(f), par_id(id), par_pos(pos) {}
256         };
257         ///
258         std::vector<Position> saved_positions;
259         ///
260         void menuInsertLyXFile(std::string const & filen);
261
262         /// this is used to handle XSelection events in the right manner
263         struct {
264                 CursorSlice cursor;
265                 CursorSlice anchor;
266                 bool set;
267         } xsel_cache_;
268         ///
269         LCursor cursor_;
270         ///
271         bool multiparsel_cache_;
272         ///
273         lyx::pit_type anchor_ref_;
274         ///
275         int offset_ref_;
276 };
277
278 #endif // BUFFERVIEW_H