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