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