]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.h
Insert mouse_x/y positon to have a REAL motion check. Fixed the spurious
[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 "paragraph.h"
26 #include "lyxcursor.h"
27
28 #include <boost/smart_ptr.hpp>
29
30 class Painter;
31 class BufferView;
32 class Buffer;
33 class LyXCursor;
34 class LyXText;
35 class LyXScreen;
36 class Row;
37
38 /**
39  A text inset is like a TeX box to write full text
40  (including styles and other insets) in a given space. 
41  @author: Jürgen Vigna
42  */
43 class InsetText : public UpdatableInset {
44 public:
45         ///
46         /// numbers need because of test if codeA < codeB
47         ///
48         enum UpdateCodes {
49                 ///
50                 NONE = 0,
51                 ///
52                 CURSOR = 1,
53                 ///
54                 CLEAR_FRAME = 2,
55                 ///
56                 DRAW_FRAME = 4,
57                 ///
58                 SELECTION = 8,
59                 ///
60                 CURSOR_PAR = 16,
61                 ///
62                 FULL = 32,
63                 ///
64                 INIT = 64
65         };
66         ///
67         enum DrawFrame {
68                 ///
69                 NEVER = 0,
70                 ///
71                 LOCKED,
72                 ///
73                 ALWAYS
74         };
75         ///
76         InsetText();
77         ///
78         explicit
79         InsetText(InsetText const &, bool same_id = false);
80         ///
81         ~InsetText();
82         ///
83         Inset * clone(Buffer const &, bool same_id = false) const;
84         ///
85         InsetText & operator=(InsetText const & it);
86         ///
87         void clear();
88         ///
89         void read(Buffer const *, LyXLex &);
90         ///
91         void write(Buffer const *, std::ostream &) const;
92         ///
93         int ascent(BufferView *, LyXFont const &) const;
94         ///
95         int descent(BufferView *, LyXFont const &) const;
96         ///
97         int width(BufferView *, LyXFont const & f) const;
98         ///
99         int textWidth(BufferView *, bool fordraw = false) const;
100         ///
101         void draw(BufferView *, LyXFont const &, int , float &, bool) const;
102         ///
103         void update(BufferView *, LyXFont const &, bool = false);
104         ///
105         void setUpdateStatus(BufferView *, int what) const;
106         ///
107         string const editMessage() const;
108         ///
109         void edit(BufferView *, int, int, unsigned int);
110         ///
111         void edit(BufferView *, bool front = true);
112         ///
113         bool isTextInset() const { return true; }
114         ///
115         bool doClearArea() const;
116         ///
117         void insetUnlock(BufferView *);
118         ///
119         bool lockInsetInInset(BufferView *, UpdatableInset *);
120         ///
121         bool unlockInsetInInset(BufferView *,
122                                 UpdatableInset *, bool lr = false);
123         ///
124         bool updateInsetInInset(BufferView *, Inset *);
125         ///
126         bool insetButtonRelease(BufferView *, int, int, int);
127         ///
128         void insetButtonPress(BufferView *, int, int, int);
129         ///
130         void insetMotionNotify(BufferView *, int, int, int);
131         ///
132         void insetKeyPress(XKeyEvent *);
133         ///
134         UpdatableInset::RESULT localDispatch(BufferView *,
135                                              kb_action, string const &);
136         ///
137         int latex(Buffer const *, std::ostream &,
138                   bool fragile, bool free_spc) const;
139         ///
140         int ascii(Buffer const *, std::ostream &, int linelen) const;
141         ///
142         int linuxdoc(Buffer const *, std::ostream &) const { return 0; }
143         ///
144         int docbook(Buffer const *, std::ostream &) const ;
145         ///
146         void validate(LaTeXFeatures & features) const;
147         ///
148         Inset::Code lyxCode() const { return Inset::TEXT_CODE; }
149         ///
150         void getCursorPos(BufferView *, int & x, int & y) const;
151         ///
152         unsigned int insetInInsetY();
153         ///
154         void toggleInsetCursor(BufferView *);
155         ///
156         void showInsetCursor(BufferView *, bool show = true);
157         ///
158         void hideInsetCursor(BufferView *);
159         ///
160         void fitInsetCursor(BufferView *) const;
161         ///
162         bool insertInset(BufferView *, Inset *);
163         ///
164         bool insetAllowed(Inset::Code) const;
165         ///
166         UpdatableInset * getLockingInset() const;
167         ///
168         UpdatableInset * getFirstLockingInsetOfType(Inset::Code);
169         ///
170         void setFont(BufferView *, LyXFont const &,
171                      bool toggleall = false,
172                      bool selectall = false);
173         ///
174         int getMaxWidth(BufferView *, UpdatableInset const *) const;
175         ///
176         void init(InsetText const * ins = 0, bool same_id = false);
177         ///
178         void writeParagraphData(Buffer const *, std::ostream &) const;
179         ///
180         void setParagraphData(Paragraph *, bool same_id = false);
181         ///
182         void setText(string const &);
183         ///
184         void setAutoBreakRows(bool);
185         ///
186         bool getAutoBreakRows() const { return autoBreakRows; }
187         ///
188         void setDrawFrame(BufferView *, DrawFrame);
189         ///
190         void setFrameColor(BufferView *, LColor::color);
191         ///
192         LyXText * getLyXText(BufferView const *,
193                              bool const recursive = false) const;
194         ///
195         void deleteLyXText(BufferView *, bool recursive = true) const;
196         ///
197         void resizeLyXText(BufferView *, bool force = false) const;
198         ///
199         bool showInsetDialog(BufferView *) const;
200         ///
201         std::vector<string> const getLabelList() const;
202         ///
203         bool nodraw() const;
204         ///
205         int scroll(bool recursive = true) const;
206         ///
207         void scroll(BufferView * bv, float sx) const {
208                 UpdatableInset::scroll(bv, sx);
209         }
210         ///
211         void scroll(BufferView * bv, int offset) const {
212                 UpdatableInset::scroll(bv, offset);
213         }
214         ///
215         void selectAll(BufferView * bv);
216         ///
217         void clearSelection(BufferView * bv);
218         ///
219         Paragraph * getParFromID(int id) const;
220         ///
221         Inset * getInsetFromID(int id) const;
222         ///
223         Paragraph * firstParagraph() const;
224         ///
225         Paragraph * getFirstParagraph(int) const;
226         ///
227         LyXCursor const & cursor(BufferView *) const;
228         ///
229         Paragraph * paragraph() const;
230         ///
231         void paragraph(Paragraph *);
232         ///
233         bool allowSpellcheck() { return true; }
234         ///
235         string const selectNextWordToSpellcheck(BufferView *,
236                                                 float & value) const;
237         void selectSelectedWord(BufferView *);
238         ///
239         void toggleSelection(BufferView *, bool kill_selection);
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         //
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         ///
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         struct InnerCache {
273                 ///
274                 InnerCache(boost::shared_ptr<LyXText>);
275                 ///
276                 boost::shared_ptr<LyXText> text;
277                 ///
278                 bool remove;
279         };
280         ///
281         typedef std::map<BufferView *, InnerCache> Cache;
282         ///
283         typedef Cache::value_type value_type;
284         ///
285         int beginningOfMainBody(Buffer const *, Paragraph * par) const;
286         ///
287         UpdatableInset::RESULT moveRight(BufferView *,
288                                          bool activate_inset = true,
289                                          bool selecting = false);
290         ///
291         UpdatableInset::RESULT moveLeft(BufferView *,
292                                         bool activate_inset = true,
293                                         bool selecting = false);
294         ///
295         UpdatableInset::RESULT moveRightIntern(BufferView *, bool behind,
296                                                bool activate_inset = true,
297                                                bool selecting = false);
298         ///
299         UpdatableInset::RESULT moveLeftIntern(BufferView *, bool behind, 
300                                               bool activate_inset = true,
301                                               bool selecting = false);
302
303         ///
304         UpdatableInset::RESULT moveUp(BufferView *);
305         ///
306         UpdatableInset::RESULT moveDown(BufferView *);
307         ///
308         void setCharFont(Buffer const *, int pos, LyXFont const & font);
309         ///
310         bool checkAndActivateInset(BufferView * bv, bool behind);
311         ///
312         bool checkAndActivateInset(BufferView * bv, int x = 0, int y = 0,
313                                    int button = 0);
314         ///
315         void removeNewlines();
316         ///
317         int cx(BufferView *) const;
318         ///
319         int cy(BufferView *) const;
320         ///
321         lyx::pos_type cpos(BufferView *) const;
322         ///
323         Paragraph * cpar(BufferView *) const;
324         ///
325         bool cboundary(BufferView *) const;
326         ///
327         Row * crow(BufferView *) const;
328         ///
329         void drawFrame(Painter &, bool cleared) const;
330         ///
331         void clearFrame(Painter &, bool cleared) const;
332         ///
333         void clearInset(BufferView *, int baseline, bool & cleared) const;
334         ///
335         void saveLyXTextState(LyXText *) const;
336         ///
337         void restoreLyXTextState(BufferView *, LyXText *) const;
338         ///
339         void reinitLyXText() const;
340         ///
341         void collapseParagraphs(BufferParams const & bparams) const;
342         
343         /* Private structures and variables */
344         ///
345         Paragraph * par;
346         ///
347         mutable bool locked;
348         ///
349         mutable int insetAscent;
350         ///
351         mutable int insetDescent;
352         ///
353         mutable int insetWidth;
354         ///
355         mutable int top_y;
356         ///
357         Paragraph * inset_par;
358         ///
359         lyx::pos_type inset_pos;
360         ///
361         bool inset_boundary;
362         ///
363         mutable int inset_x;
364         ///
365         mutable int inset_y;
366         ///
367         mutable int old_max_width;
368         ///
369         bool no_selection;
370         ///
371         mutable float xpos;
372         ///
373         UpdatableInset * the_locking_inset;
374         ///
375         mutable Paragraph * old_par;
376         /// The cache.
377         mutable Cache cache;
378         ///
379         mutable int last_drawn_width;
380         ///
381         mutable bool frame_is_visible;
382         ///
383         mutable BufferView * cached_bview;
384         ///
385         mutable boost::shared_ptr<LyXText> cached_text;
386         ///
387         struct save_state {
388                 Paragraph * lpar;
389                 Paragraph * selstartpar;
390                 Paragraph * selendpar;
391                 lyx::pos_type pos;
392                 lyx::pos_type selstartpos;
393                 lyx::pos_type selendpos;
394                 bool boundary;
395                 bool selstartboundary;
396                 bool selendboundary;
397                 bool selection;
398                 bool mark_set;
399                 bool refresh;
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