4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Alfredo Braustein
8 * \author Lars Gullik Bjønnes
10 * \author Jürgen Vigna
12 * Full author contact details are available in file CREDITS.
18 #include "CoordCache.h"
19 #include "DocumentClassPtr.h"
21 #include "update_flags.h"
23 #include "support/strfwd.h"
24 #include "support/types.h"
28 namespace support { class FileName; }
30 namespace frontend { struct CaretGeometry; }
31 namespace frontend { class Painter; }
32 namespace frontend { class GuiBufferViewDelegate; }
49 class ParagraphMetrics;
60 /// Scrollbar Parameters.
61 struct ScrollbarParameters
63 // These parameters are normalized against the screen geometry and pixel
64 // coordinates. Position 0 corresponds to the top the the screen.
66 : min(0), max(0), single_step(1), page_step(1)
68 /// Minimum scrollbar position in pixels.
70 /// Maximum scrollbar position in pixels.
72 /// Line-scroll amount in pixels.
74 /// Page-scroll amount in pixels.
78 /// Screen view of a Buffer.
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.
93 explicit BufferView(Buffer & buffer);
97 /// return the buffer being viewed.
99 Buffer const & buffer() const;
102 void setFullScreen(bool full_screen) { full_screen_ = full_screen; }
105 int rightMargin() const;
107 int leftMargin() const;
109 int topMargin() const;
111 int bottomMargin() const;
113 docstring const & searchRequestCache() const;
114 void setSearchRequestCache(docstring const & text);
116 /// return the on-screen size of this length
118 * This is a wrapper around Length::inPixels that uses the
119 * bufferview width as width and the EM value of the default
122 int inPixels(Length const & len) const;
124 /** Return the number of pixels equivalent to \c pix pixels at
125 * 100dpi and 100% zoom.
127 int zoomedPixels(int pix) const;
129 /// \return true if the BufferView is at the top of the document.
130 bool isTopScreen() const;
132 /// \return true if the BufferView is at the bottom of the document.
133 bool isBottomScreen() const;
135 /// Add \p flags to current update flags and trigger an update.
136 /* If this method is invoked several times before the update
137 * actually takes place, the effect is cumulative.
138 * \c Update::FitCursor means first to do a FitCursor, and to
139 * force an update if screen position changes.
140 * \c Update::Force means to force an update in any case.
142 void processUpdateFlags(Update::flags flags);
144 /// return true if one shall move the screen to fit the cursor.
145 /// Only to be called with good y coordinates (after a bv::metrics)
146 bool needsFitCursor() const;
148 // Returns the amount of horizontal scrolling applied to the
149 // top-level row where the cursor lies
150 int horizScrollOffset() const;
151 // Returns the amount of horizontal scrolling applied to the
152 // row of text starting at (pit, pos)
153 int horizScrollOffset(Text const * text,
154 pit_type pit, pos_type pos) const;
156 /// reset the scrollbar to reflect current view position.
157 void updateScrollbar();
158 /// return the Scrollbar Parameters.
159 ScrollbarParameters const & scrollbarParameters() const;
160 /// \return Tool tip for the given position.
161 docstring toolTip(int x, int y) const;
162 /// \return the context menu for the given position.
163 std::string contextMenu(int x, int y) const;
164 /// \return the math inset with a context menu for the given position
165 Inset const * mathContextMenu(InsetMathNest const * inset,
166 CoordCache::Insets const & inset_cache, int x, int y) const;
168 /// Save the current position as bookmark.
169 /// if idx == 0, save to temp_bookmark
170 void saveBookmark(unsigned int idx);
171 /// goto a specified position, try top_id first, and then bottom_pit.
172 /// \return true if success
174 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
175 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
176 int top_id, ///< Paragraph ID, \sa Paragraph
177 pos_type top_pos ///< Position in the \c Paragraph
179 /// return the current change at the cursor.
180 Change const getCurrentChange() const;
182 /// move cursor to the named label.
183 void gotoLabel(docstring const & label);
185 /// set the cursor based on the given TeX source row.
186 bool setCursorFromRow(int row);
187 /// set the cursor based on the given start and end TextEntries.
188 bool setCursorFromEntries(TexRow::TextEntry start, TexRow::TextEntry end);
190 /// set cursor to the given inset. Return true if found.
191 bool setCursorFromInset(Inset const *);
192 /// Recenters the BufferView such that the passed cursor
193 /// is in the center.
195 /// Ensure that the BufferView cursor is visible.
196 /// This method will automatically scroll and update the BufferView
197 /// (metrics+drawing) if needed.
199 /// Ensure the passed cursor \p dit is visible.
200 /// This method will automatically scroll and update the BufferView
201 /// (metrics+drawing) if needed.
202 /// \param recenter Whether the cursor should be centered on screen
203 void showCursor(DocIterator const & dit, bool recenter,
205 /// Scroll to the cursor.
206 void scrollToCursor();
207 /// Scroll to the cursor.
208 /// \param recenter Whether the cursor should be centered on screen
209 bool scrollToCursor(DocIterator const & dit, bool recenter);
210 /// scroll down document by the given number of pixels.
211 int scrollDown(int pixels);
212 /// scroll up document by the given number of pixels.
213 int scrollUp(int pixels);
214 /// scroll document by the given number of pixels.
215 int scroll(int pixels);
216 /// Scroll the view by a number of pixels.
217 void scrollDocView(int pixels, bool update);
218 /// Set the cursor position based on the scrollbar one.
219 void setCursorFromScrollbar();
221 /// return the pixel width of the document view.
222 int workWidth() const;
223 /// return the pixel height of the document view.
224 int workHeight() const;
226 /// return the inline completion postfix.
227 docstring const & inlineCompletion() const;
228 /// return the number of unique characters in the inline completion.
229 size_t inlineCompletionUniqueChars() const;
230 /// return the position in the buffer of the inline completion postfix.
231 DocIterator const & inlineCompletionPos() const;
232 /// make sure inline completion position is OK
233 void resetInlineCompletionPos();
234 /// set the inline completion postfix and its position in the buffer.
235 /// Updates the updateFlags in \c cur.
236 void setInlineCompletion(Cursor const & cur, DocIterator const & pos,
237 docstring const & completion, size_t uniqueChars = 0);
239 /// translate and insert a character, using the correct keymap.
240 void translateAndInsert(char_type c, Text * t, Cursor & cur);
242 /// \return true if we've made a decision
243 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
244 /// execute the given function.
245 void dispatch(FuncRequest const & cmd, DispatchResult & dr);
247 /// request an X11 selection.
248 /// \return the selected string.
249 docstring requestSelection();
250 /// clear the X11 selection.
251 void clearSelection();
253 /// resize the BufferView.
255 void resize(int width, int height);
257 /// dispatch method helper for \c WorkArea
259 void mouseEventDispatch(FuncRequest const & ev);
262 CursorStatus cursorStatus(DocIterator const & dit) const;
263 /// access to full cursor.
265 /// access to full cursor.
266 Cursor const & cursor() const;
268 /// This will also open all relevant collapsible insets.
269 void setCursor(DocIterator const &);
270 /// set the selection up to dit.
271 void setCursorSelectionTo(DocIterator const & dit);
272 /// Check deleteEmptyParagraphMechanism and update metrics if needed.
273 /// \retval true if an update was needed.
274 bool checkDepm(Cursor & cur, Cursor & old);
276 /// This is used when handling LFUN_MOUSE_PRESS.
277 bool mouseSetCursor(Cursor & cur, bool select = false);
279 /// sets the selection.
280 /* When \c backwards == false, set anchor
281 * to \c cur and cursor to \c cur + \c length. When \c
282 * backwards == true, set anchor to \c cur and cursor to \c
285 void putSelectionAt(DocIterator const & cur,
286 int length, bool backwards);
288 /// selects the item at cursor if its paragraph is empty.
289 bool selectIfEmpty(DocIterator & cur);
291 /// update the internal \c ViewMetricsInfo.
292 void updateMetrics();
294 // this is the "nodraw" drawing stage: only set the positions of the
295 // insets in metrics cache.
296 void updatePosCache();
299 TextMetrics const & textMetrics(Text const * t) const;
300 TextMetrics & textMetrics(Text const * t);
302 ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
305 CoordCache & coordCache();
307 CoordCache const & coordCache() const;
310 MathRow const & mathRow(MathData const * cell) const;
312 void setMathRow(MathData const * cell, MathRow const & mrow);
315 Point getPos(DocIterator const & dit) const;
316 /// is the paragraph of the cursor visible ?
317 bool paragraphVisible(DocIterator const & dit) const;
318 /// is the caret currently visible in the view
319 bool caretInView() const;
320 /// get the position and height of the caret
321 void caretPosAndDim(Point & p, Dimension & dim) const;
322 /// compute the shape of the caret
323 void buildCaretGeometry(bool complet);
324 /// the shape of the caret
325 frontend::CaretGeometry const & caretGeometry() const;
328 void draw(frontend::Painter & pain, bool paint_caret);
330 /// get this view's keyboard map handler.
333 Intl const & getIntl() const;
336 // Messages to the GUI
338 /// This signal is emitted when some message shows up.
339 void message(docstring const & msg);
341 /// This signal is emitted when some dialog needs to be shown.
342 void showDialog(std::string const & name);
344 /// This signal is emitted when some dialog needs to be shown with
346 void showDialog(std::string const & name, std::string const & data,
347 Inset * inset = nullptr);
349 /// This signal is emitted when some dialogs needs to be updated.
350 void updateDialog(std::string const & name, std::string const & data);
353 void setGuiDelegate(frontend::GuiBufferViewDelegate *);
356 docstring contentsOfPlaintextFile(support::FileName const & f);
357 // Insert plain text file (if filename is empty, prompt for one)
358 void insertPlaintextFile(support::FileName const & f, bool asParagraph);
360 void insertLyXFile(support::FileName const & f, bool const ignorelang = false);
361 /// save temporary bookmark for jump back navigation
362 void bookmarkEditPosition();
363 /// Find and return the inset associated with given dialog name.
364 Inset * editedInset(std::string const & name) const;
365 /// Associate an inset associated with given dialog name.
366 void editInset(std::string const & name, Inset * inset);
368 void clearLastInset(Inset * inset) const;
369 /// Is the mouse hovering a clickable inset or element?
370 bool clickableInset() const;
372 void makeDocumentClass();
376 BufferView(BufferView const &);
377 void operator=(BufferView const &);
379 /// the position relative to (0, baseline) of outermost paragraph
380 Point coordOffset(DocIterator const & dit) const;
381 /// Update current paragraph metrics.
382 /// \return true if no further update is needed.
383 bool singleParUpdate();
384 /// do the work for the public updateMetrics()
385 void updateMetrics(Update::flags & update_flags);
387 // Set the row on which the cursor lives.
388 void setCurrentRowSlice(CursorSlice const & rowSlice);
390 // Check whether the row where the cursor lives needs to be scrolled.
391 // Update the drawing strategy if needed.
392 void checkCursorScrollOffset();
394 /// The minimal size of the document that is visible. Used
395 /// when it is allowed to scroll below the document.
396 int minVisiblePart();
398 /// Search recursively for the innermost inset that covers (x, y) position.
399 /// \retval 0 if no inset is found.
400 Inset const * getCoveringInset(
401 Text const & text, //< The Text where we start searching.
402 int x, //< x-coordinate on screen
403 int y //< y-coordinate on screen
406 /// Update the hovering status of the insets. This is called when
407 /// either the screen is updated or when the buffer has scolled.
408 void updateHoveredInset() const;
411 void updateDocumentClass(DocumentClassConstPtr olddc);
425 /// some space for drawing the 'nested' markers (in pixel)
426 inline int nestMargin() { return 15; }
428 /// margin for changebar
429 inline int changebarMargin() { return 12; }
433 #endif // BUFFERVIEW_H