]> git.lyx.org Git - features.git/blob - src/insets/insettext.h
33c7d5c363a439c6ef47c351f71763c93858f873
[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
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         Inset * getInsetFromID(int id) const;
207         ///
208         Paragraph * firstParagraph() const;
209         ///
210         Paragraph * getFirstParagraph(int) const;
211         ///
212         LyXCursor const & cursor(BufferView *) const;
213         ///
214         Paragraph * paragraph() const;
215         ///
216         void paragraph(Paragraph *);
217         ///
218         bool allowSpellcheck() const { return true; }
219         ///
220         WordLangTuple const
221         selectNextWordToSpellcheck(BufferView *, float & value) const;
222         ///
223         void selectSelectedWord(BufferView *);
224         ///
225         void toggleSelection(BufferView *, bool kill_selection);
226
227         /// mark as erased for change tracking
228         void markErased() { clear(true); };
229         /**
230          * Mark as new. Used when pasting in tabular, and adding rows
231          * or columns. Note that pasting will ensure that tracking already
232          * happens, and this just resets the changes for the copied text,
233          * whereas for row/col add, we need to start tracking changes
234          * for the (empty) paragraph contained.
235          */
236         void markNew(bool track_changes = false);
237         /// find next change
238         bool nextChange(BufferView *, lyx::pos_type & length);
239
240         ///
241         bool searchForward(BufferView *, string const &,
242                            bool = true, bool = false);
243         ///
244         bool searchBackward(BufferView *, string const &,
245                             bool = true, bool = false);
246         ///
247         bool checkInsertChar(LyXFont &);
248         ///
249         void getDrawFont(LyXFont &) const;
250         /// append text onto the existing text
251         void appendParagraphs(BufferParams const & bp, Paragraph *);
252
253         ///
254         void addPreview(grfx::PreviewLoader &) const;
255
256         //
257         // Public structures and variables
258         ///
259         mutable int need_update;
260
261         ///
262         ParagraphList paragraphs;
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         mutable bool locked;
366         ///
367         mutable int insetAscent;
368         ///
369         mutable int insetDescent;
370         ///
371         mutable int insetWidth;
372         ///
373         mutable int top_y;
374         ///
375         Paragraph * inset_par;
376         ///
377         lyx::pos_type inset_pos;
378         ///
379         bool inset_boundary;
380         ///
381         mutable int inset_x;
382         ///
383         mutable int inset_y;
384         ///
385         mutable int old_max_width;
386         ///
387         bool no_selection;
388         ///
389         UpdatableInset * the_locking_inset;
390         ///
391         mutable Paragraph * old_par;
392         /// The cache.
393         mutable Cache cache;
394         ///
395         mutable int last_drawn_width;
396         ///
397         mutable bool frame_is_visible;
398         ///
399         mutable BufferView * cached_bview;
400         ///
401         mutable boost::shared_ptr<LyXText> cached_text;
402         ///
403         struct save_state {
404                 Paragraph * lpar;
405                 Paragraph * selstartpar;
406                 Paragraph * selendpar;
407                 lyx::pos_type pos;
408                 lyx::pos_type selstartpos;
409                 lyx::pos_type selendpos;
410                 bool boundary;
411                 bool selstartboundary;
412                 bool selendboundary;
413                 bool selection;
414                 bool mark_set;
415                 bool refresh;
416         };
417         ///
418         mutable save_state sstate;
419
420         ///
421         // this is needed globally so we know that we're using it actually and
422         // so the LyXText-Cache is not erased until used!
423         mutable LyXText * lt;
424         ///
425         // to remember old painted frame dimensions to clear it on the right spot!
426         ///
427         mutable int frame_x;
428         mutable int frame_y;
429         mutable int frame_w;
430         mutable int frame_h;
431         ///
432         bool in_update; /* as update is not reentrant! */
433         mutable BufferView * do_resize;
434         mutable bool do_reinit;
435         mutable bool in_insetAllowed;
436         ///
437         // these are used to check for mouse movement in Motion selection code
438         ///
439         int mouse_x;
440         int mouse_y;
441 };
442 #endif