]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
* new function to set border around selection
[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 "update_flags.h"
19
20 #include "support/strfwd.h"
21 #include "support/types.h"
22
23 namespace lyx {
24
25 namespace support { class FileName; }
26
27 namespace frontend { class Painter; }
28 namespace frontend { class GuiBufferViewDelegate; }
29
30 class Buffer;
31 class Change;
32 class CoordCache;
33 class Cursor;
34 class DocIterator;
35 class FuncRequest;
36 class FuncStatus;
37 class Intl;
38 class Inset;
39 class ParIterator;
40 class ParagraphMetrics;
41 class Point;
42 class Text;
43 class TextMetrics;
44
45 enum CursorStatus {
46         CUR_INSIDE,
47         CUR_ABOVE,
48         CUR_BELOW
49 };
50
51 /// Scrollbar Parameters.
52 struct ScrollbarParameters
53 {
54         /// Minimum scrollbar position in pixels.
55         int min;
56         /// Maximum scrollbar position in pixels.
57         int max;
58         /// Current position in the document in pixels.
59         int position;
60         /// Line-scroll amount in pixels.
61         int single_step;
62         /// Page-scroll amount in pixels.
63         int page_step;
64 };
65
66 /// Screen view of a Buffer.
67 /**
68  * A BufferView encapsulates a view onto a particular
69  * buffer, and allows access to operate upon it. A view
70  * is a sliding window of the entire document rendering.
71  * It is the official interface between the LyX core and
72  * the frontend WorkArea.
73  *
74  * \sa WorkArea
75  * \sa Buffer
76  * \sa CoordCache
77  */
78 class BufferView {
79 public:
80         ///
81         explicit BufferView(Buffer & buffer);
82         ///
83         ~BufferView();
84
85         /// return the buffer being viewed.
86         Buffer & buffer();
87         Buffer const & buffer() const;
88
89         ///
90         void setFullScreen(bool full_screen) { full_screen_ = full_screen; }
91
92         /// right margin
93         int rightMargin() const;
94
95         /// left margin
96         int leftMargin() const;
97
98         /// \return true if the BufferView is at the top of the document.
99         bool isTopScreen() const;
100
101         /// \return true if the BufferView is at the bottom of the document.
102         bool isBottomScreen() const;
103
104         /// perform pending metrics updates.
105         /** \c Update::FitCursor means first to do a FitCursor, and to
106          * force an update if screen position changes.
107          * \c Update::Force means to force an update in any case.
108          * \retval true if a screen redraw is needed
109          */
110         void processUpdateFlags(Update::flags flags);
111
112         /// move the screen to fit the cursor.
113         /// Only to be called with good y coordinates (after a bv::metrics)
114         bool fitCursor();
115         /// reset the scrollbar to reflect current view position.
116         void updateScrollbar();
117         /// return the Scrollbar Parameters.
118         ScrollbarParameters const & scrollbarParameters() const;
119         /// \return Tool tip for the given position.
120         docstring toolTip(int x, int y) const;
121         /// \return the context menu for the given position.
122         docstring contextMenu(int x, int y) const;
123
124         /// Save the current position as bookmark.
125         /// if idx == 0, save to temp_bookmark
126         void saveBookmark(unsigned int idx);
127         /// goto a specified position, try top_id first, and then bottom_pit.
128         /// \return true if success
129         bool moveToPosition(
130                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
131                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
132                 int top_id, ///< Paragraph ID, \sa Paragraph
133                 pos_type top_pos ///< Position in the \c Paragraph
134                 );
135         /// return the current change at the cursor.
136         Change const getCurrentChange() const;
137
138         /// move cursor to the named label.
139         void gotoLabel(docstring const & label);
140
141         /// set the cursor based on the given TeX source row.
142         void setCursorFromRow(int row);
143
144         /// Ensure that the BufferView cursor is visible.
145         /// This method will automatically scroll and update the BufferView
146         /// if needed.
147         void showCursor();
148         /// Ensure the passed cursor \p dit is visible.
149         /// This method will automatically scroll and update the BufferView
150         /// if needed.
151         void showCursor(DocIterator const & dit);
152         /// LFUN_SCROLL Helper.
153         void lfunScroll(FuncRequest const & cmd);
154         /// scroll down document by the given number of pixels.
155         void scrollDown(int pixels);
156         /// scroll up document by the given number of pixels.
157         void scrollUp(int pixels);
158         /// scroll document by the given number of pixels.
159         void scroll(int pixels);
160         /// Scroll the view by a number of pixels.
161         void scrollDocView(int pixels);
162         /// Set the cursor position based on the scrollbar one.
163         void setCursorFromScrollbar();
164
165         /// return the pixel width of the document view.
166         int workWidth() const;
167         /// return the pixel height of the document view.
168         int workHeight() const;
169
170         /// return the inline completion postfix.
171         docstring const & inlineCompletion() const;
172         /// return the number of unique characters in the inline completion.
173         size_t const & inlineCompletionUniqueChars() const;
174         /// return the position in the buffer of the inline completion postfix.
175         DocIterator const & inlineCompletionPos() const;
176         /// set the inline completion postfix and its position in the buffer.
177         /// Updates the updateFlags in \c cur.
178         void setInlineCompletion(Cursor & cur, DocIterator const & pos,
179                 docstring const & completion, size_t uniqueChars = 0);
180
181         /// translate and insert a character, using the correct keymap.
182         void translateAndInsert(char_type c, Text * t, Cursor & cur);
183
184         /// return true for events that will handle.
185         FuncStatus getStatus(FuncRequest const & cmd);
186         /// execute the given function.
187         /// \return true if the function has been processed.
188         bool dispatch(FuncRequest const & argument);
189
190         /// request an X11 selection.
191         /// \return the selected string.
192         docstring const requestSelection();
193         /// clear the X11 selection.
194         void clearSelection();
195
196         /// resize the BufferView.
197         /// \sa WorkArea
198         void resize(int width, int height);
199
200         /// dispatch method helper for \c WorkArea
201         /// \sa WorkArea
202         void mouseEventDispatch(FuncRequest const & ev);
203
204         /// access to anchor.
205         pit_type anchor_ref() const;
206
207         ///
208         CursorStatus cursorStatus(DocIterator const & dit) const;
209         /// access to full cursor.
210         Cursor & cursor();
211         /// access to full cursor.
212         Cursor const & cursor() const;
213         /// sets cursor.
214         /// This will also open all relevant collapsable insets.
215         void setCursor(DocIterator const &);
216         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
217         /// \retval true if an update was needed.
218         bool checkDepm(Cursor & cur, Cursor & old);
219         /// sets cursor.
220         /// This is used when handling LFUN_MOUSE_PRESS.
221         bool mouseSetCursor(Cursor & cur, bool select = false);
222
223         /// sets the selection.
224         /* When \c backwards == false, set anchor
225          * to \c cur and cursor to \c cur + \c length. When \c
226          * backwards == true, set anchor to \c cur and cursor to \c
227          * cur + \c length.
228          */
229         void putSelectionAt(DocIterator const & cur,
230                 int length, bool backwards);
231
232         /// update the internal \c ViewMetricsInfo.
233         void updateMetrics();
234
235         ///
236         TextMetrics const & textMetrics(Text const * t) const;
237         TextMetrics & textMetrics(Text const * t);
238         ///
239         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
240
241         ///
242         CoordCache & coordCache();
243         ///
244         CoordCache const & coordCache() const;
245
246         ///
247         Point getPos(DocIterator const & dit, bool boundary) const;
248
249
250         ///
251         void draw(frontend::Painter & pain);
252
253         /// get this view's keyboard map handler.
254         Intl & getIntl();
255         ///
256         Intl const & getIntl() const;
257
258         //
259         // Messages to the GUI
260         //
261         /// This signal is emitted when some message shows up.
262         void message(docstring const & msg);
263
264         /// This signal is emitted when some dialog needs to be shown.
265         void showDialog(std::string const & name);
266
267         /// This signal is emitted when some dialog needs to be shown with
268         /// some data.
269         void showDialog(std::string const & name, std::string const & data,
270                 Inset * inset = 0);
271
272         /// This signal is emitted when some dialogs needs to be updated.
273         void updateDialog(std::string const & name, std::string const & data);
274
275         ///
276         void setGuiDelegate(frontend::GuiBufferViewDelegate *);
277
278         ///
279         docstring contentsOfPlaintextFile(support::FileName const & f);
280         // Insert plain text file (if filename is empty, prompt for one)
281         void insertPlaintextFile(support::FileName const & f, bool asParagraph);
282         ///
283         void insertLyXFile(support::FileName const & f);
284
285 private:
286         /// noncopyable
287         BufferView(BufferView const &);
288         void operator=(BufferView const &);
289
290         // the position relative to (0, baseline) of outermost paragraph
291         Point coordOffset(DocIterator const & dit, bool boundary) const;
292         /// Update current paragraph metrics.
293         /// \return true if no further update is needed.
294         bool singleParUpdate();
295
296         /// Search recursively for the the innermost inset that covers (x, y) position.
297         /// \retval 0 if no inset is found.
298         Inset const * getCoveringInset(
299                 Text const & text, //< The Text where we start searching.
300                 int x, //< x-coordinate on screen
301                 int y  //< y-coordinate on screen
302                 ) const;
303
304         ///
305         int width_;
306         ///
307         int height_;
308         ///
309         bool full_screen_;
310         ///
311         Buffer & buffer_;
312
313         struct Private;
314         Private * const d;
315 };
316
317 /// some space for drawing the 'nested' markers (in pixel)
318 inline int nestMargin() { return 15; }
319
320 /// margin for changebar
321 inline int changebarMargin() { return 12; }
322
323 } // namespace lyx
324
325 #endif // BUFFERVIEW_H