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