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