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