]> git.lyx.org Git - lyx.git/blob - src/BufferView.h
Embedding: saving inzip name to .lyx file so that embedded files can always be found...
[lyx.git] / src / BufferView.h
1 // -*- C++ -*-
2 /**
3  * \file BufferView.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Alfredo Braustein
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  * \author Jürgen Vigna
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef BUFFER_VIEW_H
16 #define BUFFER_VIEW_H
17
18 #include "update_flags.h"
19
20 #include "support/strfwd.h"
21 #include "support/types.h"
22
23 namespace lyx {
24
25 namespace support { class FileName; }
26
27 namespace frontend { class Painter; }
28 namespace frontend { class GuiBufferViewDelegate; }
29
30 class Buffer;
31 class Change;
32 class CoordCache;
33 class Cursor;
34 class DocIterator;
35 class FuncRequest;
36 class FuncStatus;
37 class Intl;
38 class Inset;
39 class Menu;
40 class ParIterator;
41 class ParagraphMetrics;
42 class Point;
43 class Text;
44 class TextMetrics;
45
46 enum CursorStatus {
47         CUR_INSIDE,
48         CUR_ABOVE,
49         CUR_BELOW
50 };
51
52 /// Scrollbar Parameters.
53 struct ScrollbarParameters
54 {
55         void reset(int h = 0, int p = 0, int l = 0)
56         {
57                 height = h;
58                 position = p;
59                 lineScrollHeight = l;
60         }
61
62         /// Total document height in pixels.
63         int height;
64         /// Current position in the document in pixels.
65         int position;
66         /// Line-scroll amount in pixels.
67         int lineScrollHeight;
68 };
69
70 /// Screen view of a Buffer.
71 /**
72  * A BufferView encapsulates a view onto a particular
73  * buffer, and allows access to operate upon it. A view
74  * is a sliding window of the entire document rendering.
75  * It is the official interface between the LyX core and
76  * the frontend WorkArea.
77  *
78  * \sa WorkArea
79  * \sa Buffer
80  * \sa CoordCache
81  */
82 class BufferView {
83 public:
84         ///
85         BufferView(Buffer & buffer);
86         ///
87         ~BufferView();
88
89         /// return the buffer being viewed.
90         Buffer & buffer();
91         Buffer const & buffer() const;
92
93         /// perform pending metrics updates.
94         /** \c Update::FitCursor means first to do a FitCursor, and to
95          * force an update if screen position changes.
96          * \c Update::Force means to force an update in any case.
97          * \retval true if a screen redraw is needed
98          */
99         void processUpdateFlags(Update::flags flags);
100
101         /// move the screen to fit the cursor.
102         /// Only to be called with good y coordinates (after a bv::metrics)
103         bool fitCursor();
104         /// reset the scrollbar to reflect current view position.
105         void updateScrollbar();
106         /// return the Scrollbar Parameters.
107         ScrollbarParameters const & scrollbarParameters() const;
108         /// \return Tool tip for the given position.
109         docstring toolTip(int x, int y) const;
110         /// \return the context menu for the given position.
111         docstring contextMenu(int x, int y) const;
112
113         /// Save the current position as bookmark.
114         /// if idx == 0, save to temp_bookmark
115         void saveBookmark(unsigned int idx);
116         /// goto a specified position, try top_id first, and then bottom_pit.
117         /// \return true if success
118         bool moveToPosition(
119                 pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
120                 pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
121                 int top_id, ///< Paragraph ID, \sa Paragraph
122                 pos_type top_pos ///< Position in the \c Paragraph
123                 );
124         /// return the current change at the cursor.
125         Change const getCurrentChange() const;
126
127         /// move cursor to the named label.
128         void gotoLabel(docstring const & label);
129
130         /// set the cursor based on the given TeX source row.
131         void setCursorFromRow(int row);
132
133         /// Ensure the cursor is visible.
134         /// This method will automatically scroll and update the BufferView and updated 
135         /// if needed.
136         void showCursor();
137         /// LFUN_SCROLL Helper.
138         void lfunScroll(FuncRequest const & cmd);
139         /// scroll down document by the given number of pixels.
140         void scrollDown(int pixels);
141         /// scroll up document by the given number of pixels.
142         void scrollUp(int pixels);
143         /// scroll document by the given number of pixels.
144         void scroll(int pixels);
145         /// Scroll the view by a number of pixels.
146         void scrollDocView(int pixels);
147         /// Set the cursor position based on the scrollbar one.
148         void setCursorFromScrollbar();
149
150         /// return the pixel width of the document view.
151         int workWidth() const;
152         /// return the pixel height of the document view.
153         int workHeight() const;
154
155
156         /// translate and insert a character, using the correct keymap.
157         void translateAndInsert(char_type c, Text * t, Cursor & cur);
158
159         /// return true for events that will handle.
160         FuncStatus getStatus(FuncRequest const & cmd);
161         /// execute the given function.
162         /// \return true if the function has been processed.
163         bool dispatch(FuncRequest const & argument);
164
165         /// request an X11 selection.
166         /// \return the selected string.
167         docstring const requestSelection();
168         /// clear the X11 selection.
169         void clearSelection();
170
171         /// resize the BufferView.
172         /// \sa WorkArea
173         void resize(int width, int height);
174
175         /// dispatch method helper for \c WorkArea
176         /// \sa WorkArea
177         void mouseEventDispatch(FuncRequest const & ev);
178
179         /// access to anchor.
180         pit_type anchor_ref() const;
181
182         ///
183         CursorStatus cursorStatus(DocIterator const & dit) const;
184         /// access to full cursor.
185         Cursor & cursor();
186         /// access to full cursor.
187         Cursor const & cursor() const;
188         /// sets cursor.
189         /// This will also open all relevant collapsable insets.
190         void setCursor(DocIterator const &);
191         /// Check deleteEmptyParagraphMechanism and update metrics if needed.
192         /// \retval true if an update was needed.
193         bool checkDepm(Cursor & cur, Cursor & old);
194         /// sets cursor.
195         /// This is used when handling LFUN_MOUSE_PRESS.
196         bool mouseSetCursor(Cursor & cur, bool select = false);
197
198         /// sets the selection.
199         /* When \c backwards == false, set anchor
200          * to \c cur and cursor to \c cur + \c length. When \c
201          * backwards == true, set anchor to \c cur and cursor to \c
202          * cur + \c length.
203          */
204         void putSelectionAt(DocIterator const & cur,
205                 int length, bool backwards);
206
207         /// update the internal \c ViewMetricsInfo.
208         void updateMetrics();
209
210         ///
211         TextMetrics const & textMetrics(Text const * t) const;
212         TextMetrics & textMetrics(Text const * t);
213         ///
214         ParagraphMetrics const & parMetrics(Text const *, pit_type) const;
215
216         ///
217         CoordCache & coordCache();
218         ///
219         CoordCache const & coordCache() const;
220
221         ///
222         Point getPos(DocIterator const & dit, bool boundary) const;
223
224
225         ///
226         void draw(frontend::Painter & pain);
227
228         /// get this view's keyboard map handler.
229         Intl & getIntl();
230         ///
231         Intl const & getIntl() const;
232
233         //
234         // Messages to the GUI
235         //
236         /// This signal is emitted when some message shows up.
237         void message(docstring const & msg);
238
239         /// This signal is emitted when some dialog needs to be shown.
240         void showDialog(std::string const & name);
241
242         /// This signal is emitted when some dialog needs to be shown with
243         /// some data.
244         void showDialog(std::string const & name, std::string const & data,
245                 Inset * inset = 0);
246
247         /// This signal is emitted when some dialogs needs to be updated.
248         void updateDialog(std::string const & name, std::string const & data);
249
250         ///
251         void setGuiDelegate(frontend::GuiBufferViewDelegate *);
252
253         ///
254         docstring contentsOfPlaintextFile(support::FileName const & f);
255         // Insert plain text file (if filename is empty, prompt for one)
256         void insertPlaintextFile(support::FileName const & f, bool asParagraph);
257         ///
258         void insertLyXFile(support::FileName const & f);
259
260 private:
261         /// noncopyable
262         BufferView(BufferView const &);
263         void operator=(BufferView const &);
264
265         // the position relative to (0, baseline) of outermost paragraph
266         Point coordOffset(DocIterator const & dit, bool boundary) const;
267         /// Update current paragraph metrics.
268         /// \return true if no further update is needed.
269         bool singleParUpdate();
270
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
277                 ) const;
278
279         ///
280         int width_;
281         ///
282         int height_;
283         ///
284         Buffer & buffer_;
285
286         struct Private;
287         Private * const d;
288 };
289
290 /// some space for drawing the 'nested' markers (in pixel)
291 inline int nestMargin() { return 15; }
292
293 /// margin for changebar
294 inline int changebarMargin() { return 12; }
295
296 /// right margin
297 inline int rightMargin() { return 10; }
298
299 } // namespace lyx
300
301 #endif // BUFFERVIEW_H