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;
167 /// \return the clickable math inset for the given position
168 Inset const * clickableMathInset(InsetMathNest const * inset,
169 CoordCache::Insets const & inset_cache, int x, int y) const;
171 /// Save the current position as bookmark.
172 /// if idx == 0, save to temp_bookmark
173 void saveBookmark(unsigned int idx);
174 /// goto a specified position, try top_id first, and then bottom_pit.
175 /// \return true if success
177 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
178 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
179 int top_id, ///< Paragraph ID, \sa Paragraph
180 pos_type top_pos ///< Position in the \c Paragraph
182 /// return the current change at the cursor.
183 Change const getCurrentChange() const;
185 /// move cursor to the named label.
186 void gotoLabel(docstring const & label);
188 /// set the cursor based on the given TeX source row.
189 bool setCursorFromRow(int row);
190 /// set the cursor based on the given start and end TextEntries.
191 bool setCursorFromEntries(TexRow::TextEntry start, TexRow::TextEntry end);
193 /// set cursor to the given inset. Return true if found.
194 bool setCursorFromInset(Inset const *);
195 /// Recenters the BufferView such that the passed cursor
196 /// is in the center.
198 /// Ensure that the BufferView cursor is visible.
199 /// This method will automatically scroll and update the BufferView
200 /// (metrics+drawing) if needed.
202 /// Ensure the passed cursor \p dit is visible.
203 /// This method will automatically scroll and update the BufferView
204 /// (metrics+drawing) if needed.
205 /// \param recenter Whether the cursor should be centered on screen
206 void showCursor(DocIterator const & dit, bool recenter,
208 /// Scroll to the cursor.
209 void scrollToCursor();
210 /// Scroll to the cursor.
211 /// \param recenter Whether the cursor should be centered on screen
212 bool scrollToCursor(DocIterator const & dit, bool recenter);
213 /// scroll down document by the given number of pixels.
214 int scrollDown(int pixels);
215 /// scroll up document by the given number of pixels.
216 int scrollUp(int pixels);
217 /// scroll document by the given number of pixels.
218 int scroll(int pixels);
219 /// Scroll the view by a number of pixels.
220 void scrollDocView(int pixels, bool update);
221 /// Set the cursor position based on the scrollbar one.
222 void setCursorFromScrollbar();
224 /// return the pixel width of the document view.
225 int workWidth() const;
226 /// return the pixel height of the document view.
227 int workHeight() const;
229 /// return the inline completion postfix.
230 docstring const & inlineCompletion() const;
231 /// return the number of unique characters in the inline completion.
232 size_t inlineCompletionUniqueChars() const;
233 /// return the position in the buffer of the inline completion postfix.
234 DocIterator const & inlineCompletionPos() const;
235 /// make sure inline completion position is OK
236 void resetInlineCompletionPos();
237 /// set the inline completion postfix and its position in the buffer.
238 /// Updates the updateFlags in \c cur.
239 void setInlineCompletion(Cursor const & cur, DocIterator const & pos,
240 docstring const & completion, size_t uniqueChars = 0);
242 /// translate and insert a character, using the correct keymap.
243 void translateAndInsert(char_type c, Text * t, Cursor & cur);
245 /// \return true if we've made a decision
246 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
247 /// execute the given function.
248 void dispatch(FuncRequest const & cmd, DispatchResult & dr);
250 /// request an X11 selection.
251 /// \return the selected string.
252 docstring requestSelection();
253 /// clear the X11 selection.
254 void clearSelection();
256 /// resize the BufferView.
258 void resize(int width, int height);
260 /// dispatch method helper for \c WorkArea
262 void mouseEventDispatch(FuncRequest const & ev);
265 CursorStatus cursorStatus(DocIterator const & dit) const;
266 /// access to full cursor.
268 /// access to full cursor.
269 Cursor const & cursor() const;
271 /// This will also open all relevant collapsible insets.
272 void setCursor(DocIterator const &);
273 /// set the selection up to dit.
274 void setCursorSelectionTo(DocIterator const & dit);
275 /// Check deleteEmptyParagraphMechanism and update metrics if needed.
276 /// \retval true if an update was needed.
277 bool checkDepm(Cursor & cur, Cursor & old);
279 /// This is used when handling LFUN_MOUSE_PRESS.
280 bool mouseSetCursor(Cursor & cur, bool select = false);
282 /// sets the selection.
283 /* When \c backwards == false, set anchor
284 * to \c cur and cursor to \c cur + \c length. When \c
285 * backwards == true, set anchor to \c cur and cursor to \c
288 void putSelectionAt(DocIterator const & cur,
289 int length, bool backwards);
291 /// selects the item at cursor if its paragraph is empty.
292 bool selectIfEmpty(DocIterator & cur);
294 /// update the internal \c ViewMetricsInfo.
295 void updateMetrics();
297 // this is the "nodraw" drawing stage: only set the positions of the
298 // insets in metrics cache.
299 void updatePosCache();
302 TextMetrics const & textMetrics(Text const * t) const;
303 TextMetrics & textMetrics(Text const * t);
305 ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
308 CoordCache & coordCache();
310 CoordCache const & coordCache() const;
313 MathRow const & mathRow(MathData const * cell) const;
315 void setMathRow(MathData const * cell, MathRow const & mrow);
318 Point getPos(DocIterator const & dit) const;
319 /// is the paragraph of the cursor visible ?
320 bool paragraphVisible(DocIterator const & dit) const;
321 /// is the caret currently visible in the view
322 bool caretInView() const;
323 /// get the position and height of the caret
324 void caretPosAndDim(Point & p, Dimension & dim) const;
325 /// compute the shape of the caret
326 void buildCaretGeometry(bool complet);
327 /// the shape of the caret
328 frontend::CaretGeometry const & caretGeometry() const;
331 void draw(frontend::Painter & pain, bool paint_caret);
333 /// get this view's keyboard map handler.
336 Intl const & getIntl() const;
339 // Messages to the GUI
341 /// This signal is emitted when some message shows up.
342 void message(docstring const & msg);
344 /// This signal is emitted when some dialog needs to be shown.
345 void showDialog(std::string const & name);
347 /// This signal is emitted when some dialog needs to be shown with
349 void showDialog(std::string const & name, std::string const & data,
350 Inset * inset = nullptr);
352 /// This signal is emitted when some dialogs needs to be updated.
353 void updateDialog(std::string const & name, std::string const & data);
356 void setGuiDelegate(frontend::GuiBufferViewDelegate *);
359 docstring contentsOfPlaintextFile(support::FileName const & f);
360 // Insert plain text file (if filename is empty, prompt for one)
361 void insertPlaintextFile(support::FileName const & f, bool asParagraph);
363 void insertLyXFile(support::FileName const & f, bool const ignorelang = false);
364 /// save temporary bookmark for jump back navigation
365 void bookmarkEditPosition();
366 /// Find and return the inset associated with given dialog name.
367 Inset * editedInset(std::string const & name) const;
368 /// Associate an inset associated with given dialog name.
369 void editInset(std::string const & name, Inset * inset);
371 void clearLastInset(Inset * inset) const;
372 /// Is the mouse hovering a clickable inset or element?
373 bool clickableInset() const;
375 void makeDocumentClass();
379 BufferView(BufferView const &);
380 void operator=(BufferView const &);
382 /// the position relative to (0, baseline) of outermost paragraph
383 Point coordOffset(DocIterator const & dit) const;
384 /// Update current paragraph metrics.
385 /// \return true if no further update is needed.
386 bool singleParUpdate();
387 /// do the work for the public updateMetrics()
388 void updateMetrics(Update::flags & update_flags);
390 // Set the row on which the cursor lives.
391 void setCurrentRowSlice(CursorSlice const & rowSlice);
393 // Check whether the row where the cursor lives needs to be scrolled.
394 // Update the drawing strategy if needed.
395 void checkCursorScrollOffset();
397 /// The minimal size of the document that is visible. Used
398 /// when it is allowed to scroll below the document.
399 int minVisiblePart();
401 /// Search recursively for the innermost inset that covers (x, y) position.
402 /// \retval 0 if no inset is found.
403 Inset const * getCoveringInset(
404 Text const & text, //< The Text where we start searching.
405 int x, //< x-coordinate on screen
406 int y //< y-coordinate on screen
409 /// Update the hovering status of the insets. This is called when
410 /// either the screen is updated or when the buffer has scolled.
411 void updateHoveredInset() const;
414 void updateDocumentClass(DocumentClassConstPtr olddc);
428 /// some space for drawing the 'nested' markers (in pixel)
429 inline int nestMargin() { return 15; }
431 /// margin for changebar
432 inline int changebarMargin() { return 12; }
436 #endif // BUFFERVIEW_H