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 /// 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(string 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 string 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(string 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, string const & nm = string(),
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, string const &) {}
361 virtual int GetColumns() const { return 1; }
363 virtual int GetRows() const { return 1; }
365 virtual bool isMatrix() const { return false; }
366 // Vertical switching
368 virtual bool setArgumentIdx(int i) { return (i == 0); }
370 virtual bool setNextArgIdx() { return false; }
372 virtual int getArgumentIdx() const { return 0; }
374 virtual int getMaxArgumentIdx() const { return 0; }
376 virtual void SetStyle(short);
378 virtual MathedRowSt * getRowSt() const { return 0; }
380 virtual void setRowSt(MathedRowSt *) {}
382 virtual bool Permit(short f) { return bool(f & flag); }
385 /// Paragraph data is stored here
386 LyxArrayBase * array;
387 /// Cursor start position
396 virtual void setFlag(MathedParFlag f) { flag |= f; }
398 friend class InsetFormula;
400 friend class MathedXIter;
402 friend class MathedCursor;
404 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
405 LyxArrayBase * a = 0,
406 MathParInset ** p = 0);
410 /** The physical structure of a row and aditional information is stored here.
411 It allows to manage the extra info independently of the paragraph data.
412 Only used for multiline paragraphs.
418 w = new int[n + 1]; // this leaks
420 for (int i = 0 ; i < n + 1 ; ++i)
429 /// Should be const but...
430 MathedRowSt * getNext() const { return next; }
431 /// ...we couldn't use this.
432 void setNext(MathedRowSt * n) { next = n; }
434 string const & getLabel() const { return label; }
436 bool isNumbered() const { return numbered; }
438 int getBaseline() const { return y; }
440 void setBaseline(int b) { y = b; }
442 int ascent() const { return asc; }
444 int descent() const { return desc; }
446 void ascent(int a) { asc = a; }
448 void descent(int d) { desc = d; }
450 int getTab(int i) const { return w[i]; }
452 void setLabel(string const & l) { label = l; }
454 void setNumbered(bool nf) { numbered = nf; }
456 void setTab(int i, int t) { w[i] = t; }
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, string const &);
499 int GetColumns() const { return nc; }
501 int GetRows() const { return nr; }
503 virtual bool isMatrix() const { return true; }
505 /// Use this to manage the extra information independently of paragraph
506 MathedRowSt * getRowSt() const { return row; }
508 void setRowSt(MathedRowSt * r) { row = r; }
511 /// Number of columns & rows
518 char v_align; // add approp. type
520 string h_align; // a vector would perhaps be more correct
521 /// Vertical structure
528 /************************* Prototypes **********************************/
530 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
533 void mathed_write(MathParInset *, std::ostream &, int *, bool fragile,
534 string const & label = string());
537 void mathed_parser_file(std::istream &, int);
539 int mathed_parser_lineno();
541 int MathedLookupBOP(short);
543 /************************ Inline functions ********************************/
547 bool MathIsInset(short x)
549 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
554 bool MathIsFont(short x)
556 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
561 bool MathIsAlphaFont(short x)
563 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
568 bool MathIsActive(short x)
570 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
575 bool MathIsUp(short x)
577 return x == LM_TC_UP;
582 bool MathIsDown(short x)
584 return x == LM_TC_DOWN;
589 bool MathIsScript(short x)
591 return x == LM_TC_DOWN || x == LM_TC_UP;
596 bool MathIsBOPS(short x)
598 return MathedLookupBOP(x) > LMB_NONE;
603 bool MathIsBinary(short x)
605 return x == LM_TC_BOP || x == LM_TC_BOPS;
610 bool MathIsSymbol(short x) {
611 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
616 MathedInset::MathedInset(string const & nm, short ot, short st):
617 name(nm), objtype(ot), size(st)
619 width = ascent = descent = 0;
624 bool MathParInset::Inside(int x, int y)
626 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
631 void MathParInset::GetXY(int & x, int & y) const
638 void MathParInset::UserSetSize(short sz)
642 flag = flag & ~LMPF_FIXED_SIZE;
648 void MathParInset::SetStyle(short sz)
650 if (Permit(LMPF_FIXED_SIZE)) {
651 if (Permit(LMPF_SCRIPT))
652 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
653 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
656 MathedInset::SetStyle(sz);