]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
cleanup after svn hang-up, #undef CursorShape. Should be compilable ganin now.
[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         /// access to offset
188         int offset_ref() const;
189         /// access to anchor
190         lyx::pit_type anchor_ref() const;
191
192         /// access to full cursor
193         LCursor & cursor();
194         /// access to full cursor
195         LCursor const & cursor() const;
196         /// sets cursor and open all relevant collapsable insets.
197         void setCursor(DocIterator const &);
198         /// sets cursor; this is used when handling LFUN_MOUSE_PRESS.
199         void mouseSetCursor(LCursor & cur);
200
201         /* Sets the selection. When \c backwards == false, set anchor
202          * to \c cur and cursor to \c cur + \c length. When \c
203          * backwards == true, set anchor to \c cur and cursor to \c
204          * cur + \c length.
205          */
206         void putSelectionAt(DocIterator const & cur,
207                 int length, bool backwards);
208         ///
209         ViewMetricsInfo const & viewMetricsInfo();
210         ///
211         void updateMetrics(bool singlepar = false);
212
213         /// This signal is emitted when some message shows up.
214         boost::signal<void(lyx::docstring)> message;
215
216 private:
217         ///
218         bool multiParSel();
219         ///
220         int width_;
221         ///
222         int height_;
223         ///
224         ScrollbarParameters scrollbarParameters_;
225
226         /// track changes for the document
227         void trackChanges();
228
229         ///
230         ViewMetricsInfo metrics_info_;
231
232         ///
233         LyXView * owner_;
234         ///
235         Buffer * buffer_;
236
237         /// Estimated average par height for scrollbar
238         int wh_;
239         ///
240         class Position {
241         public:
242                 /// Filename
243                 std::string filename;
244                 /// Cursor paragraph Id
245                 int par_id;
246                 /// Cursor position
247                 lyx::pos_type par_pos;
248                 ///
249                 Position() : par_id(0), par_pos(0) {}
250                 ///
251                 Position(std::string const & f, int id, lyx::pos_type pos)
252                         : filename(f), par_id(id), par_pos(pos) {}
253         };
254         ///
255         std::vector<Position> saved_positions;
256         ///
257         void menuInsertLyXFile(std::string const & filen);
258
259         /// this is used to handle XSelection events in the right manner
260         struct {
261                 CursorSlice cursor;
262                 CursorSlice anchor;
263                 bool set;
264         } xsel_cache_;
265         ///
266         LCursor cursor_;
267         ///
268         bool multiparsel_cache_;
269         ///
270         lyx::pit_type anchor_ref_;
271         ///
272         int offset_ref_;
273 };
274
275 #endif // BUFFERVIEW_H