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