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