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; }
267 static int workWidth;
287 /// In a near future maybe we use a better fonts renderer than X
288 void drawStr(Painter &, short, int, int, int, string const &);
290 friend class MathedCursor;
292 friend void mathed_init_fonts();
298 /// Paragraph permissions
301 /// If false can use a non-standard size
303 /// If true can insert newlines
305 /// If true can use tabs
307 /// If true can insert new columns
308 LMPF_ALLOW_NEW_COL = 8,
309 /// Smaller than current size (frac)
311 /// Script size (subscript, stackrel)
316 /** The math paragraph base class, base to all editable math objects */
317 class MathParInset: public MathedInset {
320 MathParInset(short st = LM_ST_TEXT, string const & nm = string(),
321 short ot = LM_OT_MIN);
324 MathParInset(MathParInset *);
326 virtual ~MathParInset();
328 virtual MathedInset * Clone();
330 /// Draw the object on a drawable
331 virtual void draw(Painter &, int x, int baseline);
334 virtual void Write(std::ostream &, bool fragile);
337 virtual void Metrics();
339 virtual void UserSetSize(short);
341 /// Data is stored in a LyXArray
342 virtual void SetData(MathedArray *);
344 virtual MathedArray * GetData() { return array; }
346 /// Paragraph position
347 virtual void GetXY(int &, int &) const;
349 virtual void setXY(int x, int y) { xo = x; yo = y; }
351 virtual void SetFocus(int, int) {}
353 virtual bool Inside(int, int);
355 // Tab stuff used by Matrix.
357 virtual void SetAlign(char, string const &) {}
359 virtual int GetColumns() const { return 1; }
361 virtual int GetRows() const { return 1; }
363 virtual bool isMatrix() const { return false; }
364 // Vertical switching
366 virtual bool setArgumentIdx(int i) { return (i == 0); }
368 virtual bool setNextArgIdx() { return false; }
370 virtual int getArgumentIdx() const { return 0; }
372 virtual int getMaxArgumentIdx() const { return 0; }
374 virtual void SetStyle(short);
376 virtual MathedRowSt * getRowSt() const { return 0; }
378 virtual void setRowSt(MathedRowSt *) {}
380 virtual bool Permit(short f) { return bool(f & flag); }
383 /// Paragraph data is stored here
385 /// Cursor start position
394 virtual void setFlag(MathedParFlag f) { flag |= f; }
396 friend class InsetFormula;
398 friend class MathedXIter;
400 friend class MathedCursor;
402 friend MathedArray * mathed_parse(unsigned flags = 0,
404 MathParInset ** p = 0);
408 /** The physical structure of a row and aditional information is stored here.
409 It allows to manage the extra info independently of the paragraph data.
410 Only used for multiline paragraphs.
416 w = new int[n + 1]; // this leaks
418 for (int i = 0 ; i < n + 1 ; ++i)
427 /// Should be const but...
428 MathedRowSt * getNext() const { return next; }
429 /// ...we couldn't use this.
430 void setNext(MathedRowSt * n) { next = n; }
432 string const & getLabel() const { return label; }
434 bool isNumbered() const { return numbered; }
436 int getBaseline() const { return y; }
438 void setBaseline(int b) { y = b; }
440 int ascent() const { return asc; }
442 int descent() const { return desc; }
444 void ascent(int a) { asc = a; }
446 void descent(int d) { desc = d; }
448 int getTab(int i) const { return w[i]; }
450 void setLabel(string const & l) { label = l; }
452 void setNumbered(bool nf) { numbered = nf; }
454 void setTab(int i, int t) { w[i] = t; }
470 /** Multiline math paragraph base class.
471 This is the base to all multiline editable math objects
472 like array and eqnarray.
474 class MathMatrixInset: public MathParInset {
478 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
481 MathMatrixInset(MathMatrixInset *);
483 MathedInset * Clone();
485 virtual ~MathMatrixInset();
487 void draw(Painter &, int, int);
489 void Write(std::ostream &, bool fragile);
493 void SetData(MathedArray *);
495 void SetAlign(char, string const &);
497 int GetColumns() const { return nc; }
499 int GetRows() const { return nr; }
501 virtual bool isMatrix() const { return true; }
503 /// Use this to manage the extra information independently of paragraph
504 MathedRowSt * getRowSt() const { return row; }
506 void setRowSt(MathedRowSt * r) { row = r; }
509 /// Number of columns & rows
516 char v_align; // add approp. type
518 string h_align; // a vector would perhaps be more correct
519 /// Vertical structure
526 /************************* Prototypes **********************************/
528 MathedArray * mathed_parse(unsigned flags, MathedArray * data,
531 void mathed_write(MathParInset *, std::ostream &, int *, bool fragile,
532 string const & label = string());
535 void mathed_parser_file(std::istream &, int);
537 int mathed_parser_lineno();
539 int MathedLookupBOP(short);
541 /************************ Inline functions ********************************/
545 bool MathIsInset(short x)
547 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
552 bool MathIsFont(short x)
554 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
559 bool MathIsAlphaFont(short x)
561 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
566 bool MathIsActive(short x)
568 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
573 bool MathIsUp(short x)
575 return x == LM_TC_UP;
580 bool MathIsDown(short x)
582 return x == LM_TC_DOWN;
587 bool MathIsScript(short x)
589 return x == LM_TC_DOWN || x == LM_TC_UP;
594 bool MathIsBOPS(short x)
596 return MathedLookupBOP(x) > LMB_NONE;
601 bool MathIsBinary(short x)
603 return x == LM_TC_BOP || x == LM_TC_BOPS;
608 bool MathIsSymbol(short x) {
609 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
614 MathedInset::MathedInset(string const & nm, short ot, short st):
615 name(nm), objtype(ot), size(st)
617 width = ascent = descent = 0;
622 bool MathParInset::Inside(int x, int y)
624 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
629 void MathParInset::GetXY(int & x, int & y) const
636 void MathParInset::UserSetSize(short sz)
640 flag = flag & ~LMPF_FIXED_SIZE;
646 void MathParInset::SetStyle(short sz)
648 if (Permit(LMPF_FIXED_SIZE)) {
649 if (Permit(LMPF_SCRIPT))
650 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
651 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
654 MathedInset::SetStyle(sz);
659 bool is_eqn_type(short int type)
661 return type >= LM_OT_MIN && type < LM_OT_MATRIX;
666 bool is_matrix_type(short int type)
668 return type == LM_OT_MATRIX;
672 bool is_multiline(short int type)
674 return type >= LM_OT_MPAR && type < LM_OT_MATRIX;
678 inline bool is_ams(short int type)
680 return type > LM_OT_MPARN && type < LM_OT_MATRIX;
684 bool is_singlely_numbered(short int type)
686 return type == LM_OT_PARN || type == LM_OT_MULTLINEN;
690 bool is_multi_numbered(short int type)
692 return type == LM_OT_MPARN || type == LM_OT_ALIGNN
693 || type == LM_OT_ALIGNATN;
697 bool is_numbered(short int type)
699 return is_singlely_numbered(type) || is_multi_numbered(type);
703 bool is_multicolumn(short int type)
705 return type == LM_OT_ALIGN || type == LM_OT_ALIGNN
706 || type == LM_OT_ALIGNAT || type == LM_OT_ALIGNATN;