]> git.lyx.org Git - features.git/blob - src/insets/insettext.h
part of Lars' parlist-14-a.diff
[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         ///
134         void getCursorPos(BufferView *, int & x, int & y) const;
135         ///
136         int insetInInsetY() const;
137         ///
138         void toggleInsetCursor(BufferView *);
139         ///
140         void showInsetCursor(BufferView *, bool show = true);
141         ///
142         void hideInsetCursor(BufferView *);
143         ///
144         void fitInsetCursor(BufferView *) const;
145         ///
146         bool insertInset(BufferView *, Inset *);
147         ///
148         bool insetAllowed(Inset::Code) const;
149         ///
150         UpdatableInset * getLockingInset() const;
151         ///
152         UpdatableInset * getFirstLockingInsetOfType(Inset::Code);
153         ///
154         void setFont(BufferView *, LyXFont const &,
155                      bool toggleall = false,
156                      bool selectall = false);
157         ///
158         int getMaxWidth(BufferView *, UpdatableInset const *) const;
159         ///
160         void init(InsetText const * ins = 0, bool same_id = false);
161         ///
162         void writeParagraphData(Buffer const *, std::ostream &) const;
163         ///
164         void setParagraphData(ParagraphList const &, bool same_id = false);
165         ///
166         void setText(string const &, LyXFont const &);
167         ///
168         void setAutoBreakRows(bool);
169         ///
170         bool getAutoBreakRows() const { return autoBreakRows; }
171         ///
172         void setDrawFrame(BufferView *, DrawFrame);
173         ///
174         void setFrameColor(BufferView *, LColor::color);
175         ///
176         LyXText * getLyXText(BufferView const *,
177                              bool const recursive = false) const;
178         ///
179         void deleteLyXText(BufferView *, bool recursive = true) const;
180         ///
181         void resizeLyXText(BufferView *, bool force = false) const;
182         ///
183         bool showInsetDialog(BufferView *) const;
184         ///
185         std::vector<string> const getLabelList() const;
186         ///
187         bool nodraw() const;
188         ///
189         int scroll(bool recursive = true) const;
190         ///
191         void scroll(BufferView * bv, float sx) const {
192                 UpdatableInset::scroll(bv, sx);
193         }
194         ///
195         void scroll(BufferView * bv, int offset) const {
196                 UpdatableInset::scroll(bv, offset);
197         }
198         ///
199         void selectAll(BufferView * bv);
200         ///
201         void clearSelection(BufferView * bv);
202         ///
203         Inset * getInsetFromID(int id) const;
204         ///
205         Paragraph * firstParagraph() const;
206         ///
207         Paragraph * getFirstParagraph(int) const;
208         ///
209         LyXCursor const & cursor(BufferView *) const;
210         ///
211         void paragraph(Paragraph *);
212         ///
213         bool allowSpellcheck() const { return true; }
214         ///
215         WordLangTuple const
216         selectNextWordToSpellcheck(BufferView *, float & value) const;
217         ///
218         void selectSelectedWord(BufferView *);
219         ///
220         void toggleSelection(BufferView *, bool kill_selection);
221
222         /// mark as erased for change tracking
223         void markErased() { clear(true); };
224         /**
225          * Mark as new. Used when pasting in tabular, and adding rows
226          * or columns. Note that pasting will ensure that tracking already
227          * happens, and this just resets the changes for the copied text,
228          * whereas for row/col add, we need to start tracking changes
229          * for the (empty) paragraph contained.
230          */
231         void markNew(bool track_changes = false);
232         /// find next change
233         bool nextChange(BufferView *, lyx::pos_type & length);
234
235         ///
236         bool searchForward(BufferView *, string const &,
237                            bool = true, bool = false);
238         ///
239         bool searchBackward(BufferView *, string const &,
240                             bool = true, bool = false);
241         ///
242         bool checkInsertChar(LyXFont &);
243         ///
244         void getDrawFont(LyXFont &) const;
245         /// append text onto the existing text
246         void appendParagraphs(Buffer * bp, ParagraphList &);
247
248         ///
249         void addPreview(grfx::PreviewLoader &) const;
250
251         //
252         // Public structures and variables
253         ///
254         mutable int need_update;
255
256         bool haveParagraphs() const {
257                 return true;
258         }
259         ///
260         ParagraphList paragraphs;
261 protected:
262         ///
263         void updateLocal(BufferView *, int what, bool mark_dirty);
264         /// set parameters for an initial lock of this inset
265         void lockInset(BufferView *);
266         /// lock an inset inside this one
267         void lockInset(BufferView *, UpdatableInset *);
268         ///
269         mutable int drawTextXOffset;
270         ///
271         mutable int drawTextYOffset;
272         ///
273         bool autoBreakRows;
274         ///
275         DrawFrame drawFrame_;
276         ///
277         LColor::color frame_color;
278
279 private:
280         ///
281         void lfunMousePress(FuncRequest const &);
282         ///
283         bool lfunMouseRelease(FuncRequest const &);
284         ///
285         void lfunMouseMotion(FuncRequest const &);
286
287         ///
288         struct InnerCache {
289                 ///
290                 InnerCache(boost::shared_ptr<LyXText>);
291                 ///
292                 boost::shared_ptr<LyXText> text;
293                 ///
294                 bool remove;
295         };
296         ///
297         typedef std::map<BufferView *, InnerCache> Cache;
298         ///
299         typedef Cache::value_type value_type;
300         ///
301         RESULT moveRight(BufferView *,
302                                          bool activate_inset = true,
303                                          bool selecting = false);
304         ///
305         RESULT moveLeft(BufferView *,
306                                         bool activate_inset = true,
307                                         bool selecting = false);
308         ///
309         RESULT moveRightIntern(BufferView *, bool front,
310                                                bool activate_inset = true,
311                                                bool selecting = false);
312         ///
313         RESULT moveLeftIntern(BufferView *, bool front,
314                                               bool activate_inset = true,
315                                               bool selecting = false);
316
317         ///
318         RESULT moveUp(BufferView *);
319         ///
320         RESULT moveDown(BufferView *);
321         ///
322         void setCharFont(Buffer const *, int pos, LyXFont const & font);
323         ///
324         bool checkAndActivateInset(BufferView * bv, bool front);
325         ///
326         bool checkAndActivateInset(BufferView * bv, int x = 0, int y = 0,
327                                    mouse_button::state button = mouse_button::none);
328         ///
329         void removeNewlines();
330         ///
331         int cx(BufferView *) const;
332         ///
333         int cix(BufferView *) const;
334         ///
335         int cy(BufferView *) const;
336         ///
337         int ciy(BufferView *) const;
338         ///
339         lyx::pos_type cpos(BufferView *) const;
340         ///
341         ParagraphList::iterator cpar(BufferView *) const;
342         ///
343         bool cboundary(BufferView *) const;
344         ///
345         RowList::iterator crow(BufferView *) const;
346         ///
347         void drawFrame(Painter &, int x) const;
348         ///
349         void clearInset(BufferView *, int start_x, int baseline) const;
350         ///
351         void saveLyXTextState(LyXText *) const;
352         ///
353         void restoreLyXTextState(LyXText *) const;
354         ///
355         void reinitLyXText() const;
356         ///
357         void collapseParagraphs(BufferView *);
358
359         /* Private structures and variables */
360         ///
361         mutable bool locked;
362         ///
363         mutable int insetAscent;
364         ///
365         mutable int insetDescent;
366         ///
367         mutable int insetWidth;
368         ///
369         mutable int top_y;
370         ///
371         ParagraphList::iterator inset_par;
372         ///
373         lyx::pos_type inset_pos;
374         ///
375         bool inset_boundary;
376         ///
377         mutable int inset_x;
378         ///
379         mutable int inset_y;
380         ///
381         mutable int old_max_width;
382         ///
383         bool no_selection;
384         ///
385         UpdatableInset * the_locking_inset;
386         ///
387         mutable ParagraphList::iterator old_par;
388         /// The cache.
389         mutable Cache cache;
390         ///
391         mutable int last_drawn_width;
392         ///
393         mutable BufferView * cached_bview;
394         ///
395         mutable boost::shared_ptr<LyXText> cached_text;
396         ///
397         struct save_state {
398                 Paragraph * lpar;
399                 Paragraph * selstartpar;
400                 Paragraph * selendpar;
401                 lyx::pos_type pos;
402                 lyx::pos_type selstartpos;
403                 lyx::pos_type selendpos;
404                 bool boundary;
405                 bool selstartboundary;
406                 bool selendboundary;
407                 bool selection;
408                 bool mark_set;
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