]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
Fixed some lines that were too long. It compiled afterwards.
[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 #include "TextMetrics.h"
22 #include "update_flags.h"
23
24 #include "support/types.h"
25
26 #include <boost/tuple/tuple.hpp>
27 #include <boost/utility.hpp>
28 #include <boost/signal.hpp>
29
30 #include <utility>
31 #include <string>
32
33
34 namespace lyx {
35
36 namespace support { class FileName; }
37
38 class Buffer;
39 class Change;
40 class DocIterator;
41 class FuncRequest;
42 class FuncStatus;
43 class Intl;
44 class Cursor;
45 class Text;
46 class ParIterator;
47 class ParagraphMetrics;
48 class ViewMetricsInfo;
49
50 /// Scrollbar Parameters.
51 struct ScrollbarParameters
52 {
53         void reset(int h = 0, int p = 0, int l = 0)
54         {
55                 height = h;
56                 position = p;
57                 lineScrollHeight = l;
58         }
59
60         /// Total document height in pixels.
61         int height;
62         /// Current position in the document in pixels.
63         int position;
64         /// Line-scroll amount in pixels.
65         int lineScrollHeight;
66 };
67
68 /// Screen view of a Buffer.
69 /**
70  * A BufferView encapsulates a view onto a particular
71  * buffer, and allows access to operate upon it. A view
72  * is a sliding window of the entire document rendering.
73  * It is the official interface between the LyX core and
74  * the frontend WorkArea.
75  *
76  * \sa WorkArea
77  * \sa Buffer
78  * \sa CoordCache
79  */
80 class BufferView : boost::noncopyable {
81 public:
82         BufferView();
83
84         ~BufferView();
85
86         /// set the buffer we are viewing.
87         /// \todo FIXME: eventually, we will create a new BufferView
88         /// when switching Buffers, so this method should go.
89         /// returns the buffer currently set
90         Buffer * setBuffer(Buffer * b);
91         /// return the buffer being viewed.
92         Buffer * buffer() const;
93
94         /// resize the BufferView.
95         void resize();
96
97         /// perform pending metrics updates.
98         /** \c Update::FitCursor means first to do a FitCursor, and to
99          * force an update if screen position changes.
100          * \c Update::Force means to force an update in any case.
101          * \retval true if a screen redraw is needed
102          */
103         bool update(Update::flags flags = Update::FitCursor | Update::Force);
104
105         /// move the screen to fit the cursor.
106         /// Only to be called with good y coordinates (after a bv::metrics)
107         bool fitCursor();
108         /// reset the scrollbar to reflect current view position.
109         void updateScrollbar();
110         /// return the Scrollbar Parameters.
111         ScrollbarParameters const & scrollbarParameters() const;
112
113         /// Save the current position as bookmark.
114         /// if idx == 0, save to temp_bookmark
115         void saveBookmark(unsigned int idx);
116         /// goto a specified position, try top_id first, and then bottom_pit
117         /// return the bottom_pit and top_id of the new paragraph
118         boost::tuple<pit_type, pos_type, int> moveToPosition(
119                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
120                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
121                 int top_id, ///< Paragraph ID, \sa Paragraph
122                 pos_type top_pos ///< Position in the \c Paragraph
123                 );
124         /// return the current change at the cursor.
125         Change const getCurrentChange() const;
126
127         /// move cursor to the named label.
128         void gotoLabel(docstring const & label);
129
130         /// set the cursor based on the given TeX source row.
131         void setCursorFromRow(int row);
132
133         /// center the document view around the cursor.
134         void center();
135         /// scroll document by the given number of lines of default height.
136         void scroll(int lines);
137         /// Scroll the view by a number of pixels.
138         void scrollDocView(int pixels);
139         /// Set the cursor position based on the scrollbar one.
140         void setCursorFromScrollbar();
141
142         /// return the pixel width of the document view.
143         int workWidth() const;
144         /// return the pixel height of the document view.
145         int workHeight() const;
146
147         /// translate and insert a character, using the correct keymap.
148         void translateAndInsert(char_type c, Text * t, Cursor & cur);
149
150         /// return true for events that will handle.
151         FuncStatus getStatus(FuncRequest const & cmd);
152         /// execute the given function.
153         /// \return the Update::flags for further metrics update.
154         Update::flags dispatch(FuncRequest const & argument);
155
156         /// request an X11 selection.
157         /// \return the selected string.
158         docstring const requestSelection();
159         /// clear the X11 selection.
160         void clearSelection();
161
162         /// resize method helper for \c WorkArea
163         /// \sa WorkArea
164         /// \sa resise
165         void workAreaResize(int width, int height);
166
167         /// dispatch method helper for \c WorkArea
168         /// \sa WorkArea
169         /// \retval true if a redraw is needed
170         bool workAreaDispatch(FuncRequest const & ev);
171
172         /// access to anchor.
173         pit_type anchor_ref() const;
174
175         /// access to full cursor.
176         Cursor & cursor();
177         /// access to full cursor.
178         Cursor const & cursor() const;
179         /// sets cursor.
180         /// This will also open all relevant collapsable insets.
181         void setCursor(DocIterator const &);
182         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
183         /// \retval true if an update was needed.
184         bool checkDepm(Cursor & cur, Cursor & old);
185         /// sets cursor.
186         /// This is used when handling LFUN_MOUSE_PRESS.
187         bool mouseSetCursor(Cursor & cur);
188
189         /// sets the selection.
190         /* 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         /// return the internal \c ViewMetricsInfo.
199         /// This is used specifically by the \c Workrea.
200         /// \sa WorkArea
201         /// \sa ViewMetricsInfo
202         ViewMetricsInfo const & viewMetricsInfo();
203         /// update the internal \c ViewMetricsInfo.
204         /// \param singlepar indicates wether
205         void updateMetrics(bool singlepar = false);
206
207         ///
208         TextMetrics const & textMetrics(Text const * t) const;
209         TextMetrics & textMetrics(Text const * t);
210         ///
211         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
212
213         ///
214         CoordCache & coordCache() {
215                 return coord_cache_;
216         }
217         ///
218         CoordCache const & coordCache() const {
219                 return coord_cache_;
220         }
221         /// get this view's keyboard map handler.
222         Intl & getIntl() { return *intl_.get(); }
223         ///
224         Intl const & getIntl() const { return *intl_.get(); }
225
226         /// This signal is emitted when some message shows up.
227         boost::signal<void(docstring)> message;
228
229         /// This signal is emitted when some dialog needs to be shown.
230         boost::signal<void(std::string name)> showDialog;
231
232         /// This signal is emitted when some dialog needs to be shown with
233         /// some data.
234         boost::signal<void(std::string name,
235                 std::string data)> showDialogWithData;
236
237         /// This signal is emitted when some inset dialogs needs to be shown.
238         boost::signal<void(std::string name, std::string data,
239                 Inset * inset)> showInsetDialog;
240
241         /// This signal is emitted when some dialogs needs to be updated.
242         boost::signal<void(std::string name,
243                 std::string data)> updateDialog;
244
245         /// This signal is emitted when the layout at the cursor is changed.
246         boost::signal<void(docstring layout)> layoutChanged;
247
248 private:
249         ///
250         bool multiParSel();
251
252         /// Search recursively for the the innermost inset that covers (x, y) position.
253         /// \retval 0 if no inset is found.
254         Inset const * getCoveringInset(
255                 Text const & text, //< The Text where we start searching.
256                 int x, //< x-coordinate on screen
257                 int y  //< y-coordinate on screen
258                 );
259
260         ///
261         int width_;
262         ///
263         int height_;
264         ///
265         ScrollbarParameters scrollbarParameters_;
266
267         ///
268         ViewMetricsInfo metrics_info_;
269         ///
270         CoordCache coord_cache_;
271         ///
272         Buffer * buffer_;
273
274         /// Estimated average par height for scrollbar.
275         int wh_;
276         ///
277         void menuInsertLyXFile(std::string const & filen);
278
279         /// this is used to handle XSelection events in the right manner.
280         struct {
281                 CursorSlice cursor;
282                 CursorSlice anchor;
283                 bool set;
284         } xsel_cache_;
285         ///
286         Cursor cursor_;
287         ///
288         bool multiparsel_cache_;
289         ///
290         pit_type anchor_ref_;
291         ///
292         int offset_ref_;
293
294         /// keyboard mapping object.
295         boost::scoped_ptr<Intl> const intl_;
296
297         /// last visited inset (kept to send setMouseHover(false) )
298         Inset * last_inset_;
299
300         /// A map from a Text to the associated text metrics
301         typedef std::map<Text const *, TextMetrics> TextMetricsCache;
302         mutable TextMetricsCache text_metrics_;
303 };
304
305
306 } // namespace lyx
307
308 #endif // BUFFERVIEW_H