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