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