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/tuple/tuple.hpp>
27 #include <boost/noncopyable.hpp>
28 #include <boost/signal.hpp>
36 namespace support { class FileName; }
38 namespace frontend { class Painter; }
49 class ParagraphMetrics;
50 class ViewMetricsInfo;
58 /// Scrollbar Parameters.
59 struct ScrollbarParameters
61 void reset(int h = 0, int p = 0, int l = 0)
68 /// Total document height in pixels.
70 /// Current position in the document in pixels.
72 /// Line-scroll amount in pixels.
76 /// Screen view of a Buffer.
78 * A BufferView encapsulates a view onto a particular
79 * buffer, and allows access to operate upon it. A view
80 * is a sliding window of the entire document rendering.
81 * It is the official interface between the LyX core and
82 * the frontend WorkArea.
88 class BufferView : boost::noncopyable {
91 BufferView(Buffer & buffer);
95 /// return the buffer being viewed.
97 Buffer const & buffer() const;
99 /// perform pending metrics updates.
100 /** \c Update::FitCursor means first to do a FitCursor, and to
101 * force an update if screen position changes.
102 * \c Update::Force means to force an update in any case.
103 * \retval true if a screen redraw is needed
105 bool update(Update::flags flags = Update::FitCursor | Update::Force);
107 /// move the screen to fit the cursor.
108 /// Only to be called with good y coordinates (after a bv::metrics)
110 /// reset the scrollbar to reflect current view position.
111 void updateScrollbar();
112 /// return the Scrollbar Parameters.
113 ScrollbarParameters const & scrollbarParameters() const;
115 /// Save the current position as bookmark.
116 /// if idx == 0, save to temp_bookmark
117 void saveBookmark(unsigned int idx);
118 /// goto a specified position, try top_id first, and then bottom_pit.
119 /// \return true if success
121 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
122 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
123 int top_id, ///< Paragraph ID, \sa Paragraph
124 pos_type top_pos ///< Position in the \c Paragraph
126 /// return the current change at the cursor.
127 Change const getCurrentChange() const;
129 /// move cursor to the named label.
130 void gotoLabel(docstring const & label);
132 /// set the cursor based on the given TeX source row.
133 void setCursorFromRow(int row);
135 /// center the document view around the cursor.
137 /// scroll down document by the given number of pixels.
138 void scrollDown(int pixels);
139 /// scroll up document by the given number of pixels.
140 void scrollUp(int pixels);
141 /// scroll document by the given number of pixels.
142 void scroll(int pixels);
143 /// Scroll the view by a number of pixels.
144 void scrollDocView(int pixels);
145 /// Set the cursor position based on the scrollbar one.
146 void setCursorFromScrollbar();
148 /// return the pixel width of the document view.
149 int workWidth() const;
150 /// return the pixel height of the document view.
151 int workHeight() const;
153 /// translate and insert a character, using the correct keymap.
154 void translateAndInsert(char_type c, Text * t, Cursor & cur);
156 /// return true for events that will handle.
157 FuncStatus getStatus(FuncRequest const & cmd);
158 /// execute the given function.
159 /// \return the Update::flags for further metrics update.
160 Update::flags dispatch(FuncRequest const & argument);
162 /// request an X11 selection.
163 /// \return the selected string.
164 docstring const requestSelection();
165 /// clear the X11 selection.
166 void clearSelection();
168 /// resize the BufferView.
170 void resize(int width, int height);
172 /// dispatch method helper for \c WorkArea
174 /// \retval true if a redraw is needed
175 bool workAreaDispatch(FuncRequest const & ev);
177 /// access to anchor.
178 pit_type anchor_ref() const;
181 CursorStatus cursorStatus(DocIterator const & dit) const;
182 /// access to full cursor.
184 /// access to full cursor.
185 Cursor const & cursor() const;
187 /// This will also open all relevant collapsable insets.
188 void setCursor(DocIterator const &);
189 /// Check deleteEmptyParagraphMechanism and update metrics if needed.
190 /// \retval true if an update was needed.
191 bool checkDepm(Cursor & cur, Cursor & old);
193 /// This is used when handling LFUN_MOUSE_PRESS.
194 bool mouseSetCursor(Cursor & cur);
196 /// sets the selection.
197 /* When \c backwards == false, set anchor
198 * to \c cur and cursor to \c cur + \c length. When \c
199 * backwards == true, set anchor to \c cur and cursor to \c
202 void putSelectionAt(DocIterator const & cur,
203 int length, bool backwards);
205 /// return the internal \c ViewMetricsInfo.
206 /// This is used specifically by the \c Workrea.
208 /// \sa ViewMetricsInfo
209 ViewMetricsInfo const & viewMetricsInfo();
210 /// update the internal \c ViewMetricsInfo.
211 /// \param singlepar indicates wether
212 void updateMetrics(bool singlepar = false);
215 TextMetrics const & textMetrics(Text const * t) const;
216 TextMetrics & textMetrics(Text const * t);
218 ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
221 CoordCache & coordCache() { return coord_cache_; }
223 CoordCache const & coordCache() const { return coord_cache_; }
226 Point getPos(DocIterator const & dit, bool boundary) const;
230 void draw(frontend::Painter & pain);
232 /// get this view's keyboard map handler.
233 Intl & getIntl() { return *intl_.get(); }
235 Intl const & getIntl() const { return *intl_.get(); }
237 /// This signal is emitted when some message shows up.
238 boost::signal<void(docstring)> message;
240 /// This signal is emitted when some dialog needs to be shown.
241 boost::signal<void(std::string name)> showDialog;
243 /// This signal is emitted when some dialog needs to be shown with
245 boost::signal<void(std::string name,
246 std::string data)> showDialogWithData;
248 /// This signal is emitted when some inset dialogs needs to be shown.
249 boost::signal<void(std::string name, std::string data,
250 Inset * inset)> showInsetDialog;
252 /// This signal is emitted when some dialogs needs to be updated.
253 boost::signal<void(std::string name,
254 std::string data)> updateDialog;
256 /// This signal is emitted when the layout at the cursor is changed.
257 boost::signal<void(docstring layout)> layoutChanged;
260 // the position relative to (0, baseline) of outermost paragraph
261 Point coordOffset(DocIterator const & dit, bool boundary) const;
262 /// Update current paragraph metrics.
263 /// \return true if no further update is needed.
264 bool singleParUpdate();
268 /// Search recursively for the the innermost inset that covers (x, y) position.
269 /// \retval 0 if no inset is found.
270 Inset const * getCoveringInset(
271 Text const & text, //< The Text where we start searching.
272 int x, //< x-coordinate on screen
273 int y //< y-coordinate on screen
281 ScrollbarParameters scrollbarParameters_;
284 ViewMetricsInfo metrics_info_;
286 CoordCache coord_cache_;
290 /// Estimated average par height for scrollbar.
293 void menuInsertLyXFile(std::string const & filen);
295 /// this is used to handle XSelection events in the right manner.
304 bool multiparsel_cache_;
306 pit_type anchor_ref_;
310 void updateOffsetRef();
312 bool need_centering_;
314 /// keyboard mapping object.
315 boost::scoped_ptr<Intl> const intl_;
317 /// last visited inset (kept to send setMouseHover(false) )
320 /// A map from a Text to the associated text metrics
321 typedef std::map<Text const *, TextMetrics> TextMetricsCache;
322 mutable TextMetricsCache text_metrics_;
325 /// some space for drawing the 'nested' markers (in pixel)
326 inline int nestMargin() { return 15; }
328 /// margin for changebar
329 inline int changebarMargin() { return 12; }
332 inline int rightMargin() { return 10; }
336 #endif // BUFFERVIEW_H