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 bool update(Update::flags flags = Update::FitCursor | Update::Force);
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 /// \retval true if a redraw is needed
174 bool workAreaDispatch(FuncRequest const & ev);
176 /// access to anchor.
177 pit_type anchor_ref() const;
180 CursorStatus cursorStatus(DocIterator const & dit) const;
181 /// access to full cursor.
183 /// access to full cursor.
184 Cursor const & cursor() const;
186 /// This will also open all relevant collapsable insets.
187 void setCursor(DocIterator const &);
188 /// Check deleteEmptyParagraphMechanism and update metrics if needed.
189 /// \retval true if an update was needed.
190 bool checkDepm(Cursor & cur, Cursor & old);
192 /// This is used when handling LFUN_MOUSE_PRESS.
193 bool mouseSetCursor(Cursor & cur);
195 /// sets the selection.
196 /* When \c backwards == false, set anchor
197 * to \c cur and cursor to \c cur + \c length. When \c
198 * backwards == true, set anchor to \c cur and cursor to \c
201 void putSelectionAt(DocIterator const & cur,
202 int length, bool backwards);
204 /// return the internal \c ViewMetricsInfo.
205 /// This is used specifically by the \c Workrea.
207 /// \sa ViewMetricsInfo
208 ViewMetricsInfo const & viewMetricsInfo();
209 /// update the internal \c ViewMetricsInfo.
210 /// \param singlepar indicates wether
211 void updateMetrics(bool singlepar = false);
214 TextMetrics const & textMetrics(Text const * t) const;
215 TextMetrics & textMetrics(Text const * t);
217 ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
220 CoordCache & coordCache() { return coord_cache_; }
222 CoordCache const & coordCache() const { return coord_cache_; }
225 Point getPos(DocIterator const & dit, bool boundary) const;
229 void draw(frontend::Painter & pain);
231 /// get this view's keyboard map handler.
232 Intl & getIntl() { return *intl_.get(); }
234 Intl const & getIntl() const { return *intl_.get(); }
237 // Messages to the GUI
239 /// This signal is emitted when some message shows up.
240 void message(docstring const & msg);
242 /// This signal is emitted when some dialog needs to be shown.
243 void showDialog(std::string const & name);
245 /// This signal is emitted when some dialog needs to be shown with
247 void showDialogWithData(std::string const & name, std::string const & data);
249 /// This signal is emitted when some inset dialogs needs to be shown.
250 void showInsetDialog(std::string const & name, std::string const & data,
253 /// This signal is emitted when some dialogs needs to be updated.
254 void updateDialog(std::string const & name, std::string const & data);
257 void setGuiDelegate(frontend::GuiBufferViewDelegate *);
260 docstring contentsOfPlaintextFile(std::string const & f, bool asParagraph);
261 // Insert plain text file (if filename is empty, prompt for one)
262 void insertPlaintextFile(std::string const & fileName, bool asParagraph);
264 // the position relative to (0, baseline) of outermost paragraph
265 Point coordOffset(DocIterator const & dit, bool boundary) const;
266 /// Update current paragraph metrics.
267 /// \return true if no further update is needed.
268 bool singleParUpdate();
272 /// Search recursively for the the innermost inset that covers (x, y) position.
273 /// \retval 0 if no inset is found.
274 Inset const * getCoveringInset(
275 Text const & text, //< The Text where we start searching.
276 int x, //< x-coordinate on screen
277 int y //< y-coordinate on screen
285 ScrollbarParameters scrollbarParameters_;
288 ViewMetricsInfo metrics_info_;
290 CoordCache coord_cache_;
294 /// Estimated average par height for scrollbar.
297 void menuInsertLyXFile(std::string const & filen);
299 /// this is used to handle XSelection events in the right manner.
308 bool multiparsel_cache_;
310 pit_type anchor_ref_;
314 void updateOffsetRef();
316 bool need_centering_;
318 /// keyboard mapping object.
319 boost::scoped_ptr<Intl> const intl_;
321 /// last visited inset (kept to send setMouseHover(false) )
324 /// A map from a Text to the associated text metrics
325 typedef std::map<Text const *, TextMetrics> TextMetricsCache;
326 mutable TextMetricsCache text_metrics_;
328 // Whom to notify. Not owned, so don't delete.
329 frontend::GuiBufferViewDelegate * gui_;
332 /// some space for drawing the 'nested' markers (in pixel)
333 inline int nestMargin() { return 15; }
335 /// margin for changebar
336 inline int changebarMargin() { return 12; }
339 inline int rightMargin() { return 10; }
343 #endif // BUFFERVIEW_H