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