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(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() { return 1; }
363 virtual int GetRows() { return 1; }
365 virtual bool isMatrix() { return false; }
366 // Vertical switching
368 virtual bool setArgumentIdx(int i) { return (i == 0); }
370 virtual bool setNextArgIdx() { return false; }
372 virtual int getArgumentIdx() { return 0; }
374 virtual int getMaxArgumentIdx() { 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)
431 /// Should be const but...
432 MathedRowSt * getNext() const { return next; }
433 /// ...we couldn't use this.
434 void setNext(MathedRowSt * n) { next = n; }
436 string const & getLabel() const { return label; }
438 bool isNumbered() const { return numbered; }
440 int getBaseline() const { return y; }
442 int getTab(int i) { return w[i]; }
444 void setLabel(string const & l) { label = l; }
446 void setNumbered(bool nf) { numbered = nf; }
448 void setTab(int i, int t) { w[i] = t; }
462 friend class MathMatrixInset;
464 friend class MathedXIter;
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(LyxArrayBase *);
493 void SetAlign(char, string const &);
495 char * GetAlign(char * vv) {
500 int GetColumns() { return nc; }
502 int GetRows() { return nr; }
504 virtual bool isMatrix() { return true; }
506 /// Use this to manage the extra information independently of paragraph
507 MathedRowSt * getRowSt() const { return row; }
509 void setRowSt(MathedRowSt * r) { row = r; }
512 /// Number of columns & rows
519 char v_align; // add approp. type
522 /// Vertical structure
529 /************************* Prototypes **********************************/
531 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
534 void mathed_write(MathParInset *, std::ostream &, int *, bool fragile,
535 string const & label = string());
538 void mathed_parser_file(std::istream &, int);
540 int mathed_parser_lineno();
542 int MathedLookupBOP(short);
544 /************************ Inline functions ********************************/
548 bool MathIsInset(short x)
550 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
555 bool MathIsFont(short x)
557 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
562 bool MathIsAlphaFont(short x)
564 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
569 bool MathIsActive(short x)
571 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
576 bool MathIsUp(short x)
578 return x == LM_TC_UP;
583 bool MathIsDown(short x)
585 return x == LM_TC_DOWN;
590 bool MathIsScript(short x)
592 return x == LM_TC_DOWN || x == LM_TC_UP;
597 bool MathIsBOPS(short x)
599 return MathedLookupBOP(x) > LMB_NONE;
604 bool MathIsBinary(short x)
606 return x == LM_TC_BOP || x == LM_TC_BOPS;
611 bool MathIsSymbol(short x) {
612 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
617 MathedInset::MathedInset(string const & nm, short ot, short st):
618 name(nm), objtype(ot), size(st)
620 width = ascent = descent = 0;
625 bool MathParInset::Inside(int x, int y)
627 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
632 void MathParInset::GetXY(int & x, int & y) const
639 void MathParInset::UserSetSize(short sz)
643 flag = flag & ~LMPF_FIXED_SIZE;
649 void MathParInset::SetStyle(short sz)
651 if (Permit(LMPF_FIXED_SIZE)) {
652 if (Permit(LMPF_SCRIPT))
653 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
654 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
657 MathedInset::SetStyle(sz);