4 * Purpose: Math editor definitions
5 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
6 * Created: January 1996
7 * Description: Math paragraph and objects for a WYSIWYG math editor.
11 * Copyright: 1996, 1997 Alejandro Aguilar Sierra
13 * Version: 0.8beta, Mathed & Lyx project.
15 * You are free to use and modify this code under the terms of
16 * the GNU General Public Licence version 2 or later.
43 MATH_ALIGN_BOTTOM = 4,
53 /// Standard Math Sizes (Math mode styles)
66 /// Standard LaTeX Math Environments
67 enum MathedEnvironment {
83 /** The restrictions of a standard LaTeX math paragraph
84 allows to get a small number of text codes (<30) */
85 enum MathedTextCodes {
88 /// Open and Close group
98 /// Super and sub scripts
102 /// Editable Math Inset
104 /// Editable Text Inset
108 /// Internal code for constants
110 /// Internal code for variables
126 /// Math mode TeX characters ",;:{}"
128 /// Special characters "{}&#_%"
130 /// Internal code for operators
132 /// Internal code for symbols
141 /// This must be < 32
145 std::ostream & operator<<(std::ostream &, MathedTextCodes mtc);
148 #define LM_TC_NORMAL LM_TC_VAR
151 /// Types of lyx-math insets
152 enum MathedInsetTypes {
155 /// A simple paragraph
157 /// A simple numbered paragraph
159 /// A multiline paragraph
161 /// A multiline numbered paragraph
202 enum MathedBinaryTypes {
210 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
215 /** Abstract base class for all math objects.
216 A math insets is for use of the math editor only, it isn't a
217 general LyX inset. It's used to represent all the math objects.
218 The formulaInset (a LyX inset) encapsulates a math inset.
222 /// A math inset has a name (usually its LaTeX name), type and font-size
223 MathedInset(char const * nm, short ot, short st);
226 MathedInset(MathedInset *);
228 virtual ~MathedInset() {}
231 virtual void draw(Painter &, int x, int baseline) = 0;
233 /// Write LaTeX and Lyx code
234 virtual void Write(std::ostream &, bool fragile) = 0;
236 /// Reproduces itself
237 virtual MathedInset * Clone() = 0;
239 /// Compute the size of the object
240 virtual void Metrics() = 0;
242 virtual int Ascent() const { return ascent; }
244 virtual int Descent() const { return descent; }
246 virtual int Width() const { return width; }
248 virtual int Height() const { return ascent + descent; }
251 virtual bool GetLimits() const { return false; }
253 virtual void SetLimits(bool) {}
256 char const * GetName() const { return name; }
258 short GetType() const { return objtype; }
260 short GetStyle() const { return size; }
262 //Man: Avoid to use these functions if it's not strictly necessary
264 virtual void SetType(short t) { objtype = t; }
266 virtual void SetStyle(short st) { size = st; } // Metrics();
268 virtual void SetName(char const * n) { name = n; }
283 static int df_asc, df_des, df_width;
285 /// In a near future maybe we use a better fonts renderer than X
286 void drawStr(Painter &, short, int, int, int, byte const *, int);
288 friend class MathedCursor;
290 friend void mathed_init_fonts();
296 /// Paragraph permissions
299 /// If false can use a non-standard size
301 /// If true can insert newlines
303 /// If true can use tabs
305 /// If true can insert new columns
306 LMPF_ALLOW_NEW_COL = 8,
307 /// Smaller than current size (frac)
309 /// Script size (subscript, stackrel)
314 /** The math paragraph base class, base to all editable math objects */
315 class MathParInset: public MathedInset {
318 MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
319 short ot = LM_OT_MIN);
322 MathParInset(MathParInset *);
324 virtual ~MathParInset();
326 virtual MathedInset * Clone();
328 /// Draw the object on a drawable
329 virtual void draw(Painter &, int x, int baseline);
332 virtual void Write(std::ostream &, bool fragile);
335 virtual void Metrics();
337 virtual void UserSetSize(short);
339 /// Data is stored in a LyXArray
340 virtual void SetData(LyxArrayBase *);
342 virtual LyxArrayBase * GetData() { return array; }
344 /// Paragraph position
345 virtual void GetXY(int &, int &) const;
347 virtual void setXY(int x, int y) { xo = x; yo = y; }
349 virtual void SetFocus(int, int) {}
351 virtual bool Inside(int, int);
353 // Tab stuff used by Matrix.
355 virtual void SetAlign(char, char const *) {}
357 // virtual int GetTabPos() { return 0; }
359 // virtual int GetTab(int) { return 0; }
361 virtual int GetColumns() { return 1; }
363 virtual int GetRows() { return 1; }
365 virtual bool isMatrix() { return false; }
366 // /// These functions should report an error
367 // virtual char const* GetLabel() { return 0; }
368 // virtual char const* GetLabel(int) { return 0; }
370 // Vertical switching
372 virtual bool setArgumentIdx(int i) { return (i == 0); }
374 virtual bool setNextArgIdx() { return false; }
376 virtual int getArgumentIdx() { return 0; }
378 virtual int getMaxArgumentIdx() { return 0; }
380 // virtual void SetLabel(char const *) {}
382 virtual void SetStyle(short);
384 virtual MathedRowSt * getRowSt() const { return 0; }
386 virtual void setRowSt(MathedRowSt *) {}
388 virtual bool Permit(short f) { return bool(f & flag); }
391 /// Paragraph data is stored here
392 LyxArrayBase * array;
393 /// Cursor start position
400 virtual void setFlag(MathedParFlag f) { flag |= f; }
402 friend class InsetFormula;
404 friend class MathedXIter;
406 friend class MathedCursor;
408 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
409 LyxArrayBase * a = 0,
410 MathParInset ** p = 0);
414 /* The physical structure of a row and aditional information is stored here.
415 It allows to manage the extra info independently of the paragraph data.
416 Only used for multiline paragraphs.
422 w = new int[n + 1]; // this leaks
424 for (int i = 0 ; i < n + 1 ; ++i)
435 /// Should be const but...
436 MathedRowSt * getNext() const { return next; }
437 /// ...we couldn't use this.
438 void setNext(MathedRowSt * n) { next = n; }
440 char const * getLabel() const { return label; }
442 bool isNumbered() const { return numbered; }
444 int getBaseline() const { return y; }
446 int getTab(int i) { return w[i]; }
448 void setLabel(char * l) { label = l; }
450 void setNumbered(bool nf) { numbered = nf; }
452 void setTab(int i, int t) { w[i] = t; }
466 friend class MathMatrixInset;
468 friend class MathedXIter;
472 /** Multiline math paragraph base class.
473 This is the base to all multiline editable math objects
474 like array and eqnarray.
476 class MathMatrixInset: public MathParInset {
480 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
483 MathMatrixInset(MathMatrixInset *);
485 MathedInset * Clone();
487 virtual ~MathMatrixInset();
489 void draw(Painter &, int, int);
491 void Write(std::ostream &, bool fragile);
495 void SetData(LyxArrayBase *);
497 void SetAlign(char, char const *);
499 char * GetAlign(char * vv) {
506 int GetColumns() { return nc; }
508 int GetRows() { return nr; }
510 virtual bool isMatrix() { return true; }
512 /// Use this to manage the extra information independently of paragraph
513 MathedRowSt * getRowSt() const { return row; }
515 void setRowSt(MathedRowSt * r) { row = r; }
518 /// Number of columns & rows
523 char v_align; // add approp. signedness
526 /// Vertical structure
533 /************************* Prototypes **********************************/
535 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
538 void mathed_write(MathParInset *, std::ostream &, int *, bool fragile,
539 char const * label = 0);
542 void mathed_parser_file(std::istream &, int);
544 int mathed_parser_lineno();
546 int MathedLookupBOP(short);
548 /************************ Inline functions ********************************/
551 bool MathIsInset(short x)
553 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
558 bool MathIsFont(short x)
560 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
565 bool MathIsAlphaFont(short x)
567 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
572 bool MathIsActive(short x)
574 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
579 bool MathIsUp(short x)
581 return x == LM_TC_UP;
586 bool MathIsDown(short x)
588 return x == LM_TC_DOWN;
593 bool MathIsScript(short x)
595 return x == LM_TC_DOWN || x == LM_TC_UP;
600 bool MathIsBOPS(short x)
602 return MathedLookupBOP(x) > LMB_NONE;
608 bool MathIsBinary(short x)
610 return x == LM_TC_BOP || x == LM_TC_BOPS;
615 bool MathIsSymbol(short x) {
616 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
621 MathedInset::MathedInset(char const * nm, short ot, short st):
622 name(nm), objtype(ot), size(st)
624 width = ascent = descent = 0;
628 bool MathParInset::Inside(int x, int y)
630 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
635 void MathParInset::GetXY(int & x, int & y) const
642 void MathParInset::UserSetSize(short sz)
646 flag = flag & ~LMPF_FIXED_SIZE;
652 void MathParInset::SetStyle(short sz)
654 if (Permit(LMPF_FIXED_SIZE)) {
655 if (Permit(LMPF_SCRIPT))
656 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
657 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
660 MathedInset::SetStyle(sz);