]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.h
Fix math cursor positioning bug
[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 metricsT(TextMetricsInfo const & mi, Dimension & dim) const;
102         ///
103         void drawT(TextPainter & pi, int x, int y) const;
104         ///
105         void halign(std::string const & align);
106         ///
107         void halign(char c, col_type col);
108         ///
109         char halign(col_type col) const;
110         ///
111         std::string halign() const;
112         ///
113         void valign(char c);
114         ///
115         char valign() const;
116         ///
117         void vcrskip(LyXLength const &, row_type row);
118         ///
119         LyXLength vcrskip(row_type row) const;
120         ///
121         void resize(short int type, col_type cols);
122         ///
123         const RowInfo & rowinfo(row_type row) const;
124         /// returns topmost row if passed (-1)
125         RowInfo & rowinfo(row_type row);
126         ///
127         const CellInfo & cellinfo(idx_type idx) const { return cellinfo_[idx]; }
128         ///
129         CellInfo & cellinfo(idx_type idx) { return cellinfo_[idx]; }
130         /// identifies GridInset
131         MathGridInset * asGridInset() { return this; }
132         /// identifies GridInset
133         MathGridInset const * asGridInset() const { return this; }
134         ///
135         col_type ncols() const;
136         ///
137         row_type nrows() const;
138         ///
139         col_type col(idx_type idx) const;
140         ///
141         row_type row(idx_type idx) const;
142
143         ///
144         bool idxUpDown(LCursor &, bool up) const;
145         ///
146         bool idxLeft(LCursor &) const;
147         ///
148         bool idxRight(LCursor &) const;
149         ///
150         bool idxFirst(LCursor &) const;
151         ///
152         bool idxLast(LCursor &) const;
153         ///
154         bool idxDelete(idx_type & idx);
155         /// pulls cell after pressing erase
156         void idxGlue(idx_type idx);
157
158         /// add a row
159         virtual void addRow(row_type r);
160         /// delete a row
161         virtual void delRow(row_type r);
162         /// copy a row
163         virtual void copyRow(row_type r);
164         /// swap two rows
165         virtual void swapRow(row_type r);
166         /// add a column
167         virtual void addCol(col_type c);
168         /// delete a column
169         virtual void delCol(col_type c);
170         /// copy a column
171         virtual void copyCol(col_type c);
172         /// swap two columns
173         virtual void swapCol(col_type c);
174         ///
175         virtual void appendRow();
176         ///
177         idx_type index(row_type r, col_type c) const;
178         ///
179         bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
180         ///
181         virtual int defaultColSpace(col_type) { return 0; }
182         ///
183         virtual char defaultColAlign(col_type) { return 'c'; }
184         ///
185         void setDefaults();
186
187         ///
188         virtual int colsep() const;
189         ///
190         virtual int rowsep() const;
191         ///
192         virtual int hlinesep() const;
193         ///
194         virtual int vlinesep() const;
195         ///
196         virtual int border() const;
197
198         ///
199         void write(WriteStream & os) const;
200         ///
201         void normalize(NormalStream &) const;
202         ///
203         //void maple(MapleStream &) const;
204         ///
205         void mathmlize(MathMLStream &) const;
206         ///
207         //void octave(OctaveStream &) const;
208
209 protected:
210         virtual void doDispatch(LCursor & cur, FuncRequest & cmd);
211         ///
212         bool getStatus(LCursor & cur, FuncRequest const & cmd,
213                 FuncStatus & flag) const;
214         /// returns x offset of cell compared to inset
215         int cellXOffset(idx_type idx) const;
216         /// returns y offset of cell compared to inset
217         int cellYOffset(idx_type idx) const;
218         /// returns proper 'end of line' code for LaTeX
219         virtual std::string eolString(row_type row, bool fragile = false) const;
220         /// returns proper 'end of column' code for LaTeX
221         virtual std::string eocString(col_type col, col_type lastcol) const;
222         /// extract number of columns from alignment string
223         col_type guessColumns(std::string const & halign) const;
224         /// splits cells and shifts right part to the next cell
225         void splitCell(LCursor & cur);
226
227 public:
228         /// row info.
229         /// rowinfo_[nrows()] is a dummy row used only for hlines.
230         std::vector<RowInfo> rowinfo_;
231         /// column info.
232         /// colinfo_[ncols()] is a dummy column used only for vlines.
233         std::vector<ColInfo> colinfo_;
234         /// cell info
235         std::vector<CellInfo> cellinfo_;
236         ///
237         char v_align_; // add approp. type
238 private:
239         virtual std::auto_ptr<InsetBase> doClone() const;
240 };
241
242 #endif