]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.h
use the new mouse LFUNs
[lyx.git] / src / insets / insettext.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  *
5  *           LyX, The Document Processor
6  *
7  *           Copyright 1998 The LyX Team.
8  *
9  *
10  *======================================================
11  */
12 // The pristine updatable inset: Text
13
14
15 #ifndef INSETTEXT_H
16 #define INSETTEXT_H
17
18 #ifdef __GNUG__
19 #pragma interface
20 #endif
21
22 #include "inset.h"
23 #include "LString.h"
24 #include "LColor.h"
25 #include "ParagraphList.h"
26
27 #include "support/types.h"
28
29 #include <boost/shared_ptr.hpp>
30
31 class Painter;
32 class BufferView;
33 class Buffer;
34 class BufferParams;
35 class LyXCursor;
36 class LyXText;
37 class Row;
38 class Paragraph;
39
40 /**
41  A text inset is like a TeX box to write full text
42  (including styles and other insets) in a given space.
43  @author: Jürgen Vigna
44  */
45 class InsetText : public UpdatableInset {
46 public:
47         ///
48         /// numbers need because of test if codeA < codeB
49         ///
50         enum UpdateCodes {
51                 ///
52                 NONE = 0,
53                 ///
54                 CURSOR = 1,
55                 ///
56                 CLEAR_FRAME = 2,
57                 ///
58                 DRAW_FRAME = 4,
59                 ///
60                 SELECTION = 8,
61                 ///
62                 CURSOR_PAR = 16,
63                 ///
64                 FULL = 32,
65                 ///
66                 INIT = 64
67         };
68         ///
69         enum DrawFrame {
70                 ///
71                 NEVER = 0,
72                 ///
73                 LOCKED,
74                 ///
75                 ALWAYS
76         };
77         ///
78         InsetText(BufferParams const &);
79         ///
80         explicit
81         InsetText(InsetText const &, bool same_id = false);
82         ///
83         ~InsetText();
84         ///
85         Inset * clone(Buffer const &, bool same_id = false) const;
86         ///
87         InsetText & operator=(InsetText const & it);
88         ///
89         void clear();
90         ///
91         void read(Buffer const *, LyXLex &);
92         ///
93         void write(Buffer const *, std::ostream &) const;
94         ///
95         int ascent(BufferView *, LyXFont const &) const;
96         ///
97         int descent(BufferView *, LyXFont const &) const;
98         ///
99         int width(BufferView *, LyXFont const & f) const;
100         ///
101         int textWidth(BufferView *, bool fordraw = false) const;
102         ///
103         void draw(BufferView *, LyXFont const &, int , float &, bool) const;
104         ///
105         void update(BufferView *, LyXFont const &, bool = false);
106         ///
107         void setUpdateStatus(BufferView *, int what) const;
108         ///
109         string const editMessage() const;
110         ///
111         void edit(BufferView *, int, int, mouse_button::state);
112         ///
113         void edit(BufferView *, bool front = true);
114         ///
115         bool isTextInset() const { return true; }
116         ///
117         bool doClearArea() const;
118         ///
119         void insetUnlock(BufferView *);
120         ///
121         bool lockInsetInInset(BufferView *, UpdatableInset *);
122         ///
123         bool unlockInsetInInset(BufferView *,
124                                 UpdatableInset *, bool lr = false);
125         ///
126         bool updateInsetInInset(BufferView *, Inset *);
127         ///
128         RESULT localDispatch(FuncRequest const &);
129         ///
130         int latex(Buffer const *, std::ostream &,
131                   bool fragile, bool free_spc) const;
132         ///
133         int ascii(Buffer const *, std::ostream &, int linelen) const;
134         ///
135         int linuxdoc(Buffer const *, std::ostream &) const { return 0; }
136         ///
137         int docbook(Buffer const *, std::ostream &, bool mixcont) const ;
138         ///
139         void validate(LaTeXFeatures & features) const;
140         ///
141         Inset::Code lyxCode() const { return Inset::TEXT_CODE; }
142         ///
143         void getCursorPos(BufferView *, int & x, int & y) const;
144         ///
145         int insetInInsetY() const;
146         ///
147         void toggleInsetCursor(BufferView *);
148         ///
149         void showInsetCursor(BufferView *, bool show = true);
150         ///
151         void hideInsetCursor(BufferView *);
152         ///
153         void fitInsetCursor(BufferView *) const;
154         ///
155         bool insertInset(BufferView *, Inset *);
156         ///
157         bool insetAllowed(Inset::Code) const;
158         ///
159         UpdatableInset * getLockingInset() const;
160         ///
161         UpdatableInset * getFirstLockingInsetOfType(Inset::Code);
162         ///
163         void setFont(BufferView *, LyXFont const &,
164                      bool toggleall = false,
165                      bool selectall = false);
166         ///
167         int getMaxWidth(BufferView *, UpdatableInset const *) const;
168         ///
169         void init(InsetText const * ins = 0, bool same_id = false);
170         ///
171         void writeParagraphData(Buffer const *, std::ostream &) const;
172         ///
173         void setParagraphData(Paragraph *, bool same_id = false);
174         ///
175         void setText(string const &, LyXFont const &);
176         ///
177         void setAutoBreakRows(bool);
178         ///
179         bool getAutoBreakRows() const { return autoBreakRows; }
180         ///
181         void setDrawFrame(BufferView *, DrawFrame);
182         ///
183         void setFrameColor(BufferView *, LColor::color);
184         ///
185         LyXText * getLyXText(BufferView const *,
186                              bool const recursive = false) const;
187         ///
188         void deleteLyXText(BufferView *, bool recursive = true) const;
189         ///
190         void resizeLyXText(BufferView *, bool force = false) const;
191         ///
192         bool showInsetDialog(BufferView *) const;
193         ///
194         std::vector<string> const getLabelList() const;
195         ///
196         bool nodraw() const;
197         ///
198         int scroll(bool recursive = true) const;
199         ///
200         void scroll(BufferView * bv, float sx) const {
201                 UpdatableInset::scroll(bv, sx);
202         }
203         ///
204         void scroll(BufferView * bv, int offset) const {
205                 UpdatableInset::scroll(bv, offset);
206         }
207         ///
208         void selectAll(BufferView * bv);
209         ///
210         void clearSelection(BufferView * bv);
211         ///
212         Paragraph * getParFromID(int id) const;
213         ///
214         Inset * getInsetFromID(int id) const;
215         ///
216         Paragraph * firstParagraph() const;
217         ///
218         Paragraph * getFirstParagraph(int) const;
219         ///
220         LyXCursor const & cursor(BufferView *) const;
221         ///
222         Paragraph * paragraph() const;
223         ///
224         void paragraph(Paragraph *);
225         ///
226         bool allowSpellcheck() { return true; }
227         ///
228         WordLangTuple const
229         selectNextWordToSpellcheck(BufferView *, float & value) const;
230         ///
231         void selectSelectedWord(BufferView *);
232         ///
233         void toggleSelection(BufferView *, bool kill_selection);
234         ///
235         bool searchForward(BufferView *, string const &,
236                            bool = true, bool = false);
237         ///
238         bool searchBackward(BufferView *, string const &,
239                             bool = true, bool = false);
240         ///
241         bool checkInsertChar(LyXFont &);
242         ///
243         void getDrawFont(LyXFont &) const;
244         ///
245         void appendParagraphs(BufferParams const & bparams, Paragraph *);
246         ///
247         void addPreview(grfx::PreviewLoader &) const;
248
249         //
250         // Public structures and variables
251         ///
252         mutable int need_update;
253
254 protected:
255         ///
256         void updateLocal(BufferView *, int what, bool mark_dirty) const;
257         /// set parameters for an initial lock of this inset
258         void lockInset(BufferView *);
259         /// lock an inset inside this one
260         void lockInset(BufferView *, UpdatableInset *);
261         ///
262         mutable int drawTextXOffset;
263         ///
264         mutable int drawTextYOffset;
265         ///
266         bool autoBreakRows;
267         ///
268         DrawFrame drawFrame_;
269         ///
270         LColor::color frame_color;
271
272 private:
273         ///
274         void lfunMousePress(FuncRequest const &);
275         ///
276         bool lfunMouseRelease(FuncRequest const &);
277         ///
278         void lfunMouseMotion(FuncRequest const &);
279
280         ///
281         struct InnerCache {
282                 ///
283                 InnerCache(boost::shared_ptr<LyXText>);
284                 ///
285                 boost::shared_ptr<LyXText> text;
286                 ///
287                 bool remove;
288         };
289         ///
290         typedef std::map<BufferView *, InnerCache> Cache;
291         ///
292         typedef Cache::value_type value_type;
293         ///
294         int beginningOfMainBody(Paragraph * par) const;
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         Paragraph * cpar(BufferView *) const;
337         ///
338         bool cboundary(BufferView *) const;
339         ///
340         Row * crow(BufferView *) const;
341         ///
342         void drawFrame(Painter &, bool cleared) const;
343         ///
344         void clearFrame(Painter &, bool cleared) const;
345         ///
346         void clearInset(BufferView *, int baseline, bool & cleared) const;
347         ///
348         void saveLyXTextState(LyXText *) const;
349         ///
350         void restoreLyXTextState(BufferView *, LyXText *) const;
351         ///
352         void reinitLyXText() const;
353         ///
354         void collapseParagraphs(BufferView *) const;
355
356         /* Private structures and variables */
357         ///
358         ParagraphList paragraphs;
359         ///
360         mutable bool locked;
361         ///
362         mutable int insetAscent;
363         ///
364         mutable int insetDescent;
365         ///
366         mutable int insetWidth;
367         ///
368         mutable int top_y;
369         ///
370         Paragraph * inset_par;
371         ///
372         lyx::pos_type inset_pos;
373         ///
374         bool inset_boundary;
375         ///
376         mutable int inset_x;
377         ///
378         mutable int inset_y;
379         ///
380         mutable int old_max_width;
381         ///
382         bool no_selection;
383         ///
384         UpdatableInset * the_locking_inset;
385         ///
386         mutable Paragraph * old_par;
387         /// The cache.
388         mutable Cache cache;
389         ///
390         mutable int last_drawn_width;
391         ///
392         mutable bool frame_is_visible;
393         ///
394         mutable BufferView * cached_bview;
395         ///
396         mutable boost::shared_ptr<LyXText> cached_text;
397         ///
398         struct save_state {
399                 Paragraph * lpar;
400                 Paragraph * selstartpar;
401                 Paragraph * selendpar;
402                 lyx::pos_type pos;
403                 lyx::pos_type selstartpos;
404                 lyx::pos_type selendpos;
405                 bool boundary;
406                 bool selstartboundary;
407                 bool selendboundary;
408                 bool selection;
409                 bool mark_set;
410                 bool refresh;
411         };
412         ///
413         mutable save_state sstate;
414
415         ///
416         // this is needed globally so we know that we're using it actually and
417         // so the LyXText-Cache is not erased until used!
418         mutable LyXText * lt;
419         ///
420         // to remember old painted frame dimensions to clear it on the right spot!
421         ///
422         mutable int frame_x;
423         mutable int frame_y;
424         mutable int frame_w;
425         mutable int frame_h;
426         ///
427         bool in_update; /* as update is not reentrant! */
428         mutable BufferView * do_resize;
429         mutable bool do_reinit;
430         mutable bool in_insetAllowed;
431         ///
432         // these are used to check for mouse movement in Motion selection code
433         ///
434         int mouse_x;
435         int mouse_y;
436 };
437 #endif