]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.h
added a parseError signal to Buffer and use it
[lyx.git] / src / insets / insettext.h
1 // -*- C++ -*-
2 /**
3  * \file insettext.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Jürgen Vigna
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #ifndef INSETTEXT_H
13 #define INSETTEXT_H
14
15 #include "updatableinset.h"
16 #include "LString.h"
17 #include "LColor.h"
18 #include "ParagraphList.h"
19 #include "RowList.h"
20 #include "dimension.h"
21 #include "frontends/mouse_state.h"
22
23 #include "support/types.h"
24
25 #include <boost/shared_ptr.hpp>
26 #include <map>
27
28 class Painter;
29 class BufferView;
30 class Buffer;
31 class BufferParams;
32 class LyXCursor;
33 class LyXText;
34 class Row;
35 class Paragraph;
36
37 /**
38  A text inset is like a TeX box to write full text
39  (including styles and other insets) in a given space.
40  @author: Jürgen Vigna
41  */
42 class InsetText : public UpdatableInset {
43 public:
44         ///
45         /// numbers need because of test if codeA < codeB
46         ///
47         enum UpdateCodes {
48                 ///
49                 NONE = 0,
50                 ///
51                 CURSOR = 1,
52                 ///
53                 DRAW_FRAME = 2,
54                 ///
55                 SELECTION = 4,
56                 ///
57                 CURSOR_PAR = 8,
58                 ///
59                 FULL = 16,
60                 ///
61                 INIT = 32
62         };
63         ///
64         enum DrawFrame {
65                 ///
66                 NEVER = 0,
67                 ///
68                 LOCKED,
69                 ///
70                 ALWAYS
71         };
72         ///
73         InsetText(BufferParams const &);
74         ///
75         explicit InsetText(InsetText const &);
76         ///
77         InsetBase * clone() const;
78         ///
79         InsetText & operator=(InsetText const & it);
80         /// empty inset to empty par, or just mark as erased
81         void clear(bool just_mark_erased);
82         ///
83         void read(Buffer const *, LyXLex &);
84         ///
85         void write(Buffer const *, std::ostream &) const;
86         ///
87         void metrics(MetricsInfo &, Dimension &) const;
88         ///
89         int textWidth(BufferView *, bool fordraw = false) const;
90         ///
91         void draw(PainterInfo & pi, int x, int y) const;
92         ///
93         void update(BufferView *, bool = false);
94         ///
95         void setUpdateStatus(BufferView *, int what) const;
96         ///
97         string const editMessage() const;
98         ///
99         bool isTextInset() const { return true; }
100         ///
101         void insetUnlock(BufferView *);
102         ///
103         bool lockInsetInInset(BufferView *, UpdatableInset *);
104         ///
105         bool unlockInsetInInset(BufferView *,
106                                 UpdatableInset *, bool lr = false);
107         ///
108         bool updateInsetInInset(BufferView *, Inset *);
109         ///
110         RESULT localDispatch(FuncRequest const &);
111         ///
112         int latex(Buffer const *, std::ostream &,
113                   LatexRunParams const &) const;
114         ///
115         int ascii(Buffer const *, std::ostream &, int linelen) const;
116         ///
117         int linuxdoc(Buffer const *, std::ostream &) const { return 0; }
118         ///
119         int docbook(Buffer const *, std::ostream &, bool mixcont) const ;
120         ///
121         void validate(LaTeXFeatures & features) const;
122         ///
123         Inset::Code lyxCode() const { return Inset::TEXT_CODE; }
124         /// FIXME, document
125         void getCursorPos(BufferView *, int & x, int & y) const;
126         /// Get the absolute document x,y of the cursor
127         virtual void getCursor(BufferView &, int &, int &) const;
128         ///
129         int insetInInsetY() const;
130         ///
131         void fitInsetCursor(BufferView *) const;
132         ///
133         bool insertInset(BufferView *, Inset *);
134         ///
135         bool insetAllowed(Inset::Code) const;
136         ///
137         UpdatableInset * getLockingInset() const;
138         ///
139         UpdatableInset * getFirstLockingInsetOfType(Inset::Code);
140         ///
141         void setFont(BufferView *, LyXFont const &,
142                      bool toggleall = false,
143                      bool selectall = false);
144         ///
145         int getMaxWidth(BufferView *, UpdatableInset const *) const;
146         ///
147         void init(InsetText const * ins);
148         ///
149         void writeParagraphData(Buffer const *, std::ostream &) const;
150         ///
151         void setParagraphData(ParagraphList const &);
152         ///
153         void setText(string const &, LyXFont const &);
154         ///
155         void setAutoBreakRows(bool);
156         ///
157         bool getAutoBreakRows() const { return autoBreakRows; }
158         ///
159         void setDrawFrame(BufferView *, DrawFrame);
160         ///
161         void setFrameColor(BufferView *, LColor::color);
162         ///
163         LyXText * getLyXText(BufferView const *,
164                              bool const recursive = false) const;
165         ///
166         void deleteLyXText(BufferView *, bool recursive = true) const;
167         ///
168         void resizeLyXText(BufferView *, bool force = false) const;
169         ///
170         bool showInsetDialog(BufferView *) const;
171         ///
172         void getLabelList(std::vector<string> &) const;
173         ///
174         bool nodraw() const;
175         ///
176         int scroll(bool recursive = true) const;
177         ///
178         void scroll(BufferView * bv, float sx) const {
179                 UpdatableInset::scroll(bv, sx);
180         }
181         ///
182         void scroll(BufferView * bv, int offset) const {
183                 UpdatableInset::scroll(bv, offset);
184         }
185         ///
186         void selectAll(BufferView * bv);
187         ///
188         void clearSelection(BufferView * bv);
189         ///
190         Inset * getInsetFromID(int id) const;
191         ///
192         ParagraphList * getParagraphs(int) const;
193         ///
194         LyXCursor const & cursor(BufferView *) const;
195         ///
196         bool allowSpellcheck() const { return true; }
197         ///
198         WordLangTuple const
199         selectNextWordToSpellcheck(BufferView *, float & value) const;
200         ///
201         void selectSelectedWord(BufferView *);
202         ///
203         void toggleSelection(BufferView *, bool kill_selection);
204
205         /// mark as erased for change tracking
206         void markErased() { clear(true); };
207         /**
208          * Mark as new. Used when pasting in tabular, and adding rows
209          * or columns. Note that pasting will ensure that tracking already
210          * happens, and this just resets the changes for the copied text,
211          * whereas for row/col add, we need to start tracking changes
212          * for the (empty) paragraph contained.
213          */
214         void markNew(bool track_changes = false);
215         /// find next change
216         bool nextChange(BufferView *, lyx::pos_type & length);
217
218         ///
219         bool searchForward(BufferView *, string const &,
220                            bool = true, bool = false);
221         ///
222         bool searchBackward(BufferView *, string const &,
223                             bool = true, bool = false);
224         ///
225         bool checkInsertChar(LyXFont &);
226         ///
227         void getDrawFont(LyXFont &) const;
228         /// append text onto the existing text
229         void appendParagraphs(Buffer * bp, ParagraphList &);
230
231         ///
232         void addPreview(grfx::PreviewLoader &) const;
233
234         //
235         // Public structures and variables
236         ///
237         mutable int need_update;
238
239         bool haveParagraphs() const {
240                 return true;
241         }
242         ///
243         ParagraphList paragraphs;
244 protected:
245         ///
246         void updateLocal(BufferView *, int what, bool mark_dirty);
247         /// set parameters for an initial lock of this inset
248         void lockInset(BufferView *);
249         /// lock an inset inside this one
250         void lockInset(BufferView *, UpdatableInset *);
251         ///
252         mutable int drawTextXOffset;
253         ///
254         mutable int drawTextYOffset;
255         ///
256         bool autoBreakRows;
257         ///
258         DrawFrame drawFrame_;
259         ///
260         LColor::color frame_color;
261
262 private:
263         ///
264         void lfunMousePress(FuncRequest const &);
265         ///
266         bool lfunMouseRelease(FuncRequest const &);
267         ///
268         void lfunMouseMotion(FuncRequest const &);
269
270         ///
271         struct InnerCache {
272                 ///
273                 InnerCache(boost::shared_ptr<LyXText>);
274                 ///
275                 boost::shared_ptr<LyXText> text;
276                 ///
277                 bool remove;
278         };
279         ///
280         typedef std::map<BufferView *, InnerCache> Cache;
281         ///
282         typedef Cache::value_type value_type;
283         ///
284         RESULT moveRight(BufferView *,
285                                          bool activate_inset = true,
286                                          bool selecting = false);
287         ///
288         RESULT moveLeft(BufferView *,
289                                         bool activate_inset = true,
290                                         bool selecting = false);
291         ///
292         RESULT moveRightIntern(BufferView *, bool front,
293                                                bool activate_inset = true,
294                                                bool selecting = false);
295         ///
296         RESULT moveLeftIntern(BufferView *, bool front,
297                                               bool activate_inset = true,
298                                               bool selecting = false);
299
300         ///
301         RESULT moveUp(BufferView *);
302         ///
303         RESULT moveDown(BufferView *);
304         ///
305         void setCharFont(Buffer const *, int pos, LyXFont const & font);
306         ///
307         bool checkAndActivateInset(BufferView * bv, bool front);
308         ///
309         bool checkAndActivateInset(BufferView * bv, int x = 0, int y = 0,
310                                    mouse_button::state button = mouse_button::none);
311         ///
312         void removeNewlines();
313         ///
314         int cx(BufferView *) const;
315         ///
316         int cix(BufferView *) const;
317         ///
318         int cy(BufferView *) const;
319         ///
320         int ciy(BufferView *) const;
321         ///
322         lyx::pos_type cpos(BufferView *) const;
323         ///
324         ParagraphList::iterator cpar(BufferView *) const;
325         ///
326         bool cboundary(BufferView *) const;
327         ///
328         RowList::iterator crow(BufferView *) const;
329         ///
330         void drawFrame(Painter &, int x) const;
331         ///
332         void clearInset(BufferView *, int start_x, int baseline) const;
333         ///
334         void saveLyXTextState(LyXText *) const;
335         ///
336         void restoreLyXTextState(LyXText *) const;
337         ///
338         void reinitLyXText() const;
339         ///
340         void collapseParagraphs(BufferView *);
341
342         /* Private structures and variables */
343         ///
344         mutable bool locked;
345         ///
346         mutable Dimension dim_;
347         ///
348         mutable int top_y;
349         ///
350         ParagraphList::iterator inset_par;
351         ///
352         lyx::pos_type inset_pos;
353         ///
354         bool inset_boundary;
355         ///
356         mutable int inset_x;
357         ///
358         mutable int inset_y;
359         ///
360         mutable int old_max_width;
361         ///
362         bool no_selection;
363         ///
364         UpdatableInset * the_locking_inset;
365         ///
366         mutable ParagraphList::iterator old_par;
367         /// The cache.
368         mutable Cache cache;
369         ///
370         mutable int last_drawn_width;
371         ///
372         mutable BufferView * cached_bview;
373         ///
374         mutable boost::shared_ptr<LyXText> cached_text;
375         ///
376         struct save_state {
377                 ParagraphList::iterator lpar;
378                 ParagraphList::iterator selstartpar;
379                 ParagraphList::iterator selendpar;
380                 lyx::pos_type pos;
381                 lyx::pos_type selstartpos;
382                 lyx::pos_type selendpos;
383                 bool boundary;
384                 bool selstartboundary;
385                 bool selendboundary;
386                 bool selection;
387                 bool mark_set;
388         };
389         ///
390         mutable save_state sstate;
391
392         ///
393         // this is needed globally so we know that we're using it actually and
394         // so the LyXText-Cache is not erased until used!
395         mutable LyXText * lt;
396         ///
397         // to remember old painted frame dimensions to clear it on the right spot!
398         ///
399         mutable int frame_x;
400         mutable int frame_y;
401         mutable int frame_w;
402         mutable int frame_h;
403         ///
404         bool in_update; /* as update is not reentrant! */
405         mutable BufferView * do_resize;
406         mutable bool do_reinit;
407         mutable bool in_insetAllowed;
408         ///
409         // these are used to check for mouse movement in Motion selection code
410         ///
411         int mouse_x;
412         int mouse_y;
413 };
414 #endif