]> git.lyx.org Git - features.git/blob - src/insets/insettext.h
e3d85331c5a1e3b9d5b70553e2f3bd64ef028e29
[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 "textcursor.h"
17 #include "LString.h"
18 #include "LColor.h"
19 #include "ParagraphList.h"
20 #include "RowList.h"
21 #include "dimension.h"
22 #include "frontends/mouse_state.h"
23
24 #include "support/types.h"
25
26 #include <boost/shared_ptr.hpp>
27 #include <map>
28
29 class Painter;
30 class BufferView;
31 class Buffer;
32 class BufferParams;
33 class LyXCursor;
34 class LyXText;
35 class Row;
36 class Paragraph;
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                 DRAW_FRAME = 2,
55                 ///
56                 SELECTION = 4,
57                 ///
58                 CURSOR_PAR = 8,
59                 ///
60                 FULL = 16,
61                 ///
62                 INIT = 32
63         };
64         ///
65         enum DrawFrame {
66                 ///
67                 NEVER = 0,
68                 ///
69                 LOCKED,
70                 ///
71                 ALWAYS
72         };
73         ///
74         InsetText(BufferParams const &);
75         ///
76         explicit InsetText(InsetText const &);
77         ///
78         InsetBase * clone() const;
79         ///
80         InsetText & operator=(InsetText const & it);
81         /// empty inset to empty par, or just mark as erased
82         void clear(bool just_mark_erased);
83         ///
84         void read(Buffer const *, LyXLex &);
85         ///
86         void write(Buffer const *, std::ostream &) const;
87         ///
88         void metrics(MetricsInfo &, Dimension &) const;
89         ///
90         int textWidth(BufferView *, bool fordraw = false) const;
91         ///
92         void draw(PainterInfo & pi, int x, int y) const;
93         ///
94         void update(BufferView *, bool = false);
95         ///
96         void setUpdateStatus(BufferView *, int what) const;
97         ///
98         string const editMessage() const;
99         ///
100         bool isTextInset() const { return true; }
101         ///
102         void insetUnlock(BufferView *);
103         ///
104         bool lockInsetInInset(BufferView *, UpdatableInset *);
105         ///
106         bool unlockInsetInInset(BufferView *,
107                                 UpdatableInset *, bool lr = false);
108         ///
109         bool updateInsetInInset(BufferView *, Inset *);
110         ///
111         RESULT localDispatch(FuncRequest const &);
112         ///
113         int latex(Buffer const *, std::ostream &,
114                   LatexRunParams const &) const;
115         ///
116         int ascii(Buffer const *, std::ostream &, int linelen) const;
117         ///
118         int linuxdoc(Buffer const *, std::ostream &) const { return 0; }
119         ///
120         int docbook(Buffer const *, std::ostream &, bool mixcont) const ;
121         ///
122         void validate(LaTeXFeatures & features) const;
123         ///
124         Inset::Code lyxCode() const { return Inset::TEXT_CODE; }
125         /// FIXME, document
126         void getCursorPos(BufferView *, int & x, int & y) const;
127         /// Get the absolute document x,y of the cursor
128         virtual void getCursor(BufferView &, int &, int &) const;
129         ///
130         int insetInInsetY() const;
131         ///
132         void fitInsetCursor(BufferView *) const;
133         ///
134         bool insertInset(BufferView *, Inset *);
135         ///
136         bool insetAllowed(Inset::Code) const;
137         ///
138         UpdatableInset * getLockingInset() const;
139         ///
140         UpdatableInset * getFirstLockingInsetOfType(Inset::Code);
141         ///
142         void setFont(BufferView *, LyXFont const &,
143                      bool toggleall = false,
144                      bool selectall = false);
145         ///
146         int getMaxWidth(BufferView *, UpdatableInset const *) const;
147         ///
148         void init(InsetText const * ins);
149         ///
150         void writeParagraphData(Buffer const *, std::ostream &) const;
151         ///
152         void setParagraphData(ParagraphList const &);
153         ///
154         void setText(string const &, LyXFont const &);
155         ///
156         void setAutoBreakRows(bool);
157         ///
158         bool getAutoBreakRows() const { return autoBreakRows; }
159         ///
160         void setDrawFrame(BufferView *, DrawFrame);
161         ///
162         void setFrameColor(BufferView *, LColor::color);
163         ///
164         LyXText * getLyXText(BufferView const *,
165                              bool const recursive = false) const;
166         ///
167         void deleteLyXText(BufferView *, bool recursive = true) const;
168         ///
169         void resizeLyXText(BufferView *, bool force = false) const;
170         ///
171         bool showInsetDialog(BufferView *) const;
172         ///
173         void getLabelList(std::vector<string> &) const;
174         ///
175         bool nodraw() const;
176         ///
177         int scroll(bool recursive = true) const;
178         ///
179         void scroll(BufferView * bv, float sx) const {
180                 UpdatableInset::scroll(bv, sx);
181         }
182         ///
183         void scroll(BufferView * bv, int offset) const {
184                 UpdatableInset::scroll(bv, offset);
185         }
186         ///
187         void selectAll(BufferView * bv);
188         ///
189         void clearSelection(BufferView * bv);
190         ///
191         Inset * getInsetFromID(int id) const;
192         ///
193         ParagraphList * getParagraphs(int) const;
194         ///
195         LyXCursor const & cursor(BufferView *) const;
196         ///
197         bool allowSpellcheck() const { return true; }
198         ///
199         WordLangTuple const
200         selectNextWordToSpellcheck(BufferView *, float & value) const;
201         ///
202         void selectSelectedWord(BufferView *);
203         ///
204         void toggleSelection(BufferView *, bool kill_selection);
205
206         /// mark as erased for change tracking
207         void markErased() { clear(true); };
208         /**
209          * Mark as new. Used when pasting in tabular, and adding rows
210          * or columns. Note that pasting will ensure that tracking already
211          * happens, and this just resets the changes for the copied text,
212          * whereas for row/col add, we need to start tracking changes
213          * for the (empty) paragraph contained.
214          */
215         void markNew(bool track_changes = false);
216         /// find next change
217         bool nextChange(BufferView *, lyx::pos_type & length);
218
219         ///
220         bool searchForward(BufferView *, string const &,
221                            bool = true, bool = false);
222         ///
223         bool searchBackward(BufferView *, string const &,
224                             bool = true, bool = false);
225         ///
226         bool checkInsertChar(LyXFont &);
227         ///
228         void getDrawFont(LyXFont &) const;
229         /// append text onto the existing text
230         void appendParagraphs(Buffer * bp, ParagraphList &);
231
232         ///
233         void addPreview(grfx::PreviewLoader &) const;
234
235         //
236         // Public structures and variables
237         ///
238         mutable int need_update;
239
240         bool haveParagraphs() const {
241                 return true;
242         }
243         ///
244         ParagraphList paragraphs;
245 protected:
246         ///
247         void updateLocal(BufferView *, int what, bool mark_dirty);
248         /// set parameters for an initial lock of this inset
249         void lockInset(BufferView *);
250         /// lock an inset inside this one
251         void lockInset(BufferView *, UpdatableInset *);
252         ///
253         mutable int drawTextXOffset;
254         ///
255         mutable int drawTextYOffset;
256         ///
257         bool autoBreakRows;
258         ///
259         DrawFrame drawFrame_;
260         ///
261         LColor::color frame_color;
262
263 private:
264         ///
265         void lfunMousePress(FuncRequest const &);
266         ///
267         bool lfunMouseRelease(FuncRequest const &);
268         ///
269         void lfunMouseMotion(FuncRequest const &);
270
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         RESULT moveRight(BufferView *,
286                                          bool activate_inset = true,
287                                          bool selecting = false);
288         ///
289         RESULT moveLeft(BufferView *,
290                                         bool activate_inset = true,
291                                         bool selecting = false);
292         ///
293         RESULT moveRightIntern(BufferView *, bool front,
294                                                bool activate_inset = true,
295                                                bool selecting = false);
296         ///
297         RESULT moveLeftIntern(BufferView *, bool front,
298                                               bool activate_inset = true,
299                                               bool selecting = false);
300
301         ///
302         RESULT moveUp(BufferView *);
303         ///
304         RESULT moveDown(BufferView *);
305         ///
306         void setCharFont(Buffer const *, int pos, LyXFont const & font);
307         ///
308         bool checkAndActivateInset(BufferView * bv, bool front);
309         ///
310         bool checkAndActivateInset(BufferView * bv, int x = 0, int y = 0,
311                                    mouse_button::state button = mouse_button::none);
312         ///
313         void removeNewlines();
314         ///
315         int cx(BufferView *) const;
316         ///
317         int cix(BufferView *) const;
318         ///
319         int cy(BufferView *) const;
320         ///
321         int ciy(BufferView *) const;
322         ///
323         lyx::pos_type cpos(BufferView *) const;
324         ///
325         ParagraphList::iterator cpar(BufferView *) const;
326         ///
327         bool cboundary(BufferView *) const;
328         ///
329         RowList::iterator crow(BufferView *) const;
330         ///
331         void drawFrame(Painter &, int x) const;
332         ///
333         void clearInset(BufferView *, int start_x, int baseline) const;
334         ///
335         void saveLyXTextState(LyXText *) const;
336         ///
337         void restoreLyXTextState(LyXText *) const;
338         ///
339         void reinitLyXText() const;
340         ///
341         void collapseParagraphs(BufferView *);
342
343         /* Private structures and variables */
344         ///
345         mutable bool locked;
346         ///
347         mutable Dimension dim_;
348         ///
349         mutable int top_y;
350         ///
351         ParagraphList::iterator inset_par;
352         ///
353         lyx::pos_type inset_pos;
354         ///
355         bool inset_boundary;
356         ///
357         mutable int inset_x;
358         ///
359         mutable int inset_y;
360         ///
361         mutable int old_max_width;
362         ///
363         bool no_selection;
364         ///
365         UpdatableInset * the_locking_inset;
366         ///
367         mutable ParagraphList::iterator old_par;
368         /// The cache.
369         mutable Cache cache;
370         ///
371         mutable int last_drawn_width;
372         ///
373         mutable BufferView * cached_bview;
374         ///
375         mutable boost::shared_ptr<LyXText> cached_text;
376
377         /// some funny 'temporarily saved state'
378         mutable TextCursor sstate;
379
380         ///
381         // this is needed globally so we know that we're using it actually and
382         // so the LyXText-Cache is not erased until used!
383         mutable LyXText * lt;
384         ///
385         // to remember old painted frame dimensions to clear it on the right spot!
386         ///
387         mutable int frame_x;
388         mutable int frame_y;
389         mutable int frame_w;
390         mutable int frame_h;
391         ///
392         bool in_update; /* as update is not reentrant! */
393         mutable BufferView * do_resize;
394         mutable bool do_reinit;
395         mutable bool in_insetAllowed;
396         ///
397         // these are used to check for mouse movement in Motion selection code
398         ///
399         int mouse_x;
400         int mouse_y;
401 };
402 #endif