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