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: (c) 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.
27 #include "support/LIstream.h"
44 MATH_ALIGN_BOTTOM = 4,
54 /// Standard Math Sizes (Math mode styles)
67 /// Standard LaTeX Math Environments
68 enum MathedEnvironment {
84 /** The restrictions of a standard LaTeX math paragraph
85 allows to get a small number of text codes (<30) */
86 enum MathedTextCodes {
89 /// Open and Close group
99 /// Super and sub scripts
103 /// Editable Math Inset
105 /// Editable Text Inset
109 /// Internal code for constants
111 /// Internal code for variables
127 /// Math mode TeX characters ",;:{}"
129 /// Special characters "{}&#_%"
131 /// Internal code for operators
133 /// Internal code for symbols
142 /// This must be < 32
146 ostream & operator<<(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);
226 MathedInset(MathedInset *);
228 virtual ~MathedInset() {}
231 virtual void draw(Painter &, int x, int baseline) = 0;
233 /// Write LaTeX and Lyx code
234 virtual void Write(ostream &) = 0;
236 /// Write LaTeX and Lyx code
237 virtual void Write(string & file) = 0;
239 /// Reproduces itself
240 virtual MathedInset * Clone() = 0;
242 /// Compute the size of the object
243 virtual void Metrics() = 0;
245 virtual int Ascent() const { return ascent; }
247 virtual int Descent() const { return descent; }
249 virtual int Width() const { return width; }
251 virtual int Height() const { return ascent + descent; }
254 virtual bool GetLimits() const { return false; }
256 virtual void SetLimits(bool) {}
259 char const * GetName() const { return name; }
261 short GetType() const { return objtype; }
263 short GetStyle() const { return size; }
265 //Man: Avoid to use these functions if it's not strictly necessary
267 virtual void SetType(short t) { objtype = t; }
269 virtual void SetStyle(short st) { size = st; } // Metrics();
271 virtual void SetName(char const * n) { name = n; }
286 static int df_asc, df_des, df_width;
288 /// In a near future maybe we use a better fonts renderer than X
289 void drawStr(Painter &, short, int, int, int, byte *, int);
291 friend class MathedCursor;
293 friend void mathed_init_fonts();
299 /// Paragraph permissions
302 /// If false can use a non-standard size
304 /// If true can insert newlines
306 /// If true can use tabs
308 /// If true can insert new columns
309 LMPF_ALLOW_NEW_COL = 8,
310 /// Smaller than current size (frac)
312 /// Script size (subscript, stackrel)
317 /** The math paragraph base class, base to all editable math objects */
318 class MathParInset: public MathedInset {
321 MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
322 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(ostream &);
337 virtual void Write(string & file);
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
404 virtual void setFlag(MathedParFlag f) { flag |= f; }
406 friend class InsetFormula;
408 friend class MathedXIter;
410 friend class MathedCursor;
412 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
413 LyxArrayBase * a = 0,
414 MathParInset ** p = 0);
418 /* The physical structure of a row and aditional information is stored here.
419 It allows to manage the extra info independently of the paragraph data.
420 Only used for multiline paragraphs.
425 w = new int[n + 1]; // this leaks
427 for (int i = 0 ; i < n + 1 ; ++i)
438 /// Should be const but...
439 MathedRowSt * getNext() const { return next; }
440 /// ...we couldn't use this.
441 void setNext(MathedRowSt * n) { next = n; }
443 char const * getLabel() const { return label; }
445 bool isNumbered() const { return numbered; }
447 int getBaseline() const { return y; }
449 int getTab(int i) { return w[i]; }
451 void setLabel(char * l) { label = l; }
453 void setNumbered(bool nf) { numbered = nf; }
455 void setTab(int i, int t) { w[i] = t; }
469 friend class MathMatrixInset;
471 friend class MathedXIter;
475 /** Multiline math paragraph base class.
476 This is the base to all multiline editable math objects
477 like array and eqnarray.
479 class MathMatrixInset: public MathParInset {
482 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
484 MathMatrixInset(MathMatrixInset *);
486 MathedInset * Clone();
488 virtual ~MathMatrixInset();
490 void draw(Painter &, int, int);
492 void Write(ostream &);
494 void Write(string & file);
498 void SetData(LyxArrayBase *);
500 void SetAlign(char, char const *);
502 char * GetAlign(char * vv) {
509 int GetColumns() { return nc; }
511 int GetRows() { return nr; }
513 virtual bool isMatrix() { return true; }
515 /// Use this to manage the extra information independently of paragraph
516 MathedRowSt * getRowSt() const { return row; }
518 void setRowSt(MathedRowSt * r) { row = r; }
521 /// Number of columns & rows
526 char v_align; // add approp. signedness
529 /// Vertical structure
536 /************************* Prototypes **********************************/
538 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
541 void mathed_write(MathParInset *, ostream &, int *, char fragile,
542 char const * label = 0);
545 void mathed_write(MathParInset *, string &, int *, char fragile,
546 char const * label = 0);
548 void mathed_parser_file(istream &, int);
550 int mathed_parser_lineno();
552 int MathedLookupBOP(short);
554 /************************ Inline functions ********************************/
556 inline bool MathIsInset(short x)
558 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
562 inline bool MathIsFont(short x)
564 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
568 inline bool MathIsAlphaFont(short x)
570 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
574 inline bool MathIsActive(short x)
576 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
580 inline bool MathIsUp(short x)
582 return x == LM_TC_UP;
586 inline bool MathIsDown(short x)
588 return x == LM_TC_DOWN;
592 inline bool MathIsScript(short x)
594 return x == LM_TC_DOWN || x == LM_TC_UP;
598 inline bool MathIsBOPS(short x)
600 return MathedLookupBOP(x) > LMB_NONE;
605 inline bool MathIsBinary(short x)
607 return x == LM_TC_BOP || x == LM_TC_BOPS;
611 inline bool MathIsSymbol(short x) {
612 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
617 MathedInset::MathedInset(char const * nm, short ot, short st):
618 name(nm), objtype(ot), size(st)
620 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);