]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.h
Andreas' patch to prevent crash on click on previewd inset
[lyx.git] / src / mathed / math_gridinset.h
1 // -*- C++ -*-
2 /**
3  * \file math_gridinset.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef MATH_GRID_H
13 #define MATH_GRID_H
14
15 #include "math_nestinset.h"
16 #include "lyxlength.h"
17
18
19 /** Gridded math inset base class.
20  *  This is the base to all grid-like editable math objects
21  */
22 class MathGridInset : public MathNestInset {
23 public:
24
25         /// additional per-cell information
26         class CellInfo {
27         public:
28                 ///
29                 CellInfo();
30                 /// a dummy cell before a multicolumn cell
31                 int dummy_;
32                 /// special multi colums alignment
33                 std::string align_;
34                 /// these should be a per-cell property, but ok to have it here
35                 /// for single-column grids like paragraphs
36                 mutable int glue_;
37                 ///
38                 mutable pos_type begin_;
39                 ///
40                 mutable pos_type end_;
41         };
42
43         /// additional per-row information
44         class RowInfo {
45         public:
46                 ///
47                 RowInfo();
48                 ///
49                 int skipPixels() const;
50                 /// cached descent
51                 mutable int descent_;
52                 /// cached ascent
53                 mutable int ascent_;
54                 /// cached offset
55                 mutable int offset_;
56                 /// how many hlines above this row?
57                 unsigned int lines_;
58                 /// parameter to the line break
59                 LyXLength crskip_;
60                 /// extra distance between lines
61                 int skip_;
62         };
63
64         // additional per-row information
65         class ColInfo {
66         public:
67                 ///
68                 ColInfo();
69                 /// currently possible: 'l', 'c', 'r'
70                 char align_;
71                 /// cache for drawing
72                 int h_offset;
73                 /// cached width
74                 mutable int width_;
75                 /// cached offset
76                 mutable int offset_;
77                 /// how many lines to the left of this column?
78                 unsigned int lines_;
79                 /// additional amount to be skipped when drawing
80                 int skip_;
81         };
82
83 public:
84         /// sets nrows and ncols to 1
85         MathGridInset();
86         /// constructor from columns description, creates one row
87         MathGridInset(char valign, std::string const & halign);
88         /// Note: columns first!
89         MathGridInset(col_type m, row_type n);
90         ///
91         MathGridInset(col_type m, row_type n, char valign, std::string const & halign);
92         /// Ensures that the dialog is closed.
93         ~MathGridInset();
94         ///
95         void metrics(MetricsInfo & mi) const;
96         ///
97         void metrics(MetricsInfo & mi, Dimension &) const;
98         ///
99         void draw(PainterInfo & pi, int x, int y) const;
100         ///
101         void drawWithMargin(PainterInfo & pi, int x, int y,
102                 int lmargin = 0, int rmargin = 0) const;
103         ///
104         void metricsT(TextMetricsInfo const & mi, Dimension & dim) const;
105         ///
106         void drawT(TextPainter & pi, int x, int y) const;
107         ///
108         void halign(std::string const & align);
109         ///
110         void halign(char c, col_type col);
111         ///
112         char halign(col_type col) const;
113         ///
114         std::string halign() const;
115         ///
116         void valign(char c);
117         ///
118         char valign() const;
119         ///
120         void vcrskip(LyXLength const &, row_type row);
121         ///
122         LyXLength vcrskip(row_type row) const;
123         ///
124         void resize(short int type, col_type cols);
125         ///
126         const RowInfo & rowinfo(row_type row) const;
127         /// returns topmost row if passed (-1)
128         RowInfo & rowinfo(row_type row);
129         ///
130         const CellInfo & cellinfo(idx_type idx) const { return cellinfo_[idx]; }
131         ///
132         CellInfo & cellinfo(idx_type idx) { return cellinfo_[idx]; }
133         /// identifies GridInset
134         MathGridInset * asGridInset() { return this; }
135         /// identifies GridInset
136         MathGridInset const * asGridInset() const { return this; }
137         ///
138         col_type ncols() const;
139         ///
140         row_type nrows() const;
141         ///
142         col_type col(idx_type idx) const;
143         ///
144         row_type row(idx_type idx) const;
145
146         ///
147         bool idxUpDown(LCursor &, bool up) const;
148         ///
149         bool idxLeft(LCursor &) const;
150         ///
151         bool idxRight(LCursor &) const;
152         ///
153         bool idxFirst(LCursor &) const;
154         ///
155         bool idxLast(LCursor &) const;
156         ///
157         bool idxDelete(idx_type & idx);
158         /// pulls cell after pressing erase
159         void idxGlue(idx_type idx);
160
161         /// add a row
162         virtual void addRow(row_type r);
163         /// delete a row
164         virtual void delRow(row_type r);
165         /// copy a row
166         virtual void copyRow(row_type r);
167         /// swap two rows
168         virtual void swapRow(row_type r);
169         /// add a column
170         virtual void addCol(col_type c);
171         /// delete a column
172         virtual void delCol(col_type c);
173         /// copy a column
174         virtual void copyCol(col_type c);
175         /// swap two columns
176         virtual void swapCol(col_type c);
177         ///
178         virtual void appendRow();
179         ///
180         idx_type index(row_type r, col_type c) const;
181         ///
182         bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
183         ///
184         virtual int defaultColSpace(col_type) { return 0; }
185         ///
186         virtual char defaultColAlign(col_type) { return 'c'; }
187         ///
188         void setDefaults();
189
190         ///
191         virtual int colsep() const;
192         ///
193         virtual int rowsep() const;
194         ///
195         virtual int hlinesep() const;
196         ///
197         virtual int vlinesep() const;
198         ///
199         virtual int border() const;
200
201         ///
202         void write(WriteStream & os) const;
203         ///
204         void normalize(NormalStream &) const;
205         ///
206         //void maple(MapleStream &) const;
207         ///
208         void mathmlize(MathMLStream &) const;
209         ///
210         //void octave(OctaveStream &) const;
211
212 protected:
213         virtual void doDispatch(LCursor & cur, FuncRequest & cmd);
214         ///
215         bool getStatus(LCursor & cur, FuncRequest const & cmd,
216                 FuncStatus & flag) const;
217         /// returns x offset of cell compared to inset
218         int cellXOffset(idx_type idx) const;
219         /// returns y offset of cell compared to inset
220         int cellYOffset(idx_type idx) const;
221         /// returns proper 'end of line' code for LaTeX
222         virtual std::string eolString(row_type row, bool fragile = false) const;
223         /// returns proper 'end of column' code for LaTeX
224         virtual std::string eocString(col_type col, col_type lastcol) const;
225         /// extract number of columns from alignment string
226         col_type guessColumns(std::string const & halign) const;
227         /// splits cells and shifts right part to the next cell
228         void splitCell(LCursor & cur);
229
230 public:
231         /// row info.
232         /// rowinfo_[nrows()] is a dummy row used only for hlines.
233         std::vector<RowInfo> rowinfo_;
234         /// column info.
235         /// colinfo_[ncols()] is a dummy column used only for vlines.
236         std::vector<ColInfo> colinfo_;
237         /// cell info
238         std::vector<CellInfo> cellinfo_;
239         ///
240         char v_align_; // add approp. type
241 private:
242         virtual std::auto_ptr<InsetBase> doClone() const;
243 };
244
245 #endif