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