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