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