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