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"
20 #include "MetricsInfo.h"
21 #include "TextMetrics.h"
22 #include "update_flags.h"
24 #include "support/types.h"
26 #include <boost/noncopyable.hpp>
34 namespace support { class FileName; }
36 namespace frontend { class Painter; }
37 namespace frontend { class GuiBufferViewDelegate; }
48 class ParagraphMetrics;
49 class ViewMetricsInfo;
57 /// Scrollbar Parameters.
58 struct ScrollbarParameters
60 void reset(int h = 0, int p = 0, int l = 0)
67 /// Total document height in pixels.
69 /// Current position in the document in pixels.
71 /// Line-scroll amount in pixels.
75 /// Screen view of a Buffer.
77 * A BufferView encapsulates a view onto a particular
78 * buffer, and allows access to operate upon it. A view
79 * is a sliding window of the entire document rendering.
80 * It is the official interface between the LyX core and
81 * the frontend WorkArea.
87 class BufferView : boost::noncopyable {
90 BufferView(Buffer & buffer);
94 /// return the buffer being viewed.
96 Buffer const & buffer() const;
98 /// perform pending metrics updates.
99 /** \c Update::FitCursor means first to do a FitCursor, and to
100 * force an update if screen position changes.
101 * \c Update::Force means to force an update in any case.
102 * \retval true if a screen redraw is needed
104 void processUpdateFlags(Update::flags flags);
106 /// move the screen to fit the cursor.
107 /// Only to be called with good y coordinates (after a bv::metrics)
109 /// reset the scrollbar to reflect current view position.
110 void updateScrollbar();
111 /// return the Scrollbar Parameters.
112 ScrollbarParameters const & scrollbarParameters() const;
114 /// Save the current position as bookmark.
115 /// if idx == 0, save to temp_bookmark
116 void saveBookmark(unsigned int idx);
117 /// goto a specified position, try top_id first, and then bottom_pit.
118 /// \return true if success
120 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
121 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
122 int top_id, ///< Paragraph ID, \sa Paragraph
123 pos_type top_pos ///< Position in the \c Paragraph
125 /// return the current change at the cursor.
126 Change const getCurrentChange() const;
128 /// move cursor to the named label.
129 void gotoLabel(docstring const & label);
131 /// set the cursor based on the given TeX source row.
132 void setCursorFromRow(int row);
134 /// center the document view around the cursor.
136 /// scroll down document by the given number of pixels.
137 void scrollDown(int pixels);
138 /// scroll up document by the given number of pixels.
139 void scrollUp(int pixels);
140 /// scroll document by the given number of pixels.
141 void scroll(int pixels);
142 /// Scroll the view by a number of pixels.
143 void scrollDocView(int pixels);
144 /// Set the cursor position based on the scrollbar one.
145 void setCursorFromScrollbar();
147 /// return the pixel width of the document view.
148 int workWidth() const;
149 /// return the pixel height of the document view.
150 int workHeight() const;
152 /// translate and insert a character, using the correct keymap.
153 void translateAndInsert(char_type c, Text * t, Cursor & cur);
155 /// return true for events that will handle.
156 FuncStatus getStatus(FuncRequest const & cmd);
157 /// execute the given function.
158 /// \return the Update::flags for further metrics update.
159 Update::flags dispatch(FuncRequest const & argument);
161 /// request an X11 selection.
162 /// \return the selected string.
163 docstring const requestSelection();
164 /// clear the X11 selection.
165 void clearSelection();
167 /// resize the BufferView.
169 void resize(int width, int height);
171 /// dispatch method helper for \c WorkArea
173 void mouseEventDispatch(FuncRequest const & ev);
175 /// access to anchor.
176 pit_type anchor_ref() const;
179 CursorStatus cursorStatus(DocIterator const & dit) const;
180 /// access to full cursor.
182 /// access to full cursor.
183 Cursor const & cursor() const;
185 /// This will also open all relevant collapsable insets.
186 void setCursor(DocIterator const &);
187 /// Check deleteEmptyParagraphMechanism and update metrics if needed.
188 /// \retval true if an update was needed.
189 bool checkDepm(Cursor & cur, Cursor & old);
191 /// This is used when handling LFUN_MOUSE_PRESS.
192 bool mouseSetCursor(Cursor & cur);
194 /// sets the selection.
195 /* When \c backwards == false, set anchor
196 * to \c cur and cursor to \c cur + \c length. When \c
197 * backwards == true, set anchor to \c cur and cursor to \c
200 void putSelectionAt(DocIterator const & cur,
201 int length, bool backwards);
203 /// return the internal \c ViewMetricsInfo.
204 /// This is used specifically by the \c Workrea.
206 /// \sa ViewMetricsInfo
207 ViewMetricsInfo const & viewMetricsInfo();
208 /// update the internal \c ViewMetricsInfo.
209 /// \param singlepar indicates wether
210 void updateMetrics(bool singlepar = false);
213 TextMetrics const & textMetrics(Text const * t) const;
214 TextMetrics & textMetrics(Text const * t);
216 ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
219 CoordCache & coordCache() { return coord_cache_; }
221 CoordCache const & coordCache() const { return coord_cache_; }
224 Point getPos(DocIterator const & dit, bool boundary) const;
228 void draw(frontend::Painter & pain);
230 /// get this view's keyboard map handler.
231 Intl & getIntl() { return *intl_.get(); }
233 Intl const & getIntl() const { return *intl_.get(); }
236 // Messages to the GUI
238 /// This signal is emitted when some message shows up.
239 void message(docstring const & msg);
241 /// This signal is emitted when some dialog needs to be shown.
242 void showDialog(std::string const & name);
244 /// This signal is emitted when some dialog needs to be shown with
246 void showDialogWithData(std::string const & name, std::string const & data);
248 /// This signal is emitted when some inset dialogs needs to be shown.
249 void showInsetDialog(std::string const & name, std::string const & data,
252 /// This signal is emitted when some dialogs needs to be updated.
253 void updateDialog(std::string const & name, std::string const & data);
256 void setGuiDelegate(frontend::GuiBufferViewDelegate *);
259 docstring contentsOfPlaintextFile(std::string const & f, bool asParagraph);
260 // Insert plain text file (if filename is empty, prompt for one)
261 void insertPlaintextFile(std::string const & fileName, bool asParagraph);
263 // the position relative to (0, baseline) of outermost paragraph
264 Point coordOffset(DocIterator const & dit, bool boundary) const;
265 /// Update current paragraph metrics.
266 /// \return true if no further update is needed.
267 bool singleParUpdate();
271 /// Search recursively for the the innermost inset that covers (x, y) position.
272 /// \retval 0 if no inset is found.
273 Inset const * getCoveringInset(
274 Text const & text, //< The Text where we start searching.
275 int x, //< x-coordinate on screen
276 int y //< y-coordinate on screen
284 ScrollbarParameters scrollbarParameters_;
287 ViewMetricsInfo metrics_info_;
289 CoordCache coord_cache_;
293 /// Estimated average par height for scrollbar.
296 void menuInsertLyXFile(std::string const & filen);
298 /// this is used to handle XSelection events in the right manner.
307 bool multiparsel_cache_;
309 pit_type anchor_ref_;
313 void updateOffsetRef();
315 bool need_centering_;
317 /// keyboard mapping object.
318 boost::scoped_ptr<Intl> const intl_;
320 /// last visited inset (kept to send setMouseHover(false) )
323 /// A map from a Text to the associated text metrics
324 typedef std::map<Text const *, TextMetrics> TextMetricsCache;
325 mutable TextMetricsCache text_metrics_;
327 // Whom to notify. Not owned, so don't delete.
328 frontend::GuiBufferViewDelegate * gui_;
331 /// some space for drawing the 'nested' markers (in pixel)
332 inline int nestMargin() { return 15; }
334 /// margin for changebar
335 inline int changebarMargin() { return 12; }
338 inline int rightMargin() { return 10; }
342 #endif // BUFFERVIEW_H