]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.h
move inset related stuff from src/graphics to src/inset/
[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
28 #include <boost/shared_ptr.hpp>
29
30 class Painter;
31 class BufferView;
32 class Buffer;
33 class BufferParams;
34 class LyXCursor;
35 class LyXText;
36 class LyXScreen;
37 class Row;
38
39 /**
40  A text inset is like a TeX box to write full text
41  (including styles and other insets) in a given space.
42  @author: Jürgen Vigna
43  */
44 class InsetText : public UpdatableInset {
45 public:
46         ///
47         /// numbers need because of test if codeA < codeB
48         ///
49         enum UpdateCodes {
50                 ///
51                 NONE = 0,
52                 ///
53                 CURSOR = 1,
54                 ///
55                 CLEAR_FRAME = 2,
56                 ///
57                 DRAW_FRAME = 4,
58                 ///
59                 SELECTION = 8,
60                 ///
61                 CURSOR_PAR = 16,
62                 ///
63                 FULL = 32,
64                 ///
65                 INIT = 64
66         };
67         ///
68         enum DrawFrame {
69                 ///
70                 NEVER = 0,
71                 ///
72                 LOCKED,
73                 ///
74                 ALWAYS
75         };
76         ///
77         InsetText(BufferParams const &);
78         ///
79         explicit
80         InsetText(InsetText const &, bool same_id = false);
81         ///
82         ~InsetText();
83         ///
84         Inset * clone(Buffer const &, bool same_id = false) const;
85         ///
86         InsetText & operator=(InsetText const & it);
87         ///
88         void clear();
89         ///
90         void read(Buffer const *, LyXLex &);
91         ///
92         void write(Buffer const *, std::ostream &) const;
93         ///
94         int ascent(BufferView *, LyXFont const &) const;
95         ///
96         int descent(BufferView *, LyXFont const &) const;
97         ///
98         int width(BufferView *, LyXFont const & f) const;
99         ///
100         int textWidth(BufferView *, bool fordraw = false) const;
101         ///
102         void draw(BufferView *, LyXFont const &, int , float &, bool) const;
103         ///
104         void update(BufferView *, LyXFont const &, bool = false);
105         ///
106         void setUpdateStatus(BufferView *, int what) const;
107         ///
108         string const editMessage() const;
109         ///
110         void edit(BufferView *, int, int, mouse_button::state);
111         ///
112         void edit(BufferView *, bool front = true);
113         ///
114         bool isTextInset() const { return true; }
115         ///
116         bool doClearArea() const;
117         ///
118         void insetUnlock(BufferView *);
119         ///
120         bool lockInsetInInset(BufferView *, UpdatableInset *);
121         ///
122         bool unlockInsetInInset(BufferView *,
123                                 UpdatableInset *, bool lr = false);
124         ///
125         bool updateInsetInInset(BufferView *, Inset *);
126         ///
127         bool insetButtonRelease(BufferView *, int, int, mouse_button::state);
128         ///
129         void insetButtonPress(BufferView *, int, int, mouse_button::state);
130         ///
131         void insetMotionNotify(BufferView *, int, int, mouse_button::state);
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         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         string const selectNextWordToSpellcheck(BufferView *,
235                                                 float & value) const;
236         void selectSelectedWord(BufferView *);
237         ///
238         void toggleSelection(BufferView *, bool kill_selection);
239         ///
240         bool searchForward(BufferView *, string const &,
241                            bool = true, bool = false);
242         ///
243         bool searchBackward(BufferView *, string const &,
244                             bool = true, bool = false);
245         ///
246         bool checkInsertChar(LyXFont &);
247         ///
248         void getDrawFont(LyXFont &) const;
249         ///
250         void appendParagraphs(BufferParams const & bparams, Paragraph *);
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(Buffer const *, 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         mutable float xpos;
381         ///
382         UpdatableInset * the_locking_inset;
383         ///
384         mutable Paragraph * old_par;
385         /// The cache.
386         mutable Cache cache;
387         ///
388         mutable int last_drawn_width;
389         ///
390         mutable bool frame_is_visible;
391         ///
392         mutable BufferView * cached_bview;
393         ///
394         mutable boost::shared_ptr<LyXText> cached_text;
395         ///
396         struct save_state {
397                 Paragraph * lpar;
398                 Paragraph * selstartpar;
399                 Paragraph * selendpar;
400                 lyx::pos_type pos;
401                 lyx::pos_type selstartpos;
402                 lyx::pos_type selendpos;
403                 bool boundary;
404                 bool selstartboundary;
405                 bool selendboundary;
406                 bool selection;
407                 bool mark_set;
408                 bool refresh;
409         };
410         ///
411         mutable save_state sstate;
412
413         ///
414         // this is needed globally so we know that we're using it actually and
415         // so the LyXText-Cache is not erased until used!
416         mutable LyXText * lt;
417         ///
418         // to remember old painted frame dimensions to clear it on the right spot!
419         ///
420         mutable int frame_x;
421         mutable int frame_y;
422         mutable int frame_w;
423         mutable int frame_h;
424         ///
425         bool in_update; /* as update is not reentrant! */
426         mutable BufferView * do_resize;
427         mutable bool do_reinit;
428         mutable bool in_insetAllowed;
429         ///
430         // these are used to check for mouse movement in Motion selection code
431         ///
432         int mouse_x;
433         int mouse_y;
434 };
435 #endif