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