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