]> git.lyx.org Git - features.git/blob - src/insets/insettext.h
2003-02-13 John Levon <levon@movementarian.org>
[features.git] / src / insets / insettext.h
1 // -*- C++ -*-
2 /**
3  * \file insettext.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Jürgen Vigna
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #ifndef INSETTEXT_H
13 #define INSETTEXT_H
14
15
16 #include "inset.h"
17 #include "LString.h"
18 #include "LColor.h"
19 #include "ParagraphList.h"
20
21 #include "support/types.h"
22
23 #include <boost/shared_ptr.hpp>
24
25 class Painter;
26 class BufferView;
27 class Buffer;
28 class BufferParams;
29 class LyXCursor;
30 class LyXText;
31 class Row;
32 class Paragraph;
33
34 /**
35  A text inset is like a TeX box to write full text
36  (including styles and other insets) in a given space.
37  @author: Jürgen Vigna
38  */
39 class InsetText : public UpdatableInset {
40 public:
41         ///
42         /// numbers need because of test if codeA < codeB
43         ///
44         enum UpdateCodes {
45                 ///
46                 NONE = 0,
47                 ///
48                 CURSOR = 1,
49                 ///
50                 CLEAR_FRAME = 2,
51                 ///
52                 DRAW_FRAME = 4,
53                 ///
54                 SELECTION = 8,
55                 ///
56                 CURSOR_PAR = 16,
57                 ///
58                 FULL = 32,
59                 ///
60                 INIT = 64
61         };
62         ///
63         enum DrawFrame {
64                 ///
65                 NEVER = 0,
66                 ///
67                 LOCKED,
68                 ///
69                 ALWAYS
70         };
71         ///
72         InsetText(BufferParams const &);
73         ///
74         explicit
75         InsetText(InsetText const &, bool same_id = false);
76         ///
77         ~InsetText();
78         ///
79         Inset * clone(Buffer const &, bool same_id = false) const;
80         ///
81         InsetText & operator=(InsetText const & it);
82         /// empty inset to empty par, or just mark as erased
83         void clear(bool just_mark_erased);
84         ///
85         void read(Buffer const *, LyXLex &);
86         ///
87         void write(Buffer const *, std::ostream &) const;
88         ///
89         int ascent(BufferView *, LyXFont const &) const;
90         ///
91         int descent(BufferView *, LyXFont const &) const;
92         ///
93         int width(BufferView *, LyXFont const & f) const;
94         ///
95         int textWidth(BufferView *, bool fordraw = false) const;
96         ///
97         void draw(BufferView *, LyXFont const &, int , float &, bool) const;
98         ///
99         void update(BufferView *, LyXFont const &, bool = false);
100         ///
101         void setUpdateStatus(BufferView *, int what) const;
102         ///
103         string const editMessage() const;
104         ///
105         void edit(BufferView *, int, int, mouse_button::state);
106         ///
107         void edit(BufferView *, bool front = true);
108         ///
109         bool isTextInset() const { return true; }
110         ///
111         bool doClearArea() const;
112         ///
113         void insetUnlock(BufferView *);
114         ///
115         bool lockInsetInInset(BufferView *, UpdatableInset *);
116         ///
117         bool unlockInsetInInset(BufferView *,
118                                 UpdatableInset *, bool lr = false);
119         ///
120         bool updateInsetInInset(BufferView *, Inset *);
121         ///
122         RESULT localDispatch(FuncRequest const &);
123         ///
124         int latex(Buffer const *, std::ostream &,
125                   bool fragile, bool free_spc) const;
126         ///
127         int ascii(Buffer const *, std::ostream &, int linelen) const;
128         ///
129         int linuxdoc(Buffer const *, std::ostream &) const { return 0; }
130         ///
131         int docbook(Buffer const *, std::ostream &, bool mixcont) const ;
132         ///
133         void validate(LaTeXFeatures & features) const;
134         ///
135         Inset::Code lyxCode() const { return Inset::TEXT_CODE; }
136         ///
137         void getCursorPos(BufferView *, int & x, int & y) const;
138         ///
139         int insetInInsetY() const;
140         ///
141         void toggleInsetCursor(BufferView *);
142         ///
143         void showInsetCursor(BufferView *, bool show = true);
144         ///
145         void hideInsetCursor(BufferView *);
146         ///
147         void fitInsetCursor(BufferView *) const;
148         ///
149         bool insertInset(BufferView *, Inset *);
150         ///
151         bool insetAllowed(Inset::Code) const;
152         ///
153         UpdatableInset * getLockingInset() const;
154         ///
155         UpdatableInset * getFirstLockingInsetOfType(Inset::Code);
156         ///
157         void setFont(BufferView *, LyXFont const &,
158                      bool toggleall = false,
159                      bool selectall = false);
160         ///
161         int getMaxWidth(BufferView *, UpdatableInset const *) const;
162         ///
163         void init(InsetText const * ins = 0, bool same_id = false);
164         ///
165         void writeParagraphData(Buffer const *, std::ostream &) const;
166         ///
167         void setParagraphData(Paragraph *, bool same_id = false);
168         ///
169         void setText(string const &, LyXFont const &);
170         ///
171         void setAutoBreakRows(bool);
172         ///
173         bool getAutoBreakRows() const { return autoBreakRows; }
174         ///
175         void setDrawFrame(BufferView *, DrawFrame);
176         ///
177         void setFrameColor(BufferView *, LColor::color);
178         ///
179         LyXText * getLyXText(BufferView const *,
180                              bool const recursive = false) const;
181         ///
182         void deleteLyXText(BufferView *, bool recursive = true) const;
183         ///
184         void resizeLyXText(BufferView *, bool force = false) const;
185         ///
186         bool showInsetDialog(BufferView *) const;
187         ///
188         std::vector<string> const getLabelList() const;
189         ///
190         bool nodraw() const;
191         ///
192         int scroll(bool recursive = true) const;
193         ///
194         void scroll(BufferView * bv, float sx) const {
195                 UpdatableInset::scroll(bv, sx);
196         }
197         ///
198         void scroll(BufferView * bv, int offset) const {
199                 UpdatableInset::scroll(bv, offset);
200         }
201         ///
202         void selectAll(BufferView * bv);
203         ///
204         void clearSelection(BufferView * bv);
205         ///
206         Paragraph * getParFromID(int id) const;
207         ///
208         Inset * getInsetFromID(int id) const;
209         ///
210         Paragraph * firstParagraph() const;
211         ///
212         Paragraph * getFirstParagraph(int) const;
213         ///
214         LyXCursor const & cursor(BufferView *) const;
215         ///
216         Paragraph * paragraph() const;
217         ///
218         void paragraph(Paragraph *);
219         ///
220         bool allowSpellcheck() const { return true; }
221         ///
222         WordLangTuple const
223         selectNextWordToSpellcheck(BufferView *, float & value) const;
224         ///
225         void selectSelectedWord(BufferView *);
226         ///
227         void toggleSelection(BufferView *, bool kill_selection);
228  
229         /// mark as erased for change tracking
230         void markErased() { clear(true); };
231         /**
232          * Mark as new. Used when pasting in tabular, and adding rows
233          * or columns. Note that pasting will ensure that tracking already
234          * happens, and this just resets the changes for the copied text,
235          * whereas for row/col add, we need to start tracking changes
236          * for the (empty) paragraph contained.
237          */
238         void markNew(bool track_changes = false);
239         /// find next change
240         bool nextChange(BufferView *, lyx::pos_type & length);
241  
242         ///
243         bool searchForward(BufferView *, string const &,
244                            bool = true, bool = false);
245         ///
246         bool searchBackward(BufferView *, string const &,
247                             bool = true, bool = false);
248         ///
249         bool checkInsertChar(LyXFont &);
250         ///
251         void getDrawFont(LyXFont &) const;
252         /// append text onto the existing text
253         void appendParagraphs(BufferParams const & bp, Paragraph *);
254  
255         ///
256         void addPreview(grfx::PreviewLoader &) const;
257
258         //
259         // Public structures and variables
260         ///
261         mutable int need_update;
262
263 protected:
264         ///
265         void updateLocal(BufferView *, int what, bool mark_dirty) const;
266         /// set parameters for an initial lock of this inset
267         void lockInset(BufferView *);
268         /// lock an inset inside this one
269         void lockInset(BufferView *, UpdatableInset *);
270         ///
271         mutable int drawTextXOffset;
272         ///
273         mutable int drawTextYOffset;
274         ///
275         bool autoBreakRows;
276         ///
277         DrawFrame drawFrame_;
278         ///
279         LColor::color frame_color;
280
281 private:
282         ///
283         void lfunMousePress(FuncRequest const &);
284         ///
285         bool lfunMouseRelease(FuncRequest const &);
286         ///
287         void lfunMouseMotion(FuncRequest const &);
288
289         ///
290         struct InnerCache {
291                 ///
292                 InnerCache(boost::shared_ptr<LyXText>);
293                 ///
294                 boost::shared_ptr<LyXText> text;
295                 ///
296                 bool remove;
297         };
298         ///
299         typedef std::map<BufferView *, InnerCache> Cache;
300         ///
301         typedef Cache::value_type value_type;
302         ///
303         RESULT moveRight(BufferView *,
304                                          bool activate_inset = true,
305                                          bool selecting = false);
306         ///
307         RESULT moveLeft(BufferView *,
308                                         bool activate_inset = true,
309                                         bool selecting = false);
310         ///
311         RESULT moveRightIntern(BufferView *, bool front,
312                                                bool activate_inset = true,
313                                                bool selecting = false);
314         ///
315         RESULT moveLeftIntern(BufferView *, bool front,
316                                               bool activate_inset = true,
317                                               bool selecting = false);
318
319         ///
320         RESULT moveUp(BufferView *);
321         ///
322         RESULT moveDown(BufferView *);
323         ///
324         void setCharFont(Buffer const *, int pos, LyXFont const & font);
325         ///
326         bool checkAndActivateInset(BufferView * bv, bool front);
327         ///
328         bool checkAndActivateInset(BufferView * bv, int x = 0, int y = 0,
329                                    mouse_button::state button = mouse_button::none);
330         ///
331         void removeNewlines();
332         ///
333         int cx(BufferView *) const;
334         ///
335         int cix(BufferView *) const;
336         ///
337         int cy(BufferView *) const;
338         ///
339         int ciy(BufferView *) const;
340         ///
341         lyx::pos_type cpos(BufferView *) const;
342         ///
343         Paragraph * cpar(BufferView *) const;
344         ///
345         bool cboundary(BufferView *) const;
346         ///
347         Row * crow(BufferView *) const;
348         ///
349         void drawFrame(Painter &, bool cleared) const;
350         ///
351         void clearFrame(Painter &, bool cleared) const;
352         ///
353         void clearInset(BufferView *, int baseline, bool & cleared) const;
354         ///
355         void saveLyXTextState(LyXText *) const;
356         ///
357         void restoreLyXTextState(BufferView *, LyXText *) const;
358         ///
359         void reinitLyXText() const;
360         ///
361         void collapseParagraphs(BufferView *) const;
362
363         /* Private structures and variables */
364         ///
365         ParagraphList paragraphs;
366         ///
367         mutable bool locked;
368         ///
369         mutable int insetAscent;
370         ///
371         mutable int insetDescent;
372         ///
373         mutable int insetWidth;
374         ///
375         mutable int top_y;
376         ///
377         Paragraph * inset_par;
378         ///
379         lyx::pos_type inset_pos;
380         ///
381         bool inset_boundary;
382         ///
383         mutable int inset_x;
384         ///
385         mutable int inset_y;
386         ///
387         mutable int old_max_width;
388         ///
389         bool no_selection;
390         ///
391         UpdatableInset * the_locking_inset;
392         ///
393         mutable Paragraph * old_par;
394         /// The cache.
395         mutable Cache cache;
396         ///
397         mutable int last_drawn_width;
398         ///
399         mutable bool frame_is_visible;
400         ///
401         mutable BufferView * cached_bview;
402         ///
403         mutable boost::shared_ptr<LyXText> cached_text;
404         ///
405         struct save_state {
406                 Paragraph * lpar;
407                 Paragraph * selstartpar;
408                 Paragraph * selendpar;
409                 lyx::pos_type pos;
410                 lyx::pos_type selstartpos;
411                 lyx::pos_type selendpos;
412                 bool boundary;
413                 bool selstartboundary;
414                 bool selendboundary;
415                 bool selection;
416                 bool mark_set;
417                 bool refresh;
418         };
419         ///
420         mutable save_state sstate;
421
422         ///
423         // this is needed globally so we know that we're using it actually and
424         // so the LyXText-Cache is not erased until used!
425         mutable LyXText * lt;
426         ///
427         // to remember old painted frame dimensions to clear it on the right spot!
428         ///
429         mutable int frame_x;
430         mutable int frame_y;
431         mutable int frame_w;
432         mutable int frame_h;
433         ///
434         bool in_update; /* as update is not reentrant! */
435         mutable BufferView * do_resize;
436         mutable bool do_reinit;
437         mutable bool in_insetAllowed;
438         ///
439         // these are used to check for mouse movement in Motion selection code
440         ///
441         int mouse_x;
442         int mouse_y;
443 };
444 #endif