]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.h
Jean-Marc's fix for wrong descent
[lyx.git] / src / mathed / math_inset.h
1 // -*- C++ -*-
2 /**
3  *  File:        math_inset.h
4  *  Purpose:     Declaration of insets for mathed
5  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
6  *  Created:     January 1996
7  *  Description: Math paragraph and objects for a WYSIWYG math editor.
8  *
9  *  Dependencies: Xlib, XForms
10  *
11  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
12  *
13  *   Version: 0.8beta, Math & Lyx project.
14  *
15  *   You are free to use and modify this code under the terms of
16  *   the GNU General Public Licence version 2 or later.
17  */
18
19 //  Note: These math insets are internal to Math and are not derived
20 //        from lyx inset.
21
22 #ifndef MATH_INSET_H
23 #define MATH_INSET_H
24
25
26 #include <config.h>
27
28 #include "LString.h"
29 #include "frontends/mouse_state.h"
30 #include "insets/insetbase.h"
31 #include "math_data.h"
32
33 /**
34
35 Abstract base class for all math objects.  A math insets is for use of the
36 math editor only, it isn't a general LyX inset. It's used to represent all
37 the math objects.
38
39 Math insets do not know there parents, a cursor position or things
40 like that. The are dumb object that are contained in other math insets
41 (mathNestInsets, in fact) thus forming a tree. The root of this tree is
42 always a mathHullInset, which provides an interface to the Outer World by
43 inclusion in the "real LyX insets" FormulaInset and FormulaMacroInset.
44
45 */
46
47
48 class MathArrayInset;
49 class MathAMSArrayInset;
50 class MathCharInset;
51 class MathDelimInset;
52 class MathGridInset;
53 class MathFracInset;
54 class MathFontInset;
55 class MathHullInset;
56 class MathMatrixInset;
57 class MathNestInset;
58 class MathParboxInset;
59 class MathScriptInset;
60 class MathStringInset;
61 class MathSpaceInset;
62 class MathSymbolInset;
63 class MathUnknownInset;
64
65 class RefInset;
66
67 class NormalStream;
68 class OctaveStream;
69 class MapleStream;
70 class MaximaStream;
71 class MathematicaStream;
72 class MathMLStream;
73 class WriteStream;
74 class InfoStream;
75 class MathArray;
76
77 class LaTeXFeatures;
78 class BufferView;
79 class UpdatableInset;
80 class MathMacroTemplate;
81 class MathPosFinder;
82 class Dimension;
83 class FuncRequest;
84
85
86 class MathInset : public InsetBase {
87 public:
88         /// short of anything else reasonable
89         typedef MathArray::size_type        size_type;
90         /// type for cursor positions differences within a cell
91         typedef MathArray::difference_type  difference_type;
92         /// type for cursor positions within a cell
93         typedef MathArray::size_type        pos_type;
94         /// type for cell indices
95         typedef size_type                   idx_type;
96         /// type for row numbers
97         typedef size_type                   row_type;
98         /// type for column numbers
99         typedef size_type                   col_type;
100
101         /// our members behave nicely...
102         MathInset() {}
103
104         /// reproduce itself
105         virtual MathInset * clone() const = 0;
106         /// substitutes macro arguments if necessary
107         virtual void substitute(MathMacro const & macro);
108         /// compute the size of the object, sets ascend_, descend_ and width_
109         // updates the (xo,yo)-caches of all contained cells
110         virtual void metrics(MathMetricsInfo & mi) const;
111         /// draw the object
112         virtual void draw(MathPainterInfo & pi, int x, int y) const;
113         /// draw selection between two positions
114         virtual void drawSelection(MathPainterInfo & pi,
115                 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const;
116         /// the ascent of the inset above the baseline
117         /// compute the size of the object for text based drawing
118         virtual void metricsT(TextMetricsInfo const & st) const;
119         /// draw the object as text
120         virtual void drawT(TextPainter &, int x, int y) const;
121         /// the ascent of the inset above the baseline
122         virtual int ascent() const { return 1; }
123         /// the descent of the inset below the baseline
124         virtual int descent() const { return 1; }
125         /// total width
126         virtual int width() const { return 2; }
127         /// all in one batch
128         virtual Dimension dimensions() const;
129         /// total height (== ascent + descent)
130         virtual int height() const;
131
132         /// Where should we go when we press the up or down cursor key?
133         virtual bool idxUpDown(idx_type & idx, pos_type & pos, bool up,
134                 int targetx) const;
135         /// The left key
136         virtual bool idxLeft(idx_type & idx, pos_type & pos) const;
137         /// The right key
138         virtual bool idxRight(idx_type & idx, pos_type & pos) const;
139
140         /// Move one physical cell up
141         virtual bool idxNext(idx_type & idx, pos_type & pos) const;
142         /// Move one physical cell down
143         virtual bool idxPrev(idx_type & idx, pos_type & pos) const;
144
145         /// Target pos when we enter the inset from the left by pressing "Right"
146         virtual bool idxFirst(idx_type & idx, pos_type & pos) const;
147         /// Target pos when we enter the inset from the right by pressing "Left"
148         virtual bool idxLast(idx_type & idx, pos_type & pos) const;
149
150         /// Where should we go if we press home?
151         virtual bool idxHome(idx_type & idx, pos_type & pos) const;
152         /// Where should we go if we press end?
153         virtual bool idxEnd(idx_type & idx, pos_type & pos) const;
154
155         /// Delete a cell and move cursor
156         virtual bool idxDelete(idx_type &) { return false; }
157         /// pulls cell after pressing erase
158         virtual void idxGlue(idx_type) {}
159         // returns list of cell indices that are "between" from and to for
160         // selection purposes
161         virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
162
163         /// the number of nested cells this inset owns
164         virtual idx_type nargs() const;
165
166         /// return cell given its number
167         virtual MathArray & cell(idx_type);
168         /// return cell given its number
169         virtual MathArray const & cell(idx_type) const;
170
171         /// the number of columns of this inset if it is grid-like
172         virtual col_type ncols() const { return 1; }
173         /// the number of rows of this inset if it is grid-like
174         virtual row_type nrows() const { return 1; }
175         /// to which column belongs a cell with a given index?
176         virtual col_type col(idx_type) const { return 0; }
177         /// to which row belongs a cell with a given index?
178         virtual row_type row(idx_type) const { return 0; }
179         /// cell idex corresponding to row and column;
180         virtual idx_type index(row_type row, col_type col) const;
181         /// any additional x-offset when drawing a cell?
182         virtual int cellXOffset(idx_type) const { return 0; }
183         /// any additional y-offset when drawing a cell?
184         virtual int cellYOffset(idx_type) const { return 0; }
185         /// can we enter this cell?
186         virtual bool validCell(idx_type) const { return true; }
187         /// get coordinates
188         virtual void getPos(idx_type idx, pos_type pos, int & x, int & y) const;
189
190         /// identifies certain types of insets
191         virtual MathAMSArrayInset       * asAMSArrayInset()       { return 0; }
192         virtual MathAMSArrayInset const * asAMSArrayInset() const { return 0; }
193         virtual MathArrayInset          * asArrayInset()          { return 0; }
194         virtual MathArrayInset const    * asArrayInset() const    { return 0; }
195         virtual MathCharInset const     * asCharInset() const     { return 0; }
196         virtual MathDelimInset          * asDelimInset()          { return 0; }
197         virtual MathDelimInset const    * asDelimInset() const    { return 0; }
198         virtual MathFracInset           * asFracInset()           { return 0; }
199         virtual MathFracInset const     * asFracInset() const     { return 0; }
200         virtual MathFontInset           * asFontInset()           { return 0; }
201         virtual MathFontInset const     * asFontInset() const     { return 0; }
202         virtual MathGridInset           * asGridInset()           { return 0; }
203         virtual MathGridInset const     * asGridInset() const     { return 0; }
204         virtual MathHullInset           * asHullInset()           { return 0; }
205         virtual MathHullInset const     * asHullInset() const     { return 0; }
206         virtual MathMacroTemplate       * asMacroTemplate()       { return 0; }
207         virtual MathMacroTemplate const * asMacroTemplate() const { return 0; }
208         virtual MathMatrixInset const   * asMatrixInset() const   { return 0; }
209         virtual MathNestInset           * asNestInset()           { return 0; }
210         virtual MathNestInset const     * asNestInset() const     { return 0; }
211         virtual MathParboxInset         * asParboxInset()         { return 0; }
212         virtual MathScriptInset         * asScriptInset()         { return 0; }
213         virtual MathScriptInset const   * asScriptInset() const   { return 0; }
214         virtual MathSpaceInset          * asSpaceInset()          { return 0; }
215         virtual MathSpaceInset const    * asSpaceInset() const    { return 0; }
216         virtual MathStringInset         * asStringInset()         { return 0; }
217         virtual MathStringInset const   * asStringInset() const   { return 0; }
218         virtual MathSymbolInset const   * asSymbolInset() const   { return 0; }
219         virtual MathUnknownInset        * asUnknownInset()        { return 0; }
220         virtual MathUnknownInset const  * asUnknownInset() const  { return 0; }
221         virtual RefInset                * asRefInset()            { return 0; }
222
223         /// identifies things that can get scripts
224         virtual bool isScriptable() const { return false; }
225         /// thing that can be moved into
226         virtual bool isActive() const { return nargs() > 0; }
227         /// is the a relational operator (used for splitting equations)
228         virtual bool isRelOp() const { return false; }
229         /// -1: text mode, 1: math mode, 0 undecided
230         enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
231
232         virtual mode_type currentMode() const { return UNDECIDED_MODE; }
233         /// will this get written as a single block in {..}
234         virtual bool extraBraces() const { return false; }
235
236         /// return the content as char if the inset is able to do so
237         virtual char getChar() const { return 0; }
238         /// identifies things that can get \limits or \nolimits
239         virtual bool takesLimits() const { return false; }
240
241         ///
242         virtual void edit(BufferView *, int, int, mouse_button::state) {}
243
244         /// request "external features"
245         virtual void validate(LaTeXFeatures &) const {}
246         /// char char code if possible
247         virtual void handleFont(string const &) {}
248         /// is this inset equal to a given other inset?
249         virtual bool match(MathAtom const &) const { return false; }
250         /// replace things by other things
251         virtual void replace(ReplaceData &) {}
252         /// do we contain a given subsequence?
253         virtual bool contains(MathArray const &) const { return false; }
254         /// access to the lock (only nest array have one)
255         virtual bool lock() const { return false; }
256         /// access to the lock (only nest array have one)
257         virtual void lock(bool) {}
258         /// get notification when the cursor leaves this inset
259         virtual void notifyCursorLeaves(idx_type) {}
260
261         /// write LaTeX and Lyx code
262         virtual void write(WriteStream & os) const;
263         /// write normalized content
264         virtual void normalize(NormalStream &) const;
265         /// write content as something readable by Maple
266         virtual void maple(MapleStream &) const;
267         /// write content as something readable by Maxima
268         virtual void maxima(MaximaStream &) const;
269         /// write content as something readable by Mathematica
270         virtual void mathematica(MathematicaStream &) const;
271         /// write content as something resembling MathML
272         virtual void mathmlize(MathMLStream &) const;
273         /// write content as something readable by Octave
274         virtual void octave(OctaveStream &) const;
275         /// describe content if cursor inside
276         virtual void infoize(std::ostream &) const {}
277         /// describe content if cursor behind
278         virtual void infoize2(std::ostream &) const {}
279         /// plain ascii output
280         virtual int ascii(std::ostream & os, int) const;
281         /// linuxdoc output
282         virtual int linuxdoc(std::ostream & os) const;
283         /// docbook output
284         virtual int docbook(std::ostream & os, bool) const;
285
286         /// dump content to stderr for debugging
287         virtual void dump() const;
288
289         /// LyXInset stuff
290         /// write labels into a list
291         virtual void getLabelList(std::vector<string> &) const {}
292         /// LyXInset stuff
293         virtual bool numberedType() const { return false; }
294         /// hull type
295         virtual string const & getType() const;
296         /// change type
297         virtual void mutate(string const &) {}
298         /// how is the inset called in the .lyx file?
299         virtual string fileInsetLabel() const { return "Formula"; }
300         /// usually the latex name
301         virtual string name() const;
302 };
303
304 std::ostream & operator<<(std::ostream &, MathAtom const &);
305
306 // converts single cell to string
307 string asString(MathArray const & ar);
308 // converts string to single cell
309 MathArray asArray(string const & str);
310
311 // initialize math
312 void initMath();
313
314 /// here to ssave a few includes in the insets
315 class Dialogs;
316 Dialogs & getDialogs();
317
318 #endif