]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.h
several smallish changes/bugfixes/left overs from Porto
[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 #include <config.h>
26
27 #ifdef __GNUG__
28 #pragma interface
29 #endif
30
31 #include "LString.h"
32 #include "frontends/mouse_state.h"
33 #include "math_xdata.h"
34
35 /**
36
37 Abstract base class for all math objects.  A math insets is for use of the
38 math editor only, it isn't a general LyX inset. It's used to represent all
39 the math objects.
40
41 Math insets do not know there parents, a cursor position or things
42 like that. The are dumb object that are contained in other math insets
43 (mathNestInsets, in fact) thus forming a tree. The root of this tree is
44 always a mathHullInset, which provides an interface to the Outer World by
45 inclusion in the "real LyX insets" FormulaInset and FormulaMacroInset.
46
47 */
48
49
50 class MathArrayInset;
51 class MathAMSArrayInset;
52 class MathBraceInset;
53 class MathBoxInset;
54 class MathCharInset;
55 class MathDelimInset;
56 class MathFboxInset;
57 class MathFontInset;
58 class MathGridInset;
59 class MathFracInset;
60 class MathHullInset;
61 class MathMatrixInset;
62 class MathNestInset;
63 class MathParInset;
64 class MathScriptInset;
65 class MathStringInset;
66 class MathSpaceInset;
67 class MathSymbolInset;
68 class MathUnknownInset;
69 class MathXYMatrixInset;
70
71 class NormalStream;
72 class OctaveStream;
73 class MapleStream;
74 class MathMLStream;
75 class WriteStream;
76 class InfoStream;
77 class MathArray;
78
79 class LaTeXFeatures;
80 class BufferView;
81 class UpdatableInset;
82 class MathMacroTemplate;
83 class MathPosFinder;
84
85
86 class MathInset {
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         /// the virtual base destructor
104         virtual ~MathInset() {}
105
106         /// reproduce itself
107         virtual MathInset * clone() const = 0;
108         /// substitutes macro arguments if necessary
109         virtual void substitute(MathMacro const & macro);
110         /// compute the size of the object, sets ascend_, descend_ and width_
111         // updates the (xo,yo)-caches of all contained cells
112         virtual void metrics(MathMetricsInfo & st) const;
113         /// draw the object
114         virtual void draw(MathPainterInfo &, int x, int y) const;
115         /// the ascent of the inset above the baseline
116         /// compute the size of the object for text based drawing
117         virtual void metricsT(TextMetricsInfo const & st) const;
118         /// draw the object as text
119         virtual void drawT(TextPainter &, int x, int y) const;
120         /// the ascent of the inset above the baseline
121         virtual int ascent() const { return 1; }
122         /// the descent of the inset below the baseline
123         virtual int descent() const { return 1; }
124         /// total width
125         virtual int width() const { return 2; }
126         /// total height (== ascent + descent)
127         virtual int height() const;
128
129         /// Where should we go when we press the up or down cursor key?
130         virtual bool idxUpDown(idx_type & idx, bool up) const;
131         /// The left key
132         virtual bool idxLeft(idx_type & idx, pos_type & pos) const;
133         /// The right key
134         virtual bool idxRight(idx_type & idx, pos_type & pos) const;
135
136         /// Move one physical cell up
137         virtual bool idxNext(idx_type & idx, pos_type & pos) const;
138         /// Move one physical cell down
139         virtual bool idxPrev(idx_type & idx, pos_type & pos) const;
140
141         /// Target pos when we enter the inset from the left by pressing "Right"
142         virtual bool idxFirst(idx_type & idx, pos_type & pos) const;
143         /// Target pos when we enter the inset from the right by pressing "Left"
144         virtual bool idxLast(idx_type & idx, pos_type & pos) const;
145
146         /// Where should we go if we press home?
147         virtual bool idxHome(idx_type & idx, pos_type & pos) const;
148         /// Where should we go if we press end?
149         virtual bool idxEnd(idx_type & idx, pos_type & pos) const;
150
151         /// Delete a cell and move cursor
152         virtual bool idxDelete(idx_type &) { return false; }
153         /// pulls cell after pressing erase
154         virtual void idxGlue(idx_type) {}
155         // returns list of cell indices that are "between" from and to for
156         // selection purposes
157         virtual std::vector<idx_type> idxBetween(idx_type from, idx_type to) const;
158
159         /// the number of nested cells this inset owns
160         virtual idx_type nargs() const;
161
162         /// return cell given its number
163         virtual MathArray & cell(idx_type);
164         /// return cell given its number
165         virtual MathArray const & cell(idx_type) const;
166         /// return cell plus drawing cache given its number
167         virtual MathXArray & xcell(idx_type);
168         /// return cell plus drawing cache given its number
169         virtual MathXArray const & xcell(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
188         /// identifies certain types of insets
189         virtual MathAMSArrayInset      * asAMSArrayInset()      { return 0; }
190         virtual MathArrayInset         * asArrayInset()         { return 0; }
191         virtual MathBraceInset         * asBraceInset()         { return 0; }
192         virtual MathBoxInset           * asBoxInset()           { return 0; }
193         virtual MathBoxInset const     * asBoxInset() const     { return 0; }
194         virtual MathCharInset const    * asCharInset() const    { return 0; }
195         virtual MathDelimInset         * asDelimInset()         { return 0; }
196         virtual MathDelimInset const   * asDelimInset() const   { return 0; }
197         virtual MathFboxInset          * asFboxInset()          { return 0; }
198         virtual MathFontInset const    * asFontInset() const    { return 0; }
199         virtual MathFracInset          * asFracInset()          { return 0; }
200         virtual MathGridInset          * asGridInset()          { return 0; }
201         virtual MathHullInset          * asHullInset()          { return 0; }
202         virtual MathHullInset const    * asHullInset() const    { return 0; }
203         virtual MathMacroTemplate      * asMacroTemplate()      { return 0; }
204         virtual MathMatrixInset const  * asMatrixInset() const  { return 0; }
205         virtual MathNestInset          * asNestInset()          { return 0; }
206         virtual MathParInset           * asParInset()           { return 0; }
207         virtual MathScriptInset        * asScriptInset()        { return 0; }
208         virtual MathScriptInset const  * asScriptInset() const  { return 0; }
209         virtual MathSpaceInset         * asSpaceInset()         { return 0; }
210         virtual MathStringInset        * asStringInset()        { return 0; }
211         virtual MathSymbolInset const  * asSymbolInset() const  { return 0; }
212         virtual MathUnknownInset       * asUnknownInset()       { return 0; }
213         virtual MathUnknownInset const * asUnknownInset() const { return 0; }
214         virtual MathXYMatrixInset const* asXYMatrixInset() const{ return 0; }
215
216         /// identifies things that can get scripts
217         virtual bool isScriptable() const { return false; }
218         /// thing that can be moved into
219         virtual bool isActive() const { return nargs() > 0; }
220         /// is the a relational operator (used for splitting equations)
221         virtual bool isRelOp() const { return false; }
222
223         /// return the content as char if the inset is able to do so
224         virtual char getChar() const { return 0; }
225         /// identifies things that can get \limits or \nolimits
226         virtual bool takesLimits() const { return false; }
227         /// identifies complicated things that need braces if used as arg
228         virtual bool needsBraces() const { return true; }
229
230         ///
231         virtual void edit(BufferView *, int, int, mouse_button::state) {}
232
233         /// request "external features"
234         virtual void validate(LaTeXFeatures & features) const;
235         /// char char code if possible
236         virtual void handleFont(string const &) {}
237         /// is this inset equal to a given other inset?
238         virtual bool match(MathInset *) const { return false; }
239         /// replace things by other things
240         virtual void replace(ReplaceData &) {}
241         /// do we contain a given subsequence?
242         virtual bool contains(MathArray const &) { return false; }
243         /// access to the lock (only nest array have one)
244         virtual bool lock() const { return false; }
245         /// access to the lock (only nest array have one)
246         virtual void lock(bool) {}
247
248         /// write LaTeX and Lyx code
249         virtual void write(WriteStream & os) const;
250         /// write normalized content
251         virtual void normalize(NormalStream &) const;
252         /// write content as something readable by Maple
253         virtual void maplize(MapleStream &) const;
254         /// write content as something resembling MathML
255         virtual void mathmlize(MathMLStream &) const;
256         /// write content as something readable by Octave
257         virtual void octavize(OctaveStream &) const;
258         /// describe content
259         virtual void infoize(std::ostream &) const {}
260         /// plain ascii output
261         virtual int ascii(std::ostream & os, int) const;
262         /// linuxdoc output
263         virtual int linuxdoc(std::ostream & os) const;
264         /// docbook output
265         virtual int docbook(std::ostream & os, bool) const;
266
267         /// dump content to stderr for debugging
268         virtual void dump() const;
269         /// local dispatcher
270         virtual int dispatch(string const & cmd, idx_type idx, pos_type pos);
271 };
272
273 std::ostream & operator<<(std::ostream &, MathInset const &);
274 std::ostream & operator<<(std::ostream &, MathAtom const &);
275
276 string asString(MathArray const & ar);
277 MathArray asArray(string const & str);
278
279 #endif