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