]> git.lyx.org Git - features.git/blob - src/insets/insettext.h
the lfun3 patches (overall cleanup and "localizing" dispatch() in mathed)
[features.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 "ParagraphList.h"
26
27 #include "support/types.h"
28
29 #include <boost/shared_ptr.hpp>
30
31 class Painter;
32 class BufferView;
33 class Buffer;
34 class BufferParams;
35 class LyXCursor;
36 class LyXText;
37 class Row;
38 class Paragraph;
39
40 /**
41  A text inset is like a TeX box to write full text
42  (including styles and other insets) in a given space.
43  @author: Jürgen Vigna
44  */
45 class InsetText : public UpdatableInset {
46 public:
47         ///
48         /// numbers need because of test if codeA < codeB
49         ///
50         enum UpdateCodes {
51                 ///
52                 NONE = 0,
53                 ///
54                 CURSOR = 1,
55                 ///
56                 CLEAR_FRAME = 2,
57                 ///
58                 DRAW_FRAME = 4,
59                 ///
60                 SELECTION = 8,
61                 ///
62                 CURSOR_PAR = 16,
63                 ///
64                 FULL = 32,
65                 ///
66                 INIT = 64
67         };
68         ///
69         enum DrawFrame {
70                 ///
71                 NEVER = 0,
72                 ///
73                 LOCKED,
74                 ///
75                 ALWAYS
76         };
77         ///
78         InsetText(BufferParams const &);
79         ///
80         explicit
81         InsetText(InsetText const &, bool same_id = false);
82         ///
83         ~InsetText();
84         ///
85         Inset * clone(Buffer const &, bool same_id = false) const;
86         ///
87         InsetText & operator=(InsetText const & it);
88         ///
89         void clear();
90         ///
91         void read(Buffer const *, LyXLex &);
92         ///
93         void write(Buffer const *, std::ostream &) const;
94         ///
95         int ascent(BufferView *, LyXFont const &) const;
96         ///
97         int descent(BufferView *, LyXFont const &) const;
98         ///
99         int width(BufferView *, LyXFont const & f) const;
100         ///
101         int textWidth(BufferView *, bool fordraw = false) const;
102         ///
103         void draw(BufferView *, LyXFont const &, int , float &, bool) const;
104         ///
105         void update(BufferView *, LyXFont const &, bool = false);
106         ///
107         void setUpdateStatus(BufferView *, int what) const;
108         ///
109         string const editMessage() const;
110         ///
111         void edit(BufferView *, int, int, mouse_button::state);
112         ///
113         void edit(BufferView *, bool front = true);
114         ///
115         bool isTextInset() const { return true; }
116         ///
117         bool doClearArea() const;
118         ///
119         void insetUnlock(BufferView *);
120         ///
121         bool lockInsetInInset(BufferView *, UpdatableInset *);
122         ///
123         bool unlockInsetInInset(BufferView *,
124                                 UpdatableInset *, bool lr = false);
125         ///
126         bool updateInsetInInset(BufferView *, Inset *);
127         ///
128         bool insetButtonRelease(BufferView *, int, int, mouse_button::state);
129         ///
130         void insetButtonPress(BufferView *, int, int, mouse_button::state);
131         ///
132         void insetMotionNotify(BufferView *, int, int, mouse_button::state);
133         ///
134         RESULT localDispatch(FuncRequest 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 &, bool mixcont) 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         int insetInInsetY() const;
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 &, LyXFont 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         ///
234         WordLangTuple const
235         selectNextWordToSpellcheck(BufferView *, float & value) const;
236         ///
237         void selectSelectedWord(BufferView *);
238         ///
239         void toggleSelection(BufferView *, bool kill_selection);
240         ///
241         bool searchForward(BufferView *, string const &,
242                            bool = true, bool = false);
243         ///
244         bool searchBackward(BufferView *, string const &,
245                             bool = true, bool = false);
246         ///
247         bool checkInsertChar(LyXFont &);
248         ///
249         void getDrawFont(LyXFont &) const;
250         ///
251         void appendParagraphs(BufferParams const & bparams, Paragraph *);
252         ///
253         void addPreview(grfx::PreviewLoader &) const;
254
255         //
256         // Public structures and variables
257         ///
258         mutable int need_update;
259
260 protected:
261         ///
262         void updateLocal(BufferView *, int what, bool mark_dirty) const;
263         /// set parameters for an initial lock of this inset
264         void lockInset(BufferView *);
265         /// lock an inset inside this one
266         void lockInset(BufferView *, UpdatableInset *);
267         ///
268         mutable int drawTextXOffset;
269         ///
270         mutable int drawTextYOffset;
271         ///
272         bool autoBreakRows;
273         ///
274         DrawFrame drawFrame_;
275         ///
276         LColor::color frame_color;
277
278 private:
279         ///
280         struct InnerCache {
281                 ///
282                 InnerCache(boost::shared_ptr<LyXText>);
283                 ///
284                 boost::shared_ptr<LyXText> text;
285                 ///
286                 bool remove;
287         };
288         ///
289         typedef std::map<BufferView *, InnerCache> Cache;
290         ///
291         typedef Cache::value_type value_type;
292         ///
293         int beginningOfMainBody(Paragraph * par) const;
294         ///
295         UpdatableInset::RESULT moveRight(BufferView *,
296                                          bool activate_inset = true,
297                                          bool selecting = false);
298         ///
299         UpdatableInset::RESULT moveLeft(BufferView *,
300                                         bool activate_inset = true,
301                                         bool selecting = false);
302         ///
303         UpdatableInset::RESULT moveRightIntern(BufferView *, bool front,
304                                                bool activate_inset = true,
305                                                bool selecting = false);
306         ///
307         UpdatableInset::RESULT moveLeftIntern(BufferView *, bool front,
308                                               bool activate_inset = true,
309                                               bool selecting = false);
310
311         ///
312         UpdatableInset::RESULT moveUp(BufferView *);
313         ///
314         UpdatableInset::RESULT moveDown(BufferView *);
315         ///
316         void setCharFont(Buffer const *, int pos, LyXFont const & font);
317         ///
318         bool checkAndActivateInset(BufferView * bv, bool front);
319         ///
320         bool checkAndActivateInset(BufferView * bv, int x = 0, int y = 0,
321                                    mouse_button::state button = mouse_button::none);
322         ///
323         void removeNewlines();
324         ///
325         int cx(BufferView *) const;
326         ///
327         int cix(BufferView *) const;
328         ///
329         int cy(BufferView *) const;
330         ///
331         int ciy(BufferView *) const;
332         ///
333         lyx::pos_type cpos(BufferView *) const;
334         ///
335         Paragraph * cpar(BufferView *) const;
336         ///
337         bool cboundary(BufferView *) const;
338         ///
339         Row * crow(BufferView *) const;
340         ///
341         void drawFrame(Painter &, bool cleared) const;
342         ///
343         void clearFrame(Painter &, bool cleared) const;
344         ///
345         void clearInset(BufferView *, int baseline, bool & cleared) const;
346         ///
347         void saveLyXTextState(LyXText *) const;
348         ///
349         void restoreLyXTextState(BufferView *, LyXText *) const;
350         ///
351         void reinitLyXText() const;
352         ///
353         void collapseParagraphs(BufferView *) const;
354
355         /* Private structures and variables */
356         ///
357         ParagraphList paragraphs;
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 bool frame_is_visible;
392         ///
393         mutable BufferView * cached_bview;
394         ///
395         mutable boost::shared_ptr<LyXText> cached_text;
396         ///
397         struct save_state {
398                 Paragraph * lpar;
399                 Paragraph * selstartpar;
400                 Paragraph * selendpar;
401                 lyx::pos_type pos;
402                 lyx::pos_type selstartpos;
403                 lyx::pos_type selendpos;
404                 bool boundary;
405                 bool selstartboundary;
406                 bool selendboundary;
407                 bool selection;
408                 bool mark_set;
409                 bool refresh;
410         };
411         ///
412         mutable save_state sstate;
413
414         ///
415         // this is needed globally so we know that we're using it actually and
416         // so the LyXText-Cache is not erased until used!
417         mutable LyXText * lt;
418         ///
419         // to remember old painted frame dimensions to clear it on the right spot!
420         ///
421         mutable int frame_x;
422         mutable int frame_y;
423         mutable int frame_w;
424         mutable int frame_h;
425         ///
426         bool in_update; /* as update is not reentrant! */
427         mutable BufferView * do_resize;
428         mutable bool do_reinit;
429         mutable bool in_insetAllowed;
430         ///
431         // these are used to check for mouse movement in Motion selection code
432         ///
433         int mouse_x;
434         int mouse_y;
435 };
436 #endif