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
146 std::ostream & operator<<(std::ostream &, MathedTextCodes mtc);
149 #define LM_TC_NORMAL LM_TC_VAR
152 /// Types of lyx-math insets
153 enum MathedInsetTypes {
156 /// A simple paragraph
158 /// A simple numbered paragraph
160 /// A multiline paragraph
162 /// A multiline numbered paragraph
203 enum MathedBinaryTypes {
211 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
216 /** Abstract base class for all math objects.
217 A math insets is for use of the math editor only, it isn't a
218 general LyX inset. It's used to represent all the math objects.
219 The formulaInset (a LyX inset) encapsulates a math inset.
223 /// A math inset has a name (usually its LaTeX name), type and font-size
224 MathedInset(char const * nm, short ot, short st);
227 MathedInset(MathedInset *);
229 virtual ~MathedInset() {}
232 virtual void draw(Painter &, int x, int baseline) = 0;
234 /// Write LaTeX and Lyx code
235 virtual void Write(std::ostream &, bool fragile) = 0;
237 /// Reproduces itself
238 virtual MathedInset * Clone() = 0;
240 /// Compute the size of the object
241 virtual void Metrics() = 0;
243 virtual int Ascent() const { return ascent; }
245 virtual int Descent() const { return descent; }
247 virtual int Width() const { return width; }
249 virtual int Height() const { return ascent + descent; }
252 virtual bool GetLimits() const { return false; }
254 virtual void SetLimits(bool) {}
257 char const * GetName() const { return name; }
259 short GetType() const { return objtype; }
261 short GetStyle() const { return size; }
263 //Man: Avoid to use these functions if it's not strictly necessary
265 virtual void SetType(short t) { objtype = t; }
267 virtual void SetStyle(short st) { size = st; } // Metrics();
269 virtual void SetName(char const * n) { name = n; }
289 /// In a near future maybe we use a better fonts renderer than X
290 void drawStr(Painter &, short, int, int, int, byte const *, int);
292 friend class MathedCursor;
294 friend void mathed_init_fonts();
300 /// Paragraph permissions
303 /// If false can use a non-standard size
305 /// If true can insert newlines
307 /// If true can use tabs
309 /// If true can insert new columns
310 LMPF_ALLOW_NEW_COL = 8,
311 /// Smaller than current size (frac)
313 /// Script size (subscript, stackrel)
318 /** The math paragraph base class, base to all editable math objects */
319 class MathParInset: public MathedInset {
322 MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
323 short ot = LM_OT_MIN);
326 MathParInset(MathParInset *);
328 virtual ~MathParInset();
330 virtual MathedInset * Clone();
332 /// Draw the object on a drawable
333 virtual void draw(Painter &, int x, int baseline);
336 virtual void Write(std::ostream &, bool fragile);
339 virtual void Metrics();
341 virtual void UserSetSize(short);
343 /// Data is stored in a LyXArray
344 virtual void SetData(LyxArrayBase *);
346 virtual LyxArrayBase * GetData() { return array; }
348 /// Paragraph position
349 virtual void GetXY(int &, int &) const;
351 virtual void setXY(int x, int y) { xo = x; yo = y; }
353 virtual void SetFocus(int, int) {}
355 virtual bool Inside(int, int);
357 // Tab stuff used by Matrix.
359 virtual void SetAlign(char, char const *) {}
361 // virtual int GetTabPos() { return 0; }
363 // virtual int GetTab(int) { return 0; }
365 virtual int GetColumns() { return 1; }
367 virtual int GetRows() { return 1; }
369 virtual bool isMatrix() { return false; }
370 // /// These functions should report an error
371 // virtual char const* GetLabel() { return 0; }
372 // virtual char const* GetLabel(int) { return 0; }
374 // Vertical switching
376 virtual bool setArgumentIdx(int i) { return (i == 0); }
378 virtual bool setNextArgIdx() { return false; }
380 virtual int getArgumentIdx() { return 0; }
382 virtual int getMaxArgumentIdx() { return 0; }
384 // virtual void SetLabel(char const *) {}
386 virtual void SetStyle(short);
388 virtual MathedRowSt * getRowSt() const { return 0; }
390 virtual void setRowSt(MathedRowSt *) {}
392 virtual bool Permit(short f) { return bool(f & flag); }
395 /// Paragraph data is stored here
396 LyxArrayBase * array;
397 /// Cursor start position
406 virtual void setFlag(MathedParFlag f) { flag |= f; }
408 friend class InsetFormula;
410 friend class MathedXIter;
412 friend class MathedCursor;
414 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
415 LyxArrayBase * a = 0,
416 MathParInset ** p = 0);
420 /** The physical structure of a row and aditional information is stored here.
421 It allows to manage the extra info independently of the paragraph data.
422 Only used for multiline paragraphs.
428 w = new int[n + 1]; // this leaks
430 for (int i = 0 ; i < n + 1 ; ++i)
441 /// Should be const but...
442 MathedRowSt * getNext() const { return next; }
443 /// ...we couldn't use this.
444 void setNext(MathedRowSt * n) { next = n; }
446 char const * getLabel() const { return label; }
448 bool isNumbered() const { return numbered; }
450 int getBaseline() const { return y; }
452 int getTab(int i) { return w[i]; }
454 void setLabel(char * l) { label = l; }
456 void setNumbered(bool nf) { numbered = nf; }
458 void setTab(int i, int t) { w[i] = t; }
472 friend class MathMatrixInset;
474 friend class MathedXIter;
478 /** Multiline math paragraph base class.
479 This is the base to all multiline editable math objects
480 like array and eqnarray.
482 class MathMatrixInset: public MathParInset {
486 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
489 MathMatrixInset(MathMatrixInset *);
491 MathedInset * Clone();
493 virtual ~MathMatrixInset();
495 void draw(Painter &, int, int);
497 void Write(std::ostream &, bool fragile);
501 void SetData(LyxArrayBase *);
503 void SetAlign(char, char const *);
505 char * GetAlign(char * vv) {
512 int GetColumns() { return nc; }
514 int GetRows() { return nr; }
516 virtual bool isMatrix() { return true; }
518 /// Use this to manage the extra information independently of paragraph
519 MathedRowSt * getRowSt() const { return row; }
521 void setRowSt(MathedRowSt * r) { row = r; }
524 /// Number of columns & rows
531 char v_align; // add approp. signedness
534 /// Vertical structure
541 /************************* Prototypes **********************************/
543 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
546 void mathed_write(MathParInset *, std::ostream &, int *, bool fragile,
547 char const * label = 0);
550 void mathed_parser_file(std::istream &, int);
552 int mathed_parser_lineno();
554 int MathedLookupBOP(short);
556 /************************ Inline functions ********************************/
560 bool MathIsInset(short x)
562 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
567 bool MathIsFont(short x)
569 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
574 bool MathIsAlphaFont(short x)
576 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
581 bool MathIsActive(short x)
583 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
588 bool MathIsUp(short x)
590 return x == LM_TC_UP;
595 bool MathIsDown(short x)
597 return x == LM_TC_DOWN;
602 bool MathIsScript(short x)
604 return x == LM_TC_DOWN || x == LM_TC_UP;
609 bool MathIsBOPS(short x)
611 return MathedLookupBOP(x) > LMB_NONE;
616 bool MathIsBinary(short x)
618 return x == LM_TC_BOP || x == LM_TC_BOPS;
623 bool MathIsSymbol(short x) {
624 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
629 MathedInset::MathedInset(char const * nm, short ot, short st):
630 name(nm), objtype(ot), size(st)
632 width = ascent = descent = 0;
637 bool MathParInset::Inside(int x, int y)
639 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
644 void MathParInset::GetXY(int & x, int & y) const
651 void MathParInset::UserSetSize(short sz)
655 flag = flag & ~LMPF_FIXED_SIZE;
661 void MathParInset::SetStyle(short sz)
663 if (Permit(LMPF_FIXED_SIZE)) {
664 if (Permit(LMPF_SCRIPT))
665 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
666 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
669 MathedInset::SetStyle(sz);