]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.h
Hopefully fixed the redo problems with insets!
[lyx.git] / src / insets / insettext.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *
7  *           Copyright 1998 The LyX Team.
8  *
9  *
10  *======================================================
11  */
12 // The pristine updatable inset: Text
13
14
15 #ifndef INSETTEXT_H
16 #define INSETTEXT_H
17
18 #ifdef __GNUG__
19 #pragma interface
20 #endif
21
22 #include "inset.h"
23 #include "LString.h"
24 #include "LColor.h"
25 #include "paragraph.h"
26 #include "lyxcursor.h"
27 #include <boost/smart_ptr.hpp>
28
29 class Painter;
30 class BufferView;
31 class Buffer;
32 class LyXCursor;
33 class LyXText;
34 class LyXScreen;
35 class Row;
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();
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         ///
86         void clear();
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, unsigned int);
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         void insetButtonRelease(BufferView *, int, int, int);
126         ///
127         void insetButtonPress(BufferView *, int, int, int);
128         ///
129         void insetMotionNotify(BufferView *, int, int, int);
130         ///
131         void insetKeyPress(XKeyEvent *);
132         ///
133         UpdatableInset::RESULT localDispatch(BufferView *,
134                                              kb_action, string const &);
135         ///
136         int latex(Buffer const *, std::ostream &,
137                   bool fragile, bool free_spc) const;
138         ///
139         int ascii(Buffer const *, std::ostream &, int linelen) const;
140         ///
141         int linuxdoc(Buffer const *, std::ostream &) const { return 0; }
142         ///
143         int docbook(Buffer const *, std::ostream &) const ;
144         ///
145         void validate(LaTeXFeatures & features) const;
146         ///
147         Inset::Code lyxCode() const { return Inset::TEXT_CODE; }
148         ///
149         void getCursorPos(BufferView *, int & x, int & y) const;
150         ///
151         unsigned int insetInInsetY();
152         ///
153         void toggleInsetCursor(BufferView *);
154         ///
155         void showInsetCursor(BufferView *, bool show=true);
156         ///
157         void hideInsetCursor(BufferView *);
158         ///
159         void fitInsetCursor(BufferView *) const;
160         ///
161         bool insertInset(BufferView *, Inset *);
162         ///
163         bool insetAllowed(Inset::Code) const;
164         ///
165         UpdatableInset * getLockingInset() const;
166         ///
167         UpdatableInset * getFirstLockingInsetOfType(Inset::Code);
168         ///
169         void setFont(BufferView *, LyXFont const &,
170                      bool toggleall = false,
171                      bool selectall = false);
172         ///
173         int getMaxWidth(BufferView *, UpdatableInset const *) const;
174         ///
175         void init(InsetText const * ins = 0, bool same_id = false);
176         ///
177         void writeParagraphData(Buffer const *, std::ostream &) const;
178         ///
179         void setParagraphData(Paragraph *, bool same_id = false);
180         ///
181         void setText(string const &);
182         ///
183         void setAutoBreakRows(bool);
184         ///
185         bool getAutoBreakRows() const { return autoBreakRows; }
186         ///
187         void setDrawFrame(BufferView *, DrawFrame);
188         ///
189         void setFrameColor(BufferView *, LColor::color);
190         ///
191         LyXText * getLyXText(BufferView const *,
192                              bool const recursive = false) const;
193         ///
194         void deleteLyXText(BufferView *, bool recursive = true) const;
195         ///
196         void resizeLyXText(BufferView *, bool force = false) const;
197         ///
198         bool showInsetDialog(BufferView *) const;
199         ///
200         std::vector<string> const getLabelList() const;
201         ///
202         bool nodraw() const;
203         ///
204         int scroll(bool recursive=true) const;
205         ///
206         void scroll(BufferView *bv, float sx) const {
207                 UpdatableInset::scroll(bv, sx);
208         }
209         ///
210         void scroll(BufferView *bv, int offset) const {
211                 UpdatableInset::scroll(bv, offset);
212         }
213         ///
214         void selectAll(BufferView *bv);
215         ///
216         void clearSelection(BufferView *bv);
217         ///
218         Paragraph * getParFromID(int id) const;
219         ///
220         Inset * getInsetFromID(int id) const;
221         ///
222         Paragraph * firstParagraph() const;
223         ///
224         Paragraph * getFirstParagraph(int) const;
225         ///
226         LyXCursor const & cursor(BufferView *) const;
227         ///
228         Paragraph * paragraph() const;
229         ///
230         void paragraph(Paragraph *);
231         ///
232         bool allowSpellcheck() { return true; }
233         string const selectNextWordToSpellcheck(BufferView *, float & value) const;
234         void selectSelectedWord(BufferView *);
235         void toggleSelection(BufferView *, bool kill_selection);
236         ///
237         bool searchForward(BufferView *, string const &,
238                            bool const & = true, bool const & = false);
239         bool searchBackward(BufferView *, string const &,
240                             bool const & = true, bool const & = false);
241         ///
242         bool checkInsertChar(LyXFont &);
243         ///
244         //
245         // Public structures and variables
246         ///
247         mutable int need_update;
248
249 protected:
250         ///
251         void updateLocal(BufferView *, int what, bool mark_dirty) const;
252         ///
253         mutable int drawTextXOffset;
254         ///
255         mutable int drawTextYOffset;
256         ///
257         bool autoBreakRows;
258         ///
259         DrawFrame drawFrame_;
260         ///
261         LColor::color frame_color;
262
263 private:
264         struct InnerCache {
265                 InnerCache(boost::shared_ptr<LyXText>);
266
267                 boost::shared_ptr<LyXText> text;
268                 bool remove;
269         };
270         ///
271         typedef std::map<BufferView *, struct InnerCache > Cache;
272         ///
273         typedef Cache::value_type value_type;
274         ///
275         int beginningOfMainBody(Buffer const *, Paragraph * par) const;
276         ///
277         UpdatableInset::RESULT moveRight(BufferView *,
278                                          bool activate_inset = true,
279                                          bool selecting = false);
280         ///
281         UpdatableInset::RESULT moveLeft(BufferView *,
282                                         bool activate_inset = true,
283                                         bool selecting = false);
284         ///
285         UpdatableInset::RESULT moveRightIntern(BufferView *, bool behind,
286                                                bool activate_inset = true,
287                                                bool selecting = false);
288         ///
289         UpdatableInset::RESULT moveLeftIntern(BufferView *, bool behind, 
290                                               bool activate_inset = true,
291                                               bool selecting = false);
292
293         ///
294         UpdatableInset::RESULT moveUp(BufferView *);
295         ///
296         UpdatableInset::RESULT moveDown(BufferView *);
297         ///
298         void setCharFont(Buffer const *, int pos, LyXFont const & font);
299         ///
300         bool checkAndActivateInset(BufferView * bv, bool behind);
301         ///
302         bool checkAndActivateInset(BufferView * bv, int x = 0, int y = 0,
303                                    int button = 0);
304         ///
305         void removeNewlines();
306         ///
307         int cx(BufferView *) const;
308         ///
309         int cy(BufferView *) const;
310         ///
311         lyx::pos_type cpos(BufferView *) const;
312         ///
313         Paragraph * cpar(BufferView *) const;
314         ///
315         bool cboundary(BufferView *) const;
316         ///
317         Row * crow(BufferView *) const;
318         ///
319         void drawFrame(Painter &, bool cleared) const;
320         ///
321         void clearFrame(Painter &, bool cleared) const;
322         ///
323         void clearInset(Painter &, int baseline, bool & cleared) const;
324         ///
325         void saveLyXTextState(LyXText *) const;
326         void restoreLyXTextState(BufferView *, LyXText *) const;
327         ///
328         void reinitLyXText() const;
329         
330         /* Private structures and variables */
331         ///
332         Paragraph * par;
333         ///
334         mutable bool locked;
335         ///
336         mutable int insetAscent;
337         ///
338         mutable int insetDescent;
339         ///
340         mutable int insetWidth;
341         ///
342         mutable int last_width;
343         ///
344         mutable int last_height;
345         ///
346         mutable int top_y;
347         ///
348         Paragraph * inset_par;
349         ///
350         lyx::pos_type inset_pos;
351         ///
352         bool inset_boundary;
353         ///
354         mutable int inset_x;
355         ///
356         mutable int inset_y;
357         ///
358         mutable int old_max_width;
359         ///
360         bool no_selection;
361         ///
362         mutable float xpos;
363         ///
364         UpdatableInset * the_locking_inset;
365         ///
366         mutable Paragraph * old_par;
367         /// The cache.
368         mutable Cache cache;
369         ///
370         mutable int last_drawn_width;
371         ///
372         mutable bool frame_is_visible;
373         ///
374         mutable BufferView * cached_bview;
375         ///
376         mutable boost::shared_ptr<LyXText> cached_text;
377         ///
378         mutable struct save_state {
379                 Paragraph * lpar;
380                 Paragraph * selstartpar;
381                 Paragraph * selendpar;
382                 lyx::pos_type pos;
383                 lyx::pos_type selstartpos;
384                 lyx::pos_type selendpos;
385                 bool boundary;
386                 bool selstartboundary;
387                 bool selendboundary;
388                 bool selection;
389                 bool mark_set;
390                 bool refresh;
391         } sstate;
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 };
408 #endif