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