]> git.lyx.org Git - features.git/blob - src/insets/insettext.h
LColor::Pimpl
[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 #include <map>
26
27 class Painter;
28 class BufferView;
29 class Buffer;
30 class BufferParams;
31 class LyXCursor;
32 class LyXText;
33 class Row;
34 class Paragraph;
35
36 /**
37  A text inset is like a TeX box to write full text
38  (including styles and other insets) in a given space.
39  @author: Jürgen Vigna
40  */
41 class InsetText : public UpdatableInset {
42 public:
43         ///
44         /// numbers need because of test if codeA < codeB
45         ///
46         enum UpdateCodes {
47                 ///
48                 NONE = 0,
49                 ///
50                 CURSOR = 1,
51                 ///
52                 DRAW_FRAME = 2,
53                 ///
54                 SELECTION = 4,
55                 ///
56                 CURSOR_PAR = 8,
57                 ///
58                 FULL = 16,
59                 ///
60                 INIT = 32
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 &) const;
98         ///
99         void update(BufferView *, 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         void insetUnlock(BufferView *);
112         ///
113         bool lockInsetInInset(BufferView *, UpdatableInset *);
114         ///
115         bool unlockInsetInInset(BufferView *,
116                                 UpdatableInset *, bool lr = false);
117         ///
118         bool updateInsetInInset(BufferView *, Inset *);
119         ///
120         RESULT localDispatch(FuncRequest const &);
121         ///
122         int latex(Buffer const *, std::ostream &,
123                   bool fragile, bool free_spc) const;
124         ///
125         int ascii(Buffer const *, std::ostream &, int linelen) const;
126         ///
127         int linuxdoc(Buffer const *, std::ostream &) const { return 0; }
128         ///
129         int docbook(Buffer const *, std::ostream &, bool mixcont) const ;
130         ///
131         void validate(LaTeXFeatures & features) const;
132         ///
133         Inset::Code lyxCode() const { return Inset::TEXT_CODE; }
134         /// FIXME, document
135         void getCursorPos(BufferView *, int & x, int & y) const;
136         /// Get the absolute document x,y of the cursor
137         virtual void getCursor(BufferView &, int &, int &) const;
138         ///
139         int insetInInsetY() const;
140         ///
141         void fitInsetCursor(BufferView *) const;
142         ///
143         bool insertInset(BufferView *, Inset *);
144         ///
145         bool insetAllowed(Inset::Code) const;
146         ///
147         UpdatableInset * getLockingInset() const;
148         ///
149         UpdatableInset * getFirstLockingInsetOfType(Inset::Code);
150         ///
151         void setFont(BufferView *, LyXFont const &,
152                      bool toggleall = false,
153                      bool selectall = false);
154         ///
155         int getMaxWidth(BufferView *, UpdatableInset const *) const;
156         ///
157         void init(InsetText const * ins = 0, bool same_id = false);
158         ///
159         void writeParagraphData(Buffer const *, std::ostream &) const;
160         ///
161         void setParagraphData(ParagraphList const &, bool same_id = false);
162         ///
163         void setText(string const &, LyXFont const &);
164         ///
165         void setAutoBreakRows(bool);
166         ///
167         bool getAutoBreakRows() const { return autoBreakRows; }
168         ///
169         void setDrawFrame(BufferView *, DrawFrame);
170         ///
171         void setFrameColor(BufferView *, LColor::color);
172         ///
173         LyXText * getLyXText(BufferView const *,
174                              bool const recursive = false) const;
175         ///
176         void deleteLyXText(BufferView *, bool recursive = true) const;
177         ///
178         void resizeLyXText(BufferView *, bool force = false) const;
179         ///
180         bool showInsetDialog(BufferView *) const;
181         ///
182         std::vector<string> const getLabelList() const;
183         ///
184         bool nodraw() const;
185         ///
186         int scroll(bool recursive = true) const;
187         ///
188         void scroll(BufferView * bv, float sx) const {
189                 UpdatableInset::scroll(bv, sx);
190         }
191         ///
192         void scroll(BufferView * bv, int offset) const {
193                 UpdatableInset::scroll(bv, offset);
194         }
195         ///
196         void selectAll(BufferView * bv);
197         ///
198         void clearSelection(BufferView * bv);
199         ///
200         Inset * getInsetFromID(int id) const;
201         ///
202         ParagraphList * getParagraphs(int) const;
203         ///
204         LyXCursor const & cursor(BufferView *) const;
205         ///
206         void paragraph(Paragraph *);
207         ///
208         bool allowSpellcheck() const { return true; }
209         ///
210         WordLangTuple const
211         selectNextWordToSpellcheck(BufferView *, float & value) const;
212         ///
213         void selectSelectedWord(BufferView *);
214         ///
215         void toggleSelection(BufferView *, bool kill_selection);
216
217         /// mark as erased for change tracking
218         void markErased() { clear(true); };
219         /**
220          * Mark as new. Used when pasting in tabular, and adding rows
221          * or columns. Note that pasting will ensure that tracking already
222          * happens, and this just resets the changes for the copied text,
223          * whereas for row/col add, we need to start tracking changes
224          * for the (empty) paragraph contained.
225          */
226         void markNew(bool track_changes = false);
227         /// find next change
228         bool nextChange(BufferView *, lyx::pos_type & length);
229
230         ///
231         bool searchForward(BufferView *, string const &,
232                            bool = true, bool = false);
233         ///
234         bool searchBackward(BufferView *, string const &,
235                             bool = true, bool = false);
236         ///
237         bool checkInsertChar(LyXFont &);
238         ///
239         void getDrawFont(LyXFont &) const;
240         /// append text onto the existing text
241         void appendParagraphs(Buffer * bp, ParagraphList &);
242
243         ///
244         void addPreview(grfx::PreviewLoader &) const;
245
246         //
247         // Public structures and variables
248         ///
249         mutable int need_update;
250
251         bool haveParagraphs() const {
252                 return true;
253         }
254         ///
255         ParagraphList paragraphs;
256 protected:
257         ///
258         void updateLocal(BufferView *, int what, bool mark_dirty);
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         void lfunMousePress(FuncRequest const &);
277         ///
278         bool lfunMouseRelease(FuncRequest const &);
279         ///
280         void lfunMouseMotion(FuncRequest const &);
281
282         ///
283         struct InnerCache {
284                 ///
285                 InnerCache(boost::shared_ptr<LyXText>);
286                 ///
287                 boost::shared_ptr<LyXText> text;
288                 ///
289                 bool remove;
290         };
291         ///
292         typedef std::map<BufferView *, InnerCache> Cache;
293         ///
294         typedef Cache::value_type value_type;
295         ///
296         RESULT moveRight(BufferView *,
297                                          bool activate_inset = true,
298                                          bool selecting = false);
299         ///
300         RESULT moveLeft(BufferView *,
301                                         bool activate_inset = true,
302                                         bool selecting = false);
303         ///
304         RESULT moveRightIntern(BufferView *, bool front,
305                                                bool activate_inset = true,
306                                                bool selecting = false);
307         ///
308         RESULT moveLeftIntern(BufferView *, bool front,
309                                               bool activate_inset = true,
310                                               bool selecting = false);
311
312         ///
313         RESULT moveUp(BufferView *);
314         ///
315         RESULT moveDown(BufferView *);
316         ///
317         void setCharFont(Buffer const *, int pos, LyXFont const & font);
318         ///
319         bool checkAndActivateInset(BufferView * bv, bool front);
320         ///
321         bool checkAndActivateInset(BufferView * bv, int x = 0, int y = 0,
322                                    mouse_button::state button = mouse_button::none);
323         ///
324         void removeNewlines();
325         ///
326         int cx(BufferView *) const;
327         ///
328         int cix(BufferView *) const;
329         ///
330         int cy(BufferView *) const;
331         ///
332         int ciy(BufferView *) const;
333         ///
334         lyx::pos_type cpos(BufferView *) const;
335         ///
336         ParagraphList::iterator cpar(BufferView *) const;
337         ///
338         bool cboundary(BufferView *) const;
339         ///
340         RowList::iterator crow(BufferView *) const;
341         ///
342         void drawFrame(Painter &, int x) const;
343         ///
344         void clearInset(BufferView *, int start_x, int baseline) const;
345         ///
346         void saveLyXTextState(LyXText *) const;
347         ///
348         void restoreLyXTextState(LyXText *) const;
349         ///
350         void reinitLyXText() const;
351         ///
352         void collapseParagraphs(BufferView *);
353
354         /* Private structures and variables */
355         ///
356         mutable bool locked;
357         ///
358         mutable int insetAscent;
359         ///
360         mutable int insetDescent;
361         ///
362         mutable int insetWidth;
363         ///
364         mutable int top_y;
365         ///
366         ParagraphList::iterator inset_par;
367         ///
368         lyx::pos_type inset_pos;
369         ///
370         bool inset_boundary;
371         ///
372         mutable int inset_x;
373         ///
374         mutable int inset_y;
375         ///
376         mutable int old_max_width;
377         ///
378         bool no_selection;
379         ///
380         UpdatableInset * the_locking_inset;
381         ///
382         mutable ParagraphList::iterator old_par;
383         /// The cache.
384         mutable Cache cache;
385         ///
386         mutable int last_drawn_width;
387         ///
388         mutable BufferView * cached_bview;
389         ///
390         mutable boost::shared_ptr<LyXText> cached_text;
391         ///
392         struct save_state {
393                 Paragraph * lpar;
394                 Paragraph * selstartpar;
395                 Paragraph * selendpar;
396                 lyx::pos_type pos;
397                 lyx::pos_type selstartpos;
398                 lyx::pos_type selendpos;
399                 bool boundary;
400                 bool selstartboundary;
401                 bool selendboundary;
402                 bool selection;
403                 bool mark_set;
404         };
405         ///
406         mutable save_state sstate;
407
408         ///
409         // this is needed globally so we know that we're using it actually and
410         // so the LyXText-Cache is not erased until used!
411         mutable LyXText * lt;
412         ///
413         // to remember old painted frame dimensions to clear it on the right spot!
414         ///
415         mutable int frame_x;
416         mutable int frame_y;
417         mutable int frame_w;
418         mutable int frame_h;
419         ///
420         bool in_update; /* as update is not reentrant! */
421         mutable BufferView * do_resize;
422         mutable bool do_reinit;
423         mutable bool in_insetAllowed;
424         ///
425         // these are used to check for mouse movement in Motion selection code
426         ///
427         int mouse_x;
428         int mouse_y;
429 };
430 #endif