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