]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.h
Rename screen, and don't re-construct it on a buffer change (that is far too
[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 Row;
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 *,
133                                              kb_action, string const &);
134         ///
135         int latex(Buffer const *, std::ostream &,
136                   bool fragile, bool free_spc) const;
137         ///
138         int ascii(Buffer const *, std::ostream &, int linelen) const;
139         ///
140         int linuxdoc(Buffer const *, std::ostream &) const { return 0; }
141         ///
142         int docbook(Buffer const *, std::ostream &) const ;
143         ///
144         void validate(LaTeXFeatures & features) const;
145         ///
146         Inset::Code lyxCode() const { return Inset::TEXT_CODE; }
147         ///
148         void getCursorPos(BufferView *, int & x, int & y) const;
149         ///
150         int insetInInsetY() const;
151         ///
152         void toggleInsetCursor(BufferView *);
153         ///
154         void showInsetCursor(BufferView *, bool show = true);
155         ///
156         void hideInsetCursor(BufferView *);
157         ///
158         void fitInsetCursor(BufferView *) const;
159         ///
160         bool insertInset(BufferView *, Inset *);
161         ///
162         bool insetAllowed(Inset::Code) const;
163         ///
164         UpdatableInset * getLockingInset() const;
165         ///
166         UpdatableInset * getFirstLockingInsetOfType(Inset::Code);
167         ///
168         void setFont(BufferView *, LyXFont const &,
169                      bool toggleall = false,
170                      bool selectall = false);
171         ///
172         int getMaxWidth(BufferView *, UpdatableInset const *) const;
173         ///
174         void init(InsetText const * ins = 0, bool same_id = false);
175         ///
176         void writeParagraphData(Buffer const *, std::ostream &) const;
177         ///
178         void setParagraphData(Paragraph *, bool same_id = false);
179         ///
180         void setText(string const &, LyXFont const &);
181         ///
182         void setAutoBreakRows(bool);
183         ///
184         bool getAutoBreakRows() const { return autoBreakRows; }
185         ///
186         void setDrawFrame(BufferView *, DrawFrame);
187         ///
188         void setFrameColor(BufferView *, LColor::color);
189         ///
190         LyXText * getLyXText(BufferView const *,
191                              bool const recursive = false) const;
192         ///
193         void deleteLyXText(BufferView *, bool recursive = true) const;
194         ///
195         void resizeLyXText(BufferView *, bool force = false) const;
196         ///
197         bool showInsetDialog(BufferView *) const;
198         ///
199         std::vector<string> const getLabelList() const;
200         ///
201         bool nodraw() const;
202         ///
203         int scroll(bool recursive = true) const;
204         ///
205         void scroll(BufferView * bv, float sx) const {
206                 UpdatableInset::scroll(bv, sx);
207         }
208         ///
209         void scroll(BufferView * bv, int offset) const {
210                 UpdatableInset::scroll(bv, offset);
211         }
212         ///
213         void selectAll(BufferView * bv);
214         ///
215         void clearSelection(BufferView * bv);
216         ///
217         Paragraph * getParFromID(int id) const;
218         ///
219         Inset * getInsetFromID(int id) const;
220         ///
221         Paragraph * firstParagraph() const;
222         ///
223         Paragraph * getFirstParagraph(int) const;
224         ///
225         LyXCursor const & cursor(BufferView *) const;
226         ///
227         Paragraph * paragraph() const;
228         ///
229         void paragraph(Paragraph *);
230         ///
231         bool allowSpellcheck() { return true; }
232         ///
233         string const selectNextWordToSpellcheck(BufferView *,
234                                                 float & value) const;
235         void selectSelectedWord(BufferView *);
236         ///
237         void toggleSelection(BufferView *, bool kill_selection);
238         ///
239         bool searchForward(BufferView *, string const &,
240                            bool = true, bool = false);
241         ///
242         bool searchBackward(BufferView *, string const &,
243                             bool = true, bool = false);
244         ///
245         bool checkInsertChar(LyXFont &);
246         ///
247         void getDrawFont(LyXFont &) const;
248         ///
249         void appendParagraphs(BufferParams const & bparams, Paragraph *);
250         ///
251         //
252         // Public structures and variables
253         ///
254         mutable int need_update;
255
256 protected:
257         ///
258         void updateLocal(BufferView *, int what, bool mark_dirty) const;
259         /// set parameters for an initial lock of this inset
260         void lockInset(BufferView *);
261         /// lock an inset inside this one
262         void lockInset(BufferView *, UpdatableInset *);
263         ///
264         mutable int drawTextXOffset;
265         ///
266         mutable int drawTextYOffset;
267         ///
268         bool autoBreakRows;
269         ///
270         DrawFrame drawFrame_;
271         ///
272         LColor::color frame_color;
273
274 private:
275         ///
276         struct InnerCache {
277                 ///
278                 InnerCache(boost::shared_ptr<LyXText>);
279                 ///
280                 boost::shared_ptr<LyXText> text;
281                 ///
282                 bool remove;
283         };
284         ///
285         typedef std::map<BufferView *, InnerCache> Cache;
286         ///
287         typedef Cache::value_type value_type;
288         ///
289         int beginningOfMainBody(Buffer const *, Paragraph * par) const;
290         ///
291         UpdatableInset::RESULT moveRight(BufferView *,
292                                          bool activate_inset = true,
293                                          bool selecting = false);
294         ///
295         UpdatableInset::RESULT moveLeft(BufferView *,
296                                         bool activate_inset = true,
297                                         bool selecting = false);
298         ///
299         UpdatableInset::RESULT moveRightIntern(BufferView *, bool front,
300                                                bool activate_inset = true,
301                                                bool selecting = false);
302         ///
303         UpdatableInset::RESULT moveLeftIntern(BufferView *, bool front,
304                                               bool activate_inset = true,
305                                               bool selecting = false);
306
307         ///
308         UpdatableInset::RESULT moveUp(BufferView *);
309         ///
310         UpdatableInset::RESULT moveDown(BufferView *);
311         ///
312         void setCharFont(Buffer const *, int pos, LyXFont const & font);
313         ///
314         bool checkAndActivateInset(BufferView * bv, bool front);
315         ///
316         bool checkAndActivateInset(BufferView * bv, int x = 0, int y = 0,
317                                    mouse_button::state button = mouse_button::none);
318         ///
319         void removeNewlines();
320         ///
321         int cx(BufferView *) const;
322         ///
323         int cix(BufferView *) const;
324         ///
325         int cy(BufferView *) const;
326         ///
327         int ciy(BufferView *) const;
328         ///
329         lyx::pos_type cpos(BufferView *) const;
330         ///
331         Paragraph * cpar(BufferView *) const;
332         ///
333         bool cboundary(BufferView *) const;
334         ///
335         Row * crow(BufferView *) const;
336         ///
337         void drawFrame(Painter &, bool cleared) const;
338         ///
339         void clearFrame(Painter &, bool cleared) const;
340         ///
341         void clearInset(BufferView *, int baseline, bool & cleared) const;
342         ///
343         void saveLyXTextState(LyXText *) const;
344         ///
345         void restoreLyXTextState(BufferView *, LyXText *) const;
346         ///
347         void reinitLyXText() const;
348         ///
349         void collapseParagraphs(BufferView *) const;
350
351         /* Private structures and variables */
352         ///
353         Paragraph * par;
354         ///
355         mutable bool locked;
356         ///
357         mutable int insetAscent;
358         ///
359         mutable int insetDescent;
360         ///
361         mutable int insetWidth;
362         ///
363         mutable int top_y;
364         ///
365         Paragraph * inset_par;
366         ///
367         lyx::pos_type inset_pos;
368         ///
369         bool inset_boundary;
370         ///
371         mutable int inset_x;
372         ///
373         mutable int inset_y;
374         ///
375         mutable int old_max_width;
376         ///
377         bool no_selection;
378         ///
379         mutable float xpos;
380         ///
381         UpdatableInset * the_locking_inset;
382         ///
383         mutable Paragraph * old_par;
384         /// The cache.
385         mutable Cache cache;
386         ///
387         mutable int last_drawn_width;
388         ///
389         mutable bool frame_is_visible;
390         ///
391         mutable BufferView * cached_bview;
392         ///
393         mutable boost::shared_ptr<LyXText> cached_text;
394         ///
395         struct save_state {
396                 Paragraph * lpar;
397                 Paragraph * selstartpar;
398                 Paragraph * selendpar;
399                 lyx::pos_type pos;
400                 lyx::pos_type selstartpos;
401                 lyx::pos_type selendpos;
402                 bool boundary;
403                 bool selstartboundary;
404                 bool selendboundary;
405                 bool selection;
406                 bool mark_set;
407                 bool refresh;
408         };
409         ///
410         mutable save_state sstate;
411
412         ///
413         // this is needed globally so we know that we're using it actually and
414         // so the LyXText-Cache is not erased until used!
415         mutable LyXText * lt;
416         ///
417         // to remember old painted frame dimensions to clear it on the right spot!
418         ///
419         mutable int frame_x;
420         mutable int frame_y;
421         mutable int frame_w;
422         mutable int frame_h;
423         ///
424         bool in_update; /* as update is not reentrant! */
425         mutable BufferView * do_resize;
426         mutable bool do_reinit;
427         mutable bool in_insetAllowed;
428         ///
429         // these are used to check for mouse movement in Motion selection code
430         ///
431         int mouse_x;
432         int mouse_y;
433 };
434 #endif