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