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,
48 static int const MATH_COLSEP = 8;
50 static int const MATH_ROWSEP = 8;
53 /// Standard Math Sizes (Math mode styles)
66 /** The restrictions of a standard LaTeX math paragraph
67 allows to get a small number of text codes (<30) */
68 enum MathedTextCodes {
71 /// Open and Close group
81 /// Super and sub scripts
85 /// Editable Math Inset
87 /// Editable Text Inset
91 /// Internal code for constants
93 /// Internal code for variables
109 /// Math mode TeX characters ",;:{}"
111 /// Special characters "{}&#_%"
113 /// Internal code for operators
115 /// Internal code for symbols
124 /// This must be < 32
129 std::ostream & operator<<(std::ostream &, MathedTextCodes mtc);
132 #define LM_TC_NORMAL LM_TC_VAR
135 /// Types of lyx-math insets
136 enum MathedInsetTypes {
139 /// A simple paragraph
141 /// A simple numbered paragraph
143 /// A multiline paragraph
145 /// A multiline numbered paragraph
199 enum MathedBinaryTypes {
207 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
212 /** Abstract base class for all math objects.
213 A math insets is for use of the math editor only, it isn't a
214 general LyX inset. It's used to represent all the math objects.
215 The formulaInset (a LyX inset) encapsulates a math inset.
219 /// A math inset has a name (usually its LaTeX name), type and font-size
220 MathedInset(string const & nm, short ot, short st);
223 MathedInset(MathedInset *);
225 virtual ~MathedInset() {}
228 virtual void draw(Painter &, int x, int baseline) = 0;
230 /// Write LaTeX and Lyx code
231 virtual void Write(std::ostream &, bool fragile) = 0;
233 /// Reproduces itself
234 virtual MathedInset * Clone() = 0;
236 /// Compute the size of the object
237 virtual void Metrics() = 0;
239 virtual int Ascent() const { return ascent; }
241 virtual int Descent() const { return descent; }
243 virtual int Width() const { return width; }
245 virtual int Height() const { return ascent + descent; }
248 virtual bool GetLimits() const { return false; }
250 virtual void SetLimits(bool) {}
253 string const & GetName() const { return name; }
255 short GetType() const { return objtype; }
257 short GetStyle() const { return size; }
259 //Man: Avoid to use these functions if it's not strictly necessary
261 virtual void SetType(short t) { objtype = t; }
263 virtual void SetStyle(short st) { size = st; } // Metrics();
265 virtual void SetName(string const & n) { name = n; }
285 /// In a near future maybe we use a better fonts renderer than X
286 void drawStr(Painter &, short, int, int, int, string const &);
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, string const & nm = string(),
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(MathedArray *);
342 virtual MathedArray * 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, string const &) {}
357 virtual int GetColumns() const { return 1; }
359 virtual int GetRows() const { return 1; }
361 virtual bool isMatrix() const { return false; }
362 // Vertical switching
364 virtual bool setArgumentIdx(int i) { return (i == 0); }
366 virtual bool setNextArgIdx() { return false; }
368 virtual int getArgumentIdx() const { return 0; }
370 virtual int getMaxArgumentIdx() const { return 0; }
372 virtual void SetStyle(short);
374 virtual MathedRowSt * getRowSt() const { return 0; }
376 virtual void setRowSt(MathedRowSt *) {}
378 virtual bool Permit(short f) { return bool(f & flag); }
381 /// Paragraph data is stored here
383 /// Cursor start position
392 virtual void setFlag(MathedParFlag f) { flag |= f; }
394 friend class InsetFormula;
396 friend class MathedXIter;
398 friend class MathedCursor;
400 friend MathedArray * mathed_parse(unsigned flags = 0,
402 MathParInset ** p = 0);
406 /** The physical structure of a row and aditional information is stored here.
407 It allows to manage the extra info independently of the paragraph data.
408 Only used for multiline paragraphs.
414 w = new int[n + 1]; // this leaks
416 for (int i = 0 ; i < n + 1 ; ++i)
425 /// Should be const but...
426 MathedRowSt * getNext() const { return next; }
427 /// ...we couldn't use this.
428 void setNext(MathedRowSt * n) { next = n; }
430 string const & getLabel() const { return label; }
432 bool isNumbered() const { return numbered; }
434 int getBaseline() const { return y; }
436 void setBaseline(int b) { y = b; }
438 int ascent() const { return asc; }
440 int descent() const { return desc; }
442 void ascent(int a) { asc = a; }
444 void descent(int d) { desc = d; }
446 int getTab(int i) const { return w[i]; }
448 void setLabel(string const & l) { label = l; }
450 void setNumbered(bool nf) { numbered = nf; }
452 void setTab(int i, int t) { w[i] = t; }
468 /** Multiline math paragraph base class.
469 This is the base to all multiline editable math objects
470 like array and eqnarray.
472 class MathMatrixInset: public MathParInset {
476 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
479 MathMatrixInset(MathMatrixInset *);
481 MathedInset * Clone();
483 virtual ~MathMatrixInset();
485 void draw(Painter &, int, int);
487 void Write(std::ostream &, bool fragile);
491 void SetData(MathedArray *);
493 void SetAlign(char, string const &);
495 int GetColumns() const { return nc; }
497 int GetRows() const { return nr; }
499 virtual bool isMatrix() const { return true; }
501 /// Use this to manage the extra information independently of paragraph
502 MathedRowSt * getRowSt() const { return row; }
504 void setRowSt(MathedRowSt * r) { row = r; }
507 /// Number of columns & rows
514 char v_align; // add approp. type
516 string h_align; // a vector would perhaps be more correct
517 /// Vertical structure
524 /************************* Prototypes **********************************/
526 MathedArray * mathed_parse(unsigned flags, MathedArray * data,
529 void mathed_write(MathParInset *, std::ostream &, int *, bool fragile,
530 string const & label = string());
533 void mathed_parser_file(std::istream &, int);
535 int mathed_parser_lineno();
537 int MathedLookupBOP(short);
539 /************************ Inline functions ********************************/
543 bool MathIsInset(short x)
545 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
550 bool MathIsFont(short x)
552 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
557 bool MathIsAlphaFont(short x)
559 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
564 bool MathIsActive(short x)
566 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
571 bool MathIsUp(short x)
573 return x == LM_TC_UP;
578 bool MathIsDown(short x)
580 return x == LM_TC_DOWN;
585 bool MathIsScript(short x)
587 return x == LM_TC_DOWN || x == LM_TC_UP;
592 bool MathIsBOPS(short x)
594 return MathedLookupBOP(x) > LMB_NONE;
599 bool MathIsBinary(short x)
601 return x == LM_TC_BOP || x == LM_TC_BOPS;
606 bool MathIsSymbol(short x) {
607 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
612 MathedInset::MathedInset(string const & nm, short ot, short st):
613 name(nm), objtype(ot), size(st)
615 width = ascent = descent = 0;
620 bool MathParInset::Inside(int x, int y)
622 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
627 void MathParInset::GetXY(int & x, int & y) const
634 void MathParInset::UserSetSize(short sz)
638 flag = flag & ~LMPF_FIXED_SIZE;
644 void MathParInset::SetStyle(short sz)
646 if (Permit(LMPF_FIXED_SIZE)) {
647 if (Permit(LMPF_SCRIPT))
648 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
649 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
652 MathedInset::SetStyle(sz);
657 bool is_eqn_type(short int type)
659 return type >= LM_OT_MIN && type < LM_OT_MATRIX;
664 bool is_matrix_type(short int type)
666 return type == LM_OT_MATRIX;
670 bool is_multiline(short int type)
672 return type >= LM_OT_MPAR && type < LM_OT_MATRIX;
676 inline bool is_ams(short int type)
678 return type > LM_OT_MPARN && type < LM_OT_MATRIX;
682 bool is_singlely_numbered(short int type)
684 return type == LM_OT_PARN || type == LM_OT_MULTLINEN;
688 bool is_multi_numbered(short int type)
690 return type == LM_OT_MPARN || type == LM_OT_ALIGNN
691 || type == LM_OT_ALIGNATN;
695 bool is_numbered(short int type)
697 return is_singlely_numbered(type) || is_multi_numbered(type);
701 bool is_multicolumn(short int type)
703 return type == LM_OT_ALIGN || type == LM_OT_ALIGNN
704 || type == LM_OT_ALIGNAT || type == LM_OT_ALIGNATN;