]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.h
fix #832
[lyx.git] / src / insets / insettabular.h
1 // -*- C++ -*-
2 /**
3  * \file insettabular.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 // This is the rewrite of the tabular (table) support.
12
13 // It will probably be a lot of work.
14
15 // One first goal could be to make the inset read the old table format
16 // and just output it again... no viewing at all.
17
18 // When making the internal structure of tabular support I really think
19 // that STL containers should be used. This will separate the container from
20 // the rest of the code, which is a good thing.
21
22 // Ideally the tabular support should do as the mathed and use
23 // LaTeX in the .lyx file too.
24
25 // Things to think of when desingning the new tabular support:
26 // - color support (colortbl, color)
27 // - decimal alignment (dcloumn)
28 // - custom lines (hhline)
29 // - rotation
30 // - multicolumn
31 // - multirow
32 // - column styles
33
34 // This is what I have written about tabular support in the LyX3-Tasks file:
35 //
36 //  o rewrite of table code. Should probably be written as some
37 //          kind of an inset. At least get the code out of the kernel.
38 //                - colortbl  -multirow
39 //                - hhline    -multicolumn
40 //                - dcolumn
41 // o enhance longtable support
42
43 // Lgb
44
45 #ifndef INSETTABULAR_H
46 #define INSETTABULAR_H
47
48 #include "inset.h"
49 #include "tabular.h"
50 #include "LString.h"
51 #include "lyxcursor.h"
52 #include "FuncStatus.h"
53
54 #include <boost/scoped_ptr.hpp>
55 #include <boost/weak_ptr.hpp>
56
57 class LyXLex;
58 class Painter;
59 class BufferView;
60 class Buffer;
61 class BufferParams;
62 class Paragraph;
63
64 class InsetTabular : public UpdatableInset {
65 public:
66         ///
67         enum UpdateCodes {
68                 NONE = 0,
69                 CURSOR = 1,
70                 CELL = 2,
71                 SELECTION = 3,
72                 FULL = 4,
73                 INIT = 5
74         };
75         ///
76         InsetTabular(Buffer const &, int rows = 1, int columns = 1);
77         ///
78         InsetTabular(InsetTabular const &, Buffer const &, bool same_id = false);
79         ///
80         ~InsetTabular();
81         ///
82         Inset * clone(Buffer const &, bool same_id = false) const;
83         ///
84         void read(Buffer const *, LyXLex &);
85         ///
86         void write(Buffer const *, std::ostream &) const;
87         ///
88         int ascent(BufferView *, LyXFont const &) const;
89         ///
90         int descent(BufferView *, LyXFont const &) const;
91         ///
92         int width(BufferView *, LyXFont const & f) const;
93         ///
94         void draw(BufferView *, const LyXFont &, int , float &) const;
95         ///
96         void update(BufferView *, bool = false);
97         ///
98         string const editMessage() const;
99         //
100         void insetUnlock(BufferView *);
101         ///
102         void updateLocal(BufferView *, UpdateCodes) const;
103         ///
104         bool lockInsetInInset(BufferView *, UpdatableInset *);
105         ///
106         bool unlockInsetInInset(BufferView *, UpdatableInset *,
107                                 bool lr = false);
108         ///
109         bool updateInsetInInset(BufferView *, Inset *);
110         ///
111         int insetInInsetY() const;
112         ///
113         UpdatableInset * getLockingInset() const;
114         ///
115         UpdatableInset * getFirstLockingInsetOfType(Inset::Code);
116         ///
117         bool insertInset(BufferView *, Inset *);
118         ///
119         bool insetAllowed(Inset::Code code) const;
120         ///
121         bool isTextInset() const { return true; }
122         /** returns true if, when outputing LaTeX, font changes should
123             be closed before generating this inset. This is needed for
124             insets that may contain several paragraphs */
125         bool noFontChange() const { return true; }
126         ///
127         bool display() const { return tabular->IsLongTabular(); }
128         ///
129         RESULT localDispatch(FuncRequest const &);
130         ///
131         int latex(Buffer const *, std::ostream &, bool, bool) const;
132         ///
133         int ascii(Buffer const *, std::ostream &, int linelen) const;
134         ///
135         int linuxdoc(Buffer const *, std::ostream &) const;
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::TABULAR_CODE; }
142         /// FIXME, document
143         void getCursorPos(BufferView *, int & x, int & y) const;
144         /// Get the absolute document x,y of the cursor
145         virtual void getCursor(BufferView &, int &, int &) const;
146         ///
147         bool tabularFeatures(BufferView * bv, string const & what);
148         ///
149         void tabularFeatures(BufferView * bv, LyXTabular::Feature feature,
150                              string const & val = string());
151         ///
152         int getActCell() const { return actcell; }
153         ///
154         void setFont(BufferView *, LyXFont const &, bool toggleall = false,
155                      bool selectall = false);
156         ///
157         int getMaxWidth(BufferView *, UpdatableInset const *) const;
158         ///
159         Buffer * bufferOwner() const { return const_cast<Buffer *>(buffer); }
160         ///
161         LyXText * getLyXText(BufferView const *,
162                              bool const recursive = false) const;
163         ///
164         void deleteLyXText(BufferView *, bool recursive = true) const;
165         ///
166         void resizeLyXText(BufferView *, bool force = false) const;
167         ///
168         void openLayoutDialog(BufferView *) const;
169         ///
170         bool showInsetDialog(BufferView *) const;
171         ///
172         FuncStatus getStatus(string const & argument) const;
173         ///
174         std::vector<string> const getLabelList() const;
175         ///
176         void nodraw(bool b) const {
177                 UpdatableInset::nodraw(b);
178         }
179         bool nodraw() const;
180         ///
181         int scroll(bool recursive=true) const;
182         ///
183         void scroll(BufferView *bv, float sx) const {
184                 UpdatableInset::scroll(bv, sx);
185         }
186         ///
187         void scroll(BufferView *bv, int offset) const {
188                 UpdatableInset::scroll(bv, offset);
189         }
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         void markErased();
207
208         /// find next change
209         bool nextChange(BufferView *, lyx::pos_type & length);
210         ///
211         bool searchForward(BufferView *, string const &,
212                            bool = true, bool = false);
213         bool searchBackward(BufferView *, string const &,
214                             bool = true, bool = false);
215
216         // this should return true if we have a "normal" cell, otherwise true.
217         // "normal" means without width set!
218         bool forceDefaultParagraphs(Inset const * in) const;
219
220         ///
221         void addPreview(grfx::PreviewLoader &) const;
222
223         //
224         // Public structures and variables
225         ///
226         boost::scoped_ptr<LyXTabular> tabular;
227
228         /// are some cells selected ?
229         bool hasSelection() const {
230                 return has_selection;
231         }
232
233         ///
234         virtual BufferView * view() const;
235 private:
236         ///
237         void lfunMousePress(FuncRequest const &);
238         ///
239         // the bool return is used to see if we opened a dialog so that we can
240         // check this from an outer inset and open the dialog of the outer inset
241         // if that one has one!
242         ///
243         bool lfunMouseRelease(FuncRequest const &);
244         ///
245         void lfunMouseMotion(FuncRequest const &);
246         ///
247         bool calculate_dimensions_of_cells(BufferView *, bool = false) const;
248         ///
249         void drawCellLines(Painter &, int x, int baseline,
250                            int row, int cell) const;
251         ///
252         void drawCellSelection(Painter &, int x, int baseline,
253                                int row, int column, int cell) const;
254         ///
255         void fitInsetCursor(BufferView *) const;
256         ///
257         void setPos(BufferView *, int x, int y) const;
258         ///
259         RESULT moveRight(BufferView *, bool lock = true);
260         ///
261         RESULT moveLeft(BufferView *, bool lock = true);
262         ///
263         RESULT moveUp(BufferView *, bool lock = true);
264         ///
265         RESULT moveDown(BufferView *, bool lock = true);
266         ///
267         bool moveNextCell(BufferView *, bool lock = false);
268         ///
269         bool movePrevCell(BufferView *, bool lock = false);
270         ///
271         int getCellXPos(int cell) const;
272         ///
273         void resetPos(BufferView *) const;
274         ///
275         void removeTabularRow();
276         ///
277         void clearSelection() const {
278                 sel_cell_start = sel_cell_end = 0;
279                 has_selection = false;
280         }
281         void setSelection(int start, int end) const {
282                 sel_cell_start = start;
283                 sel_cell_end = end;
284                 has_selection = true;
285         }
286         ///
287         bool activateCellInset(BufferView *, int x = 0, int y = 0,
288                                mouse_button::state button = mouse_button::none,
289                                bool behind = false);
290         ///
291         bool activateCellInsetAbs(BufferView *, int x = 0, int y = 0,
292                                   mouse_button::state button = mouse_button::none);
293         ///
294         bool insetHit(BufferView * bv, int x, int y) const;
295         ///
296         int getMaxWidthOfCell(BufferView * bv, int cell) const;
297         ///
298         bool hasPasteBuffer() const;
299         ///
300         bool copySelection(BufferView *);
301         ///
302         bool pasteSelection(BufferView *);
303         ///
304         bool cutSelection(BufferParams const & bp);
305         ///
306         bool isRightToLeft(BufferView *);
307         ///
308         void getSelection(int & scol, int & ecol,
309                           int & srow, int & erow) const;
310         ///
311         WordLangTuple selectNextWordInt(BufferView *, float & value) const;
312         ///
313         bool insertAsciiString(BufferView *, string const & buf, bool usePaste);
314
315         //
316         // Private structures and variables
317         ///
318         InsetText * the_locking_inset;
319         ///
320         InsetText * old_locking_inset;
321         ///
322         Buffer const * buffer;
323         ///
324         mutable LyXCursor cursor_;
325         ///
326         mutable unsigned int inset_x;
327         ///
328         mutable unsigned int inset_y;
329         /// true if a set of cells are selected
330         mutable bool has_selection;
331         /// the starting cell selection nr
332         mutable int sel_cell_start;
333         /// the ending cell selection nr
334         mutable int sel_cell_end;
335         ///
336         mutable int actcell;
337         ///
338         mutable int oldcell;
339         ///
340         mutable int actcol;
341         ///
342         mutable int actrow;
343         ///
344         mutable int first_visible_cell;
345         ///
346         bool no_selection;
347         ///
348         mutable bool locked;
349         ///
350         mutable UpdateCodes need_update;
351         ///
352         bool in_update;
353         ///
354         mutable int in_reset_pos;
355 };
356
357
358 #include "mailinset.h"
359
360
361 class InsetTabularMailer : public MailInset {
362 public:
363         ///
364         InsetTabularMailer(InsetTabular & inset);
365         ///
366         virtual InsetBase & inset() const { return inset_; }
367         ///
368         virtual string const & name() const { return name_; }
369         ///
370         virtual string const inset2string() const;
371         /// Returns the active cell if successful, else -1.
372         static int string2params(string const &, InsetTabular &);
373         ///
374         static string const params2string(InsetTabular const &);
375 private:
376         ///
377         static string const name_;
378         ///
379         InsetTabular & inset_;
380 };
381
382 string const featureAsString(LyXTabular::Feature feature);
383
384 #endif