]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
Use home made code for the bool facet.
[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         void setBuffer(Buffer * b);
90         /// return the buffer being viewed.
91         Buffer * buffer() const;
92
93         /// resize the BufferView.
94         void resize();
95
96         /// perform pending metrics updates.
97         /** \c Update::FitCursor means first to do a FitCursor, and to
98          * force an update if screen position changes.
99          * \c Update::Force means to force an update in any case.
100          * \retval true if a screen redraw is needed
101          */
102         bool update(Update::flags flags = Update::FitCursor | Update::Force);
103
104         /// move the screen to fit the cursor.
105         /// Only to be called with good y coordinates (after a bv::metrics)
106         bool fitCursor();
107         /// reset the scrollbar to reflect current view position.
108         void updateScrollbar();
109         /// return the Scrollbar Parameters.
110         ScrollbarParameters const & scrollbarParameters() const;
111
112         /// Save the current position as bookmark.
113         /// if idx == 0, save to temp_bookmark
114         void saveBookmark(unsigned int idx);
115         /// goto a specified position, try top_id first, and then bottom_pit
116         /// return the bottom_pit and top_id of the new paragraph
117         boost::tuple<pit_type, pos_type, int> moveToPosition(
118                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
119                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
120                 int top_id, ///< Paragraph ID, \sa Paragraph
121                 pos_type top_pos ///< Position in the \c Paragraph
122                 );
123         /// return the current change at the cursor.
124         Change const getCurrentChange() const;
125
126         /// move cursor to the named label.
127         void gotoLabel(docstring const & label);
128
129         /// set the cursor based on the given TeX source row.
130         void setCursorFromRow(int row);
131
132         /// center the document view around the cursor.
133         void center();
134         /// scroll document by the given number of lines of default height.
135         void scroll(int lines);
136         /// Scroll the view by a number of pixels.
137         void scrollDocView(int pixels);
138         /// Set the cursor position based on the scrollbar one.
139         void setCursorFromScrollbar();
140
141         /// return the pixel width of the document view.
142         int workWidth() const;
143         /// return the pixel height of the document view.
144         int workHeight() const;
145
146         /// translate and insert a character, using the correct keymap.
147         void translateAndInsert(char_type c, Text * t, Cursor & cur);
148
149         /// return true for events that will handle.
150         FuncStatus getStatus(FuncRequest const & cmd);
151         /// execute the given function.
152         /// \return the Update::flags for further metrics update.
153         Update::flags dispatch(FuncRequest const & argument);
154
155         /// request an X11 selection.
156         /// \return the selected string.
157         docstring const requestSelection();
158         /// clear the X11 selection.
159         void clearSelection();
160
161         /// resize method helper for \c WorkArea
162         /// \sa WorkArea
163         /// \sa resise
164         void workAreaResize(int width, int height);
165
166         /// dispatch method helper for \c WorkArea
167         /// \sa WorkArea
168         /// \retval true if a redraw is needed
169         bool workAreaDispatch(FuncRequest const & ev);
170
171         /// access to anchor.
172         pit_type anchor_ref() const;
173
174         /// access to full cursor.
175         Cursor & cursor();
176         /// access to full cursor.
177         Cursor const & cursor() const;
178         /// sets cursor.
179         /// This will also open all relevant collapsable insets.
180         void setCursor(DocIterator const &);
181         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
182         /// \retval true if an update was needed.
183         bool checkDepm(Cursor & cur, Cursor & old);
184         /// sets cursor.
185         /// This is used when handling LFUN_MOUSE_PRESS.
186         bool mouseSetCursor(Cursor & cur);
187
188         /// sets the selection.
189         /* When \c backwards == false, set anchor
190          * to \c cur and cursor to \c cur + \c length. When \c
191          * backwards == true, set anchor to \c cur and cursor to \c
192          * cur + \c length.
193          */
194         void putSelectionAt(DocIterator const & cur,
195                 int length, bool backwards);
196
197         /// return the internal \c ViewMetricsInfo.
198         /// This is used specifically by the \c Workrea.
199         /// \sa WorkArea
200         /// \sa ViewMetricsInfo
201         ViewMetricsInfo const & viewMetricsInfo();
202         /// update the internal \c ViewMetricsInfo.
203         /// \param singlepar indicates wether
204         void updateMetrics(bool singlepar = false);
205
206         ///
207         TextMetrics const & textMetrics(Text const * t) const;
208         TextMetrics & textMetrics(Text const * t);
209         ///
210         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
211
212         ///
213         CoordCache & coordCache() {
214                 return coord_cache_;
215         }
216         ///
217         CoordCache const & coordCache() const {
218                 return coord_cache_;
219         }
220         /// get this view's keyboard map handler.
221         Intl & getIntl() { return *intl_.get(); }
222         ///
223         Intl const & getIntl() const { return *intl_.get(); }
224
225         /// This signal is emitted when some message shows up.
226         boost::signal<void(docstring)> message;
227
228         /// This signal is emitted when some dialog needs to be shown.
229         boost::signal<void(std::string name)> showDialog;
230
231         /// This signal is emitted when some dialog needs to be shown with
232         /// some data.
233         boost::signal<void(std::string name,
234                 std::string data)> showDialogWithData;
235
236         /// This signal is emitted when some inset dialogs needs to be shown.
237         boost::signal<void(std::string name, std::string data,
238                 Inset * inset)> showInsetDialog;
239
240         /// This signal is emitted when some dialogs needs to be updated.
241         boost::signal<void(std::string name,
242                 std::string data)> updateDialog;
243
244         /// This signal is emitted when the layout at the cursor is changed.
245         boost::signal<void(docstring layout)> layoutChanged;
246
247 private:
248         ///
249         bool multiParSel();
250
251         /// Search recursively for the the innermost inset that covers (x, y) position.
252         /// \retval 0 if no inset is found.
253         Inset const * getCoveringInset(
254                 Text const & text, //< The Text where we start searching.
255                 int x, //< x-coordinate on screen
256                 int y  //< y-coordinate on screen
257                 );
258
259         ///
260         int width_;
261         ///
262         int height_;
263         ///
264         ScrollbarParameters scrollbarParameters_;
265
266         ///
267         ViewMetricsInfo metrics_info_;
268         ///
269         CoordCache coord_cache_;
270         ///
271         Buffer * buffer_;
272
273         /// Estimated average par height for scrollbar.
274         int wh_;
275         ///
276         void menuInsertLyXFile(std::string const & filen);
277
278         /// this is used to handle XSelection events in the right manner.
279         struct {
280                 CursorSlice cursor;
281                 CursorSlice anchor;
282                 bool set;
283         } xsel_cache_;
284         ///
285         Cursor cursor_;
286         ///
287         bool multiparsel_cache_;
288         ///
289         pit_type anchor_ref_;
290         ///
291         int offset_ref_;
292
293         /// keyboard mapping object.
294         boost::scoped_ptr<Intl> const intl_;
295
296         /// last visited inset (kept to send setMouseHover(false) )
297         Inset * last_inset_;
298
299         /// A map from a Text to the associated text metrics
300         typedef std::map<Text const *, TextMetrics> TextMetricsCache;
301         mutable TextMetricsCache text_metrics_;
302 };
303
304
305 } // namespace lyx
306
307 #endif // BUFFERVIEW_H