]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathGrid.h
Revert "XHTML: remove DOCTYPE, as the document is then understood as HTML4/XHTML1...
[lyx.git] / src / mathed / InsetMathGrid.h
1 // -*- C++ -*-
2 /**
3  * \file InsetMathGrid.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 "InsetMathNest.h"
16
17 #include "support/Length.h"
18
19 #include <map>
20
21 namespace lyx {
22
23 class BufferParams;
24 class BufferView;
25
26 /** Gridded math inset base class.
27  *  This is the base to all grid-like editable math objects
28  */
29 class InsetMathGrid : public InsetMathNest {
30 public:
31
32         enum Multicolumn {
33                 /// A normal cell
34                 CELL_NORMAL = 0,
35                 /// A multicolumn cell. The number of columns is <tt>1 + number
36                 /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
37                 CELL_BEGIN_OF_MULTICOLUMN = 1,
38                 /// This is a dummy cell (part of a multicolumn cell)
39                 CELL_PART_OF_MULTICOLUMN = 2
40         };
41
42         /// additional per-cell information
43         class CellInfo {
44         public:
45                 ///
46                 CellInfo();
47                 /// multicolumn flag
48                 Multicolumn multi;
49                 /// special multi columns alignment
50                 docstring align;
51         };
52
53         /// additional per-row information
54         class RowInfo {
55         public:
56                 ///
57                 RowInfo();
58                 ///
59                 int skipPixels(MetricsInfo const & mi) const;
60                 /// cached descent
61                 mutable int descent;
62                 /// cached ascent
63                 mutable int ascent;
64                 /// cached offset for each bufferview
65                 mutable std::map<BufferView const *, int> offset;
66                 /// how many hlines above this row?
67                 unsigned int lines;
68                 /// parameter to the line break
69                 Length crskip;
70                 /// extra distance between lines
71                 int skip;
72                 /// Is a page break allowed after this row?
73                 bool allow_newpage;
74         };
75
76         // additional per-row information
77         class ColInfo {
78         public:
79                 ///
80                 ColInfo();
81                 /// currently possible: 'l', 'c', 'r'
82                 char align;
83                 /// cached width
84                 mutable int width;
85                 /// cached offset
86                 mutable int offset;
87                 /// how many lines to the left of this column?
88                 unsigned int lines;
89                 /// additional amount to the right to be skipped when drawing
90                 int skip;
91                 /// Special alignment.
92                 /// This does also contain align_ and lines_ if it is nonempty.
93                 /// It needs to be in sync with align_ and lines_ because some
94                 /// code only uses align_ and lines_.
95                 docstring special;
96         };
97
98 public:
99         /// sets nrows and ncols to 1, vertical alignment to 'c'
100         explicit InsetMathGrid(Buffer * buf);
101         /// Note: columns first!
102         InsetMathGrid(Buffer * buf, col_type m, row_type n);
103         ///
104         InsetMathGrid(Buffer * buf, col_type m, row_type n, char valign,
105                 docstring const & halign);
106         ///
107         marker_type marker(BufferView const *) const { return MARKER2; };
108         ///
109         void metrics(MetricsInfo & mi, Dimension &) const;
110         ///
111         void draw(PainterInfo & pi, int x, int y) const;
112         ///
113         void metricsT(TextMetricsInfo const & mi, Dimension & dim) const;
114         ///
115         void drawT(TextPainter & pi, int x, int y) const;
116         ///
117         void updateBuffer(ParIterator const &, UpdateType, bool const deleted = false);
118         /// extract number of columns from alignment string
119         static col_type guessColumns(docstring const & halign);
120         /// accepts some LaTeX column codes: p,m,!,@,M,<,>
121         void setHorizontalAlignments(docstring const & align);
122         ///
123         void setHorizontalAlignment(char c, col_type col);
124         ///
125         char horizontalAlignment(col_type col) const;
126         ///
127         docstring horizontalAlignments() const;
128         /// 't', 'b', or 'm'
129         void setVerticalAlignment(char c);
130         ///
131         char verticalAlignment() const;
132         ///
133         void vcrskip(Length const &, row_type row);
134         ///
135         Length vcrskip(row_type row) const;
136         ///
137         void resize(short int type, col_type cols);
138         ///
139         const RowInfo & rowinfo(row_type row) const;
140         /// returns topmost row if passed (-1)
141         RowInfo & rowinfo(row_type row);
142         ///
143         const CellInfo & cellinfo(idx_type idx) const { return cellinfo_[idx]; }
144         ///
145         CellInfo & cellinfo(idx_type idx) { return cellinfo_[idx]; }
146         /// identifies GridInset
147         InsetMathGrid * asGridInset() { return this; }
148         /// identifies GridInset
149         InsetMathGrid const * asGridInset() const { return this; }
150         //
151         bool isTable() const { return true; }
152         ///
153         col_type ncols() const;
154         ///
155         row_type nrows() const;
156         ///
157         col_type col(idx_type idx) const;
158         ///
159         row_type row(idx_type idx) const;
160         /// number of columns of cell \p idx
161         col_type ncellcols(idx_type idx) const;
162
163         ///
164         bool idxUpDown(Cursor &, bool up) const;
165         ///
166         bool idxBackward(Cursor &) const;
167         ///
168         bool idxForward(Cursor &) const;
169         //
170         idx_type firstIdx() const;
171         //
172         idx_type lastIdx() const;
173         ///
174         bool idxDelete(idx_type & idx);
175         /// pulls cell after pressing erase
176         void idxGlue(idx_type idx);
177
178         /// add a row, one row down
179         virtual void addRow(row_type r);
180         /// delete a row
181         virtual void delRow(row_type r);
182         /// copy a row
183         virtual void copyRow(row_type r);
184         /// swap two rows
185         virtual void swapRow(row_type r);
186         /// add a column, here
187         virtual void addCol(col_type c);
188         /// delete a column
189         virtual void delCol(col_type c);
190         /// copy a column
191         virtual void copyCol(col_type c);
192         /// swap two columns
193         virtual void swapCol(col_type c);
194         ///
195         idx_type index(row_type r, col_type c) const;
196         ///
197         bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
198         ///
199         virtual int defaultColSpace(col_type) { return 0; }
200         ///
201         virtual char defaultColAlign(col_type) { return 'c'; }
202         ///
203         void setDefaults();
204         ///
205         virtual bool interpretString(Cursor & cur, docstring const & str);
206
207         ///
208         virtual int colsep() const;
209         ///
210         virtual int rowsep() const;
211         ///
212         virtual int hlinesep() const;
213         ///
214         virtual int vlinesep() const;
215         ///
216         virtual int border() const;
217         ///
218         virtual bool handlesMulticolumn() const { return false; }
219
220         ///
221         void write(WriteStream & os) const;
222         ///
223         void write(WriteStream & os,
224                    row_type beg_row, col_type beg_col,
225                    row_type end_row, col_type end_col) const;
226         ///
227         void normalize(NormalStream &) const;
228         ///
229         //void maple(MapleStream &) const;
230         ///
231         void mathmlize(MathStream &) const;
232         ///
233         void htmlize(HtmlStream &) const;
234         ///
235         void htmlize(HtmlStream &, std::string attrib) const;
236         ///
237         void validate(LaTeXFeatures & features) const;
238         ///
239         //void octave(OctaveStream &) const;
240
241 protected:
242         ///
243         void doDispatch(Cursor & cur, FuncRequest & cmd);
244         ///
245         bool getStatus(Cursor & cur, FuncRequest const & cmd,
246                 FuncStatus & flag) const;
247         /// returns x offset of cell compared to inset
248         int cellXOffset(BufferView const &, idx_type idx) const;
249         /// returns y offset of cell compared to inset
250         int cellYOffset(BufferView const &, idx_type idx) const;
251         /// Width of cell, taking combined columns into account
252         int cellWidth(idx_type idx) const;
253         ///
254         virtual int leftMargin() const { return 0; }
255         ///
256         virtual int rightMargin() const { return 0; }
257
258         /// returns proper 'end of line' code for LaTeX
259         virtual docstring eolString(row_type row, bool fragile, bool latex,
260                         bool last_eoln) const;
261         /// returns proper 'end of column' code for LaTeX
262         virtual docstring eocString(col_type col, col_type lastcol) const;
263         /// splits cells and shifts right part to the next cell
264         void splitCell(Cursor & cur);
265         /// Column alignment for display of cell \p idx.
266         /// Must not be written to file!
267         virtual char displayColAlign(idx_type idx) const;
268         /// Column spacing for display of column \p col.
269         /// Must not be written to file!
270         virtual int displayColSpace(col_type col) const;
271
272         // The following two functions are used in InsetMathHull and
273         // InsetMathSplit.
274         /// The value of a fixed col align for a certain hull type (can
275         /// depend on the "indent math" setting).
276         char colAlign(HullType type, col_type col) const;
277         /// The value of a fixed col spacing for a certain hull type
278         static int colSpace(HullType type, col_type col);
279
280         /// positions of vertical and horizontal lines
281         int vLineHOffset(col_type col, unsigned int line) const;
282         int hLineVOffset(BufferView const &, row_type row, unsigned int line) const;
283
284         ///
285         InsetCode lyxCode() const { return MATH_GRID_CODE; }
286
287 private:
288         /// row info.
289         /// rowinfo_[nrows()] is a dummy row used only for hlines.
290         std::vector<RowInfo> rowinfo_;
291         /// column info.
292         /// colinfo_[ncols()] is a dummy column used only for vlines.
293         std::vector<ColInfo> colinfo_;
294         /// cell info
295         std::vector<CellInfo> cellinfo_;
296         ///
297         char v_align_; // FIXME: add approp. type
298         ///
299         Inset * clone() const;
300 };
301
302
303 } // namespace lyx
304
305 #endif