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 "DocumentClassPtr.h"
20 #include "update_flags.h"
22 #include "support/strfwd.h"
23 #include "support/types.h"
27 namespace support { class FileName; }
29 namespace frontend { class Painter; }
30 namespace frontend { class GuiBufferViewDelegate; }
46 class ParagraphMetrics;
59 /// Scrollbar Parameters.
60 struct ScrollbarParameters
62 // These parameters are normalized against the screen geometry and pixel
63 // coordinates. Position 0 corresponds to the top the the screen.
65 : min(0), max(0), single_step(1), page_step(1)
67 /// Minimum scrollbar position in pixels.
69 /// Maximum scrollbar position in pixels.
71 /// Line-scroll amount in pixels.
73 /// Page-scroll amount in pixels.
77 /// Screen view of a Buffer.
79 * A BufferView encapsulates a view onto a particular
80 * buffer, and allows access to operate upon it. A view
81 * is a sliding window of the entire document rendering.
82 * It is the official interface between the LyX core and
83 * the frontend WorkArea.
92 explicit BufferView(Buffer & buffer);
96 /// return the buffer being viewed.
98 Buffer const & buffer() const;
101 void setFullScreen(bool full_screen) { full_screen_ = full_screen; }
104 int rightMargin() const;
107 int leftMargin() const;
109 /// return the on-screen size of this length
111 * This is a wrapper around Length::inPixels that uses the
112 * bufferview width as width and the EM value of the default
115 int inPixels(Length const & len) const;
117 /// \return true if the BufferView is at the top of the document.
118 bool isTopScreen() const;
120 /// \return true if the BufferView is at the bottom of the document.
121 bool isBottomScreen() const;
123 /// Add \p flags to current update flags and trigger an update.
124 /* If this method is invoked several times before the update
125 * actually takes place, the effect is cumulative.
126 * \c Update::FitCursor means first to do a FitCursor, and to
127 * force an update if screen position changes.
128 * \c Update::Force means to force an update in any case.
130 void processUpdateFlags(Update::flags flags);
132 /// return true if one shall move the screen to fit the cursor.
133 /// Only to be called with good y coordinates (after a bv::metrics)
134 bool needsFitCursor() const;
136 /// returns true if this row needs to be repainted (to erase caret)
137 bool needRepaint(Text const * text, Row const & row) const;
139 // Returns the amount of horizontal scrolling applied to the
140 // top-level row where the cursor lies
141 int horizScrollOffset() const;
142 // Returns the amount of horizontal scrolling applied to the
143 // row of text starting at (pit, pos)
144 int horizScrollOffset(Text const * text,
145 pit_type pit, pos_type pos) const;
147 // Returns true if the row of text starting at (pit, pos) was scrolled
148 // at the last draw event.
149 bool hadHorizScrollOffset(Text const * text,
150 pit_type pit, pos_type pos) const;
152 /// reset the scrollbar to reflect current view position.
153 void updateScrollbar();
154 /// return the Scrollbar Parameters.
155 ScrollbarParameters const & scrollbarParameters() const;
156 /// \return Tool tip for the given position.
157 docstring toolTip(int x, int y) const;
158 /// \return the context menu for the given position.
159 std::string contextMenu(int x, int y) const;
161 /// Save the current position as bookmark.
162 /// if idx == 0, save to temp_bookmark
163 void saveBookmark(unsigned int idx);
164 /// goto a specified position, try top_id first, and then bottom_pit.
165 /// \return true if success
167 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
168 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
169 int top_id, ///< Paragraph ID, \sa Paragraph
170 pos_type top_pos ///< Position in the \c Paragraph
172 /// return the current change at the cursor.
173 Change const getCurrentChange() const;
175 /// move cursor to the named label.
176 void gotoLabel(docstring const & label);
178 /// set the cursor based on the given TeX source row.
179 bool setCursorFromRow(int row);
180 /// set the cursor based on the given start and end TextEntries.
181 bool setCursorFromEntries(TexRow::TextEntry start, TexRow::TextEntry end);
183 /// set cursor to the given inset. Return true if found.
184 bool setCursorFromInset(Inset const *);
185 /// Recenters the BufferView such that the passed cursor
186 /// is in the center.
188 /// Ensure that the BufferView cursor is visible.
189 /// This method will automatically scroll and update the BufferView
190 /// (metrics+drawing) if needed.
192 /// Ensure the passed cursor \p dit is visible.
193 /// This method will automatically scroll and update the BufferView
194 /// (metrics+drawing) if needed.
195 /// \param recenter Whether the cursor should be centered on screen
196 void showCursor(DocIterator const & dit, bool recenter,
198 /// Scroll to the cursor.
199 void scrollToCursor();
200 /// Scroll to the cursor.
201 /// \param recenter Whether the cursor should be centered on screen
202 bool scrollToCursor(DocIterator const & dit, bool recenter);
203 /// scroll down document by the given number of pixels.
204 int scrollDown(int pixels);
205 /// scroll up document by the given number of pixels.
206 int scrollUp(int pixels);
207 /// scroll document by the given number of pixels.
208 int scroll(int pixels);
209 /// Scroll the view by a number of pixels.
210 void scrollDocView(int pixels, bool update);
211 /// Set the cursor position based on the scrollbar one.
212 void setCursorFromScrollbar();
214 /// return the pixel width of the document view.
215 int workWidth() const;
216 /// return the pixel height of the document view.
217 int workHeight() const;
219 /// return the inline completion postfix.
220 docstring const & inlineCompletion() const;
221 /// return the number of unique characters in the inline completion.
222 size_t const & inlineCompletionUniqueChars() const;
223 /// return the position in the buffer of the inline completion postfix.
224 DocIterator const & inlineCompletionPos() const;
225 /// make sure inline completion position is OK
226 void resetInlineCompletionPos();
227 /// set the inline completion postfix and its position in the buffer.
228 /// Updates the updateFlags in \c cur.
229 void setInlineCompletion(Cursor const & cur, DocIterator const & pos,
230 docstring const & completion, size_t uniqueChars = 0);
232 /// translate and insert a character, using the correct keymap.
233 void translateAndInsert(char_type c, Text * t, Cursor & cur);
235 /// \return true if we've made a decision
236 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
237 /// execute the given function.
238 void dispatch(FuncRequest const & cmd, DispatchResult & dr);
240 /// request an X11 selection.
241 /// \return the selected string.
242 docstring const requestSelection();
243 /// clear the X11 selection.
244 void clearSelection();
246 /// resize the BufferView.
248 void resize(int width, int height);
250 /// dispatch method helper for \c WorkArea
252 void mouseEventDispatch(FuncRequest const & ev);
254 /// access to anchor.
255 pit_type anchor_ref() const;
258 CursorStatus cursorStatus(DocIterator const & dit) const;
259 /// access to full cursor.
261 /// access to full cursor.
262 Cursor const & cursor() const;
264 /// This will also open all relevant collapsible insets.
265 void setCursor(DocIterator const &);
266 /// set the selection up to dit.
267 void setCursorSelectionTo(DocIterator const & dit);
268 /// Check deleteEmptyParagraphMechanism and update metrics if needed.
269 /// \retval true if an update was needed.
270 bool checkDepm(Cursor & cur, Cursor & old);
272 /// This is used when handling LFUN_MOUSE_PRESS.
273 bool mouseSetCursor(Cursor & cur, bool select = false);
275 /// sets the selection.
276 /* When \c backwards == false, set anchor
277 * to \c cur and cursor to \c cur + \c length. When \c
278 * backwards == true, set anchor to \c cur and cursor to \c
281 void putSelectionAt(DocIterator const & cur,
282 int length, bool backwards);
284 /// selects the item at cursor if its paragraph is empty.
285 bool selectIfEmpty(DocIterator & cur);
287 /// update the internal \c ViewMetricsInfo.
288 void updateMetrics();
290 // this is the "nodraw" drawing stage: only set the positions of the
291 // insets in metrics cache.
292 void updatePosCache();
295 TextMetrics const & textMetrics(Text const * t) const;
296 TextMetrics & textMetrics(Text const * t);
298 ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
301 CoordCache & coordCache();
303 CoordCache const & coordCache() const;
306 Point getPos(DocIterator const & dit) const;
307 /// is the paragraph of the cursor visible ?
308 bool paragraphVisible(DocIterator const & dit) const;
309 /// is the cursor currently visible in the view
310 bool cursorInView(Point const & p, int h) const;
311 /// get the position and height of the caret
312 void caretPosAndHeight(Point & p, int & h) const;
315 void draw(frontend::Painter & pain, bool paint_caret);
317 /// get this view's keyboard map handler.
320 Intl const & getIntl() const;
323 // Messages to the GUI
325 /// This signal is emitted when some message shows up.
326 void message(docstring const & msg);
328 /// This signal is emitted when some dialog needs to be shown.
329 void showDialog(std::string const & name);
331 /// This signal is emitted when some dialog needs to be shown with
333 void showDialog(std::string const & name, std::string const & data,
336 /// This signal is emitted when some dialogs needs to be updated.
337 void updateDialog(std::string const & name, std::string const & data);
340 void setGuiDelegate(frontend::GuiBufferViewDelegate *);
343 docstring contentsOfPlaintextFile(support::FileName const & f);
344 // Insert plain text file (if filename is empty, prompt for one)
345 void insertPlaintextFile(support::FileName const & f, bool asParagraph);
347 void insertLyXFile(support::FileName const & f);
348 /// save temporary bookmark for jump back navigation
349 void bookmarkEditPosition();
350 /// Find and return the inset associated with given dialog name.
351 Inset * editedInset(std::string const & name) const;
352 /// Associate an inset associated with given dialog name.
353 void editInset(std::string const & name, Inset * inset);
355 void clearLastInset(Inset * inset) const;
356 /// Is the mouse hovering a clickable inset or element?
357 bool clickableInset() const;
359 void makeDocumentClass();
363 BufferView(BufferView const &);
364 void operator=(BufferView const &);
366 /// the position relative to (0, baseline) of outermost paragraph
367 Point coordOffset(DocIterator const & dit) const;
368 /// Update current paragraph metrics.
369 /// \return true if no further update is needed.
370 bool singleParUpdate();
371 /// do the work for the public updateMetrics()
372 void updateMetrics(Update::flags & update_flags);
374 // Set the row on which the cursor lives.
375 void setCurrentRowSlice(CursorSlice const & rowSlice);
377 // Check whether the row where the cursor lives needs to be scrolled.
378 // Update the drawing strategy if needed.
379 void checkCursorScrollOffset();
381 /// The minimal size of the document that is visible. Used
382 /// when it is allowed to scroll below the document.
383 int minVisiblePart();
385 /// Search recursively for the innermost inset that covers (x, y) position.
386 /// \retval 0 if no inset is found.
387 Inset const * getCoveringInset(
388 Text const & text, //< The Text where we start searching.
389 int x, //< x-coordinate on screen
390 int y //< y-coordinate on screen
393 /// Update the hovering status of the insets. This is called when
394 /// either the screen is updated or when the buffer has scolled.
395 void updateHoveredInset() const;
398 void updateDocumentClass(DocumentClassConstPtr olddc);
412 /// some space for drawing the 'nested' markers (in pixel)
413 inline int nestMargin() { return 15; }
415 /// margin for changebar
416 inline int changebarMargin() { return 12; }
420 #endif // BUFFERVIEW_H