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 /// How to show cursor
62 // Make sure row if visible (do nothing if it is visible already)
64 // Force cursor to be on top of screen
66 // Force cursor to be at center of screen
70 /// Scrollbar Parameters.
71 struct ScrollbarParameters
73 // These parameters are normalized against the screen geometry and pixel
74 // coordinates. Position 0 corresponds to the top the the screen.
76 : min(0), max(0), single_step(1), page_step(1)
78 /// Minimum scrollbar position in pixels.
80 /// Maximum scrollbar position in pixels.
82 /// Line-scroll amount in pixels.
84 /// Page-scroll amount in pixels.
88 /// Screen view of a Buffer.
90 * A BufferView encapsulates a view onto a particular
91 * buffer, and allows access to operate upon it. A view
92 * is a sliding window of the entire document rendering.
93 * It is the official interface between the LyX core and
94 * the frontend WorkArea.
103 explicit BufferView(Buffer & buffer);
107 /// return the buffer being viewed.
109 Buffer const & buffer() const;
112 void setFullScreen(bool full_screen) { full_screen_ = full_screen; }
114 /// default value for the margins
115 int defaultMargin() const;
117 int rightMargin() const;
119 int leftMargin() const;
121 int topMargin() const;
123 int bottomMargin() const;
125 docstring const & searchRequestCache() const;
126 void setSearchRequestCache(docstring const & text);
128 /// return the on-screen size of this length
130 * This is a wrapper around Length::inPixels that uses the
131 * bufferview width as width and the EM value of the default
134 int inPixels(Length const & len) const;
136 /** Return the number of pixels equivalent to \c pix pixels at
137 * 100dpi and 100% zoom.
139 int zoomedPixels(int pix) const;
141 /// \return true if the BufferView is at the top of the document.
142 bool isTopScreen() const;
144 /// \return true if the BufferView is at the bottom of the document.
145 bool isBottomScreen() const;
147 /// Add \p flags to current update flags and trigger an update.
148 /* If this method is invoked several times before the update
149 * actually takes place, the effect is cumulative.
150 * \c Update::FitCursor means first to do a FitCursor, and to
151 * force an update if screen position changes.
152 * \c Update::Force means to force an update in any case.
154 void processUpdateFlags(Update::flags flags);
156 /// return true if one shall move the screen to fit the cursor.
157 /// Only to be called with good y coordinates (after a bv::metrics)
158 bool needsFitCursor() const;
160 // Returns the amount of horizontal scrolling applied to the
161 // top-level row where the cursor lies
162 int horizScrollOffset() const;
163 // Returns the amount of horizontal scrolling applied to the
164 // row of text starting at (pit, pos)
165 int horizScrollOffset(Text const * text,
166 pit_type pit, pos_type pos) const;
168 /// reset the scrollbar parameters to reflect current view position.
169 void updateScrollbarParameters();
170 /// return the Scrollbar Parameters.
171 ScrollbarParameters const & scrollbarParameters() const;
172 /// \return Tool tip for the given position.
173 docstring toolTip(int x, int y) const;
174 /// \return the context menu for the given position.
175 std::string contextMenu(int x, int y) const;
176 /// \return the math inset with a context menu for the given position
177 Inset const * mathContextMenu(InsetMathNest const * inset,
178 CoordCache::Insets const & inset_cache, int x, int y) const;
179 /// \return the clickable math inset for the given position
180 Inset const * clickableMathInset(InsetMathNest const * inset,
181 CoordCache::Insets const & inset_cache, int x, int y) const;
183 /// Save the current position as bookmark.
184 /// if idx == 0, save to temp_bookmark
185 void saveBookmark(unsigned int idx);
186 /// goto a specified position, try top_id first, and then bottom_pit.
187 /// \return true if success
189 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
190 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
191 int top_id, ///< Paragraph ID, \sa Paragraph
192 pos_type top_pos ///< Position in the \c Paragraph
194 /// return the current change at the cursor.
195 Change const getCurrentChange() const;
197 /// move cursor to the named label.
198 void gotoLabel(docstring const & label);
200 /// set the cursor based on the given TeX source row.
201 bool setCursorFromRow(int row);
202 /// set the cursor based on the given start and end TextEntries.
203 bool setCursorFromEntries(TexRow::TextEntry start, TexRow::TextEntry end);
205 /// set cursor to the given inset. Return true if found.
206 bool setCursorFromInset(Inset const *);
207 /// Recenters the BufferView such that the passed cursor
208 /// is in the center.
210 /// Ensure that the BufferView cursor is visible.
211 /// This method will automatically scroll and update the BufferView
212 /// (metrics+drawing) if needed.
215 /// Ensure the passed cursor \p dit is visible.
216 /// This method will automatically scroll and update the BufferView
217 /// (metrics+drawing) if needed.
218 /// \param how Use this scroll strategy
219 /// \param force If true, update screen after scrolling
220 void showCursor(DocIterator const & dit, ScrollType how, bool update);
221 /// Scroll to the cursor.
222 /// \param how Use this scroll strategy
223 /// \return true if screen was scrolled
224 bool scrollToCursor(DocIterator const & dit, ScrollType how);
225 /// scroll down document by the given number of pixels.
226 int scrollDown(int pixels);
227 /// scroll up document by the given number of pixels.
228 int scrollUp(int pixels);
229 /// scroll document by the given number of pixels.
230 int scroll(int pixels);
231 /// Scroll the view by a number of pixels.
232 void scrollDocView(int pixels, bool update);
233 /// Set the cursor position based on the scrollbar one.
234 void setCursorFromScrollbar();
236 /// return the pixel width of the document view.
237 int workWidth() const;
238 /// return the pixel height of the document view.
239 int workHeight() const;
241 /// return the inline completion postfix.
242 docstring const & inlineCompletion() const;
243 /// return the number of unique characters in the inline completion.
244 size_t inlineCompletionUniqueChars() const;
245 /// return the position in the buffer of the inline completion postfix.
246 DocIterator const & inlineCompletionPos() const;
247 /// make sure inline completion position is OK
248 void resetInlineCompletionPos();
249 /// set the inline completion postfix and its position in the buffer.
250 /// Updates the updateFlags in \c cur.
251 void setInlineCompletion(Cursor const & cur, DocIterator const & pos,
252 docstring const & completion, size_t uniqueChars = 0);
254 /// translate and insert a character, using the correct keymap.
255 void translateAndInsert(char_type c, Text * t, Cursor & cur);
257 /// \return true if we've made a decision
258 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
259 /// execute the given function.
260 void dispatch(FuncRequest const & cmd, DispatchResult & dr);
262 /// request an X11 selection.
263 /// \return the selected string.
264 docstring requestSelection();
265 /// clear the X11 selection.
266 void clearSelection();
268 /// resize the BufferView.
270 void resize(int width, int height);
272 /// dispatch method helper for \c WorkArea
274 void mouseEventDispatch(FuncRequest const & ev);
277 CursorStatus cursorStatus(DocIterator const & dit) const;
278 /// access to full cursor.
280 /// access to full cursor.
281 Cursor const & cursor() const;
283 /// This will also open all relevant collapsible insets.
284 void setCursor(DocIterator const &);
285 /// set the selection up to dit.
286 void setCursorSelectionTo(DocIterator const & dit);
287 /// Check deleteEmptyParagraphMechanism and update metrics if needed.
288 /// \retval true if an update was needed.
289 bool checkDepm(Cursor & cur, Cursor & old);
291 /// This is used when handling LFUN_MOUSE_PRESS.
292 bool mouseSetCursor(Cursor & cur, bool select = false);
294 /// sets the selection.
295 /* When \c backwards == false, set anchor
296 * to \c cur and cursor to \c cur + \c length. When \c
297 * backwards == true, set anchor to \c cur and cursor to \c
300 void putSelectionAt(DocIterator const & cur,
301 int length, bool backwards);
303 /// selects the item at cursor if its paragraph is empty.
304 bool selectIfEmpty(DocIterator & cur);
306 /// update the internal \c ViewMetricsInfo.
307 void updateMetrics();
309 // this is the "nodraw" drawing stage: only set the positions of the
310 // insets in metrics cache.
311 void updatePosCache();
314 TextMetrics const & textMetrics(Text const * t) const;
315 TextMetrics & textMetrics(Text const * t);
317 ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
320 CoordCache & coordCache();
322 CoordCache const & coordCache() const;
325 MathRow const & mathRow(MathData const * cell) const;
327 void setMathRow(MathData const * cell, MathRow const & mrow);
330 Point getPos(DocIterator const & dit) const;
331 /// is the paragraph of the cursor visible ?
332 bool paragraphVisible(DocIterator const & dit) const;
333 /// is the caret currently visible in the view
334 bool caretInView() const;
335 /// get the position and height of the caret
336 void caretPosAndDim(Point & p, Dimension & dim) const;
337 /// compute the shape of the caret
338 void buildCaretGeometry(bool complet);
339 /// the shape of the caret
340 frontend::CaretGeometry const & caretGeometry() const;
343 void draw(frontend::Painter & pain, bool paint_caret);
345 /// get this view's keyboard map handler.
348 Intl const & getIntl() const;
351 // Messages to the GUI
353 /// This signal is emitted when some message shows up.
354 void message(docstring const & msg);
356 /// This signal is emitted when some dialog needs to be shown.
357 void showDialog(std::string const & name);
359 /// This signal is emitted when some dialog needs to be shown with
361 void showDialog(std::string const & name, std::string const & data,
362 Inset * inset = nullptr);
364 /// This signal is emitted when some dialogs needs to be updated.
365 void updateDialog(std::string const & name, std::string const & data);
368 void setGuiDelegate(frontend::GuiBufferViewDelegate *);
371 docstring contentsOfPlaintextFile(support::FileName const & f);
372 // Insert plain text file (if filename is empty, prompt for one)
373 void insertPlaintextFile(support::FileName const & f, bool asParagraph);
375 void insertLyXFile(support::FileName const & f, bool const ignorelang = false);
376 /// save temporary bookmark for jump back navigation
377 void bookmarkEditPosition();
378 /// Find and return the inset associated with given dialog name.
379 Inset * editedInset(std::string const & name) const;
380 /// Associate an inset associated with given dialog name.
381 void editInset(std::string const & name, Inset * inset);
383 void clearLastInset(Inset * inset) const;
384 /// Is the mouse hovering a clickable inset or element?
385 bool clickableInset() const;
387 void makeDocumentClass();
388 /// Are we currently performing a selection with the mouse?
389 bool mouseSelecting() const;
393 BufferView(BufferView const &);
394 void operator=(BufferView const &);
396 /// the position relative to (0, baseline) of outermost paragraph
397 Point coordOffset(DocIterator const & dit) const;
398 /// Update current paragraph metrics.
399 /// \return true if no further update is needed.
400 bool singleParUpdate();
401 /// do the work for the public updateMetrics()
402 void updateMetrics(Update::flags & update_flags);
404 // Set the row on which the cursor lives.
405 void setCurrentRowSlice(CursorSlice const & rowSlice);
407 // Check whether the row where the cursor lives needs to be scrolled.
408 // Update the drawing strategy if needed.
409 void checkCursorScrollOffset();
411 /// The minimal size of the document that is visible. Used
412 /// when it is allowed to scroll below the document.
413 int minVisiblePart();
415 /// Search recursively for the innermost inset that covers (x, y) position.
416 /// \retval 0 if no inset is found.
417 Inset const * getCoveringInset(
418 Text const & text, //< The Text where we start searching.
419 int x, //< x-coordinate on screen
420 int y //< y-coordinate on screen
423 /// Update the hovering status of the insets. This is called when
424 /// either the screen is updated or when the buffer has scolled.
425 void updateHoveredInset() const;
428 void updateDocumentClass(DocumentClassConstPtr olddc);
442 /// some space for drawing the 'nested' markers (in pixel)
443 inline int nestMargin() { return 15; }
445 /// margin for changebar
446 inline int changebarMargin() { return 12; }
450 #endif // BUFFERVIEW_H