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() {}
232 virtual void draw(Painter &, int x, int baseline) = 0;
235 virtual void Draw(int x, int baseline) = 0;
238 /// Write LaTeX and Lyx code
239 virtual void Write(ostream &) = 0;
241 /// Write LaTeX and Lyx code
242 virtual void Write(string & file) = 0;
244 /// Reproduces itself
245 virtual MathedInset * Clone() = 0;
247 /// Compute the size of the object
248 virtual void Metrics() = 0;
250 virtual int Ascent() const { return ascent; }
252 virtual int Descent() const { return descent; }
254 virtual int Width() const { return width; }
256 virtual int Height() const { return ascent + descent; }
259 virtual bool GetLimits() const { return false; }
261 virtual void SetLimits(bool) {}
264 char const * GetName() const { return name; }
266 short GetType() const { return objtype; }
268 short GetStyle() const { return size; }
270 //Man: Avoid to use these functions if it's not strictly necessary
272 virtual void SetType(short t) { objtype = t; }
274 virtual void SetStyle(short st) { size = st; } // Metrics();
276 virtual void SetName(char const * n) { name = n; }
279 void setDrawable(long unsigned int d) { pm = d; }
296 /// This works while only one process can draw unless
297 /// the process have their own data
298 static unsigned long pm;
301 static int df_asc, df_des, df_width;
303 /// In a near future maybe we use a better fonts renderer than X
305 void drawStr(Painter &, short, int, int, int, byte *, int);
307 void drawStr(short, int, int, int, byte *, int);
310 friend class MathedCursor;
312 friend void mathed_init_fonts();
318 /// Paragraph permissions
321 /// If false can use a non-standard size
323 /// If true can insert newlines
325 /// If true can use tabs
327 /// If true can insert new columns
328 LMPF_ALLOW_NEW_COL = 8,
329 /// Smaller than current size (frac)
331 /// Script size (subscript, stackrel)
336 /** The math paragraph base class, base to all editable math objects */
337 class MathParInset: public MathedInset {
340 MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
341 short ot = LM_OT_MIN);
343 MathParInset(MathParInset *);
345 virtual ~MathParInset();
347 virtual MathedInset * Clone();
349 /// Draw the object on a drawable
351 virtual void draw(Painter &, int x, int baseline);
353 virtual void Draw(int x, int baseline);
357 virtual void Write(ostream &);
360 virtual void Write(string & file);
362 virtual void Metrics();
364 virtual void UserSetSize(short);
366 /// Data is stored in a LyXArray
367 virtual void SetData(LyxArrayBase *);
369 virtual LyxArrayBase * GetData() { return array; }
371 /// Paragraph position
372 virtual void GetXY(int &, int &) const;
374 virtual void setXY(int x, int y) { xo = x; yo = y; }
376 virtual void SetFocus(int, int) {}
378 virtual bool Inside(int, int);
380 // Tab stuff used by Matrix.
382 virtual void SetAlign(char, char const *) {}
384 // virtual int GetTabPos() { return 0; }
386 // virtual int GetTab(int) { return 0; }
388 virtual int GetColumns() { return 1; }
390 virtual int GetRows() { return 1; }
392 virtual bool isMatrix() { return false; }
393 // /// These functions should report an error
394 // virtual char const* GetLabel() { return 0; }
395 // virtual char const* GetLabel(int) { return 0; }
397 // Vertical switching
399 virtual bool setArgumentIdx(int i) { return (i == 0); }
401 virtual bool setNextArgIdx() { return false; }
403 virtual int getArgumentIdx() { return 0; }
405 virtual int getMaxArgumentIdx() { return 0; }
407 // virtual void SetLabel(char const *) {}
409 virtual void SetStyle(short);
411 virtual MathedRowSt * getRowSt() const { return 0; }
413 virtual void setRowSt(MathedRowSt *) {}
415 virtual bool Permit(short f) { return bool(f & flag); }
418 /// Paragraph data is stored here
419 LyxArrayBase * array;
420 /// Cursor start position
427 virtual void setFlag(MathedParFlag f) { flag |= f; }
429 friend class InsetFormula;
431 friend class MathedXIter;
433 friend class MathedCursor;
435 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
436 LyxArrayBase * a = 0,
437 MathParInset ** p = 0);
441 /* The physical structure of a row and aditional information is stored here.
442 It allows to manage the extra info independently of the paragraph data.
443 Only used for multiline paragraphs.
448 w = new int[n + 1]; // this leaks
450 for (int i = 0 ; i < n + 1 ; ++i)
461 /// Should be const but...
462 MathedRowSt * getNext() const { return next; }
463 /// ...we couldn't use this.
464 void setNext(MathedRowSt * n) { next = n; }
466 char const * getLabel() const { return label; }
468 bool isNumbered() const { return numbered; }
470 int getBaseline() const { return y; }
472 int getTab(int i) { return w[i]; }
474 void setLabel(char * l) { label = l; }
476 void setNumbered(bool nf) { numbered = nf; }
478 void setTab(int i, int t) { w[i] = t; }
492 friend class MathMatrixInset;
494 friend class MathedXIter;
498 /** Multiline math paragraph base class.
499 This is the base to all multiline editable math objects
500 like array and eqnarray.
502 class MathMatrixInset: public MathParInset {
505 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
507 MathMatrixInset(MathMatrixInset *);
509 MathedInset * Clone();
511 virtual ~MathMatrixInset();
514 void draw(Painter &, int, int);
520 void Write(ostream &);
522 void Write(string & file);
526 void SetData(LyxArrayBase *);
528 void SetAlign(char, char const *);
530 char * GetAlign(char * vv) {
537 int GetColumns() { return nc; }
539 int GetRows() { return nr; }
541 virtual bool isMatrix() { return true; }
543 /// Use this to manage the extra information independently of paragraph
544 MathedRowSt * getRowSt() const { return row; }
546 void setRowSt(MathedRowSt * r) { row = r; }
549 /// Number of columns & rows
554 char v_align; // add approp. signedness
557 /// Vertical structure
564 /************************* Prototypes **********************************/
566 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
569 void mathed_write(MathParInset *, ostream &, int *, char fragile,
570 char const * label = 0);
573 void mathed_write(MathParInset *, string &, int *, char fragile,
574 char const * label = 0);
576 void mathed_parser_file(istream &, int);
578 int mathed_parser_lineno();
580 int MathedLookupBOP(short);
582 /************************ Inline functions ********************************/
584 inline bool MathIsInset(short x)
586 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
590 inline bool MathIsFont(short x)
592 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
596 inline bool MathIsAlphaFont(short x)
598 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
602 inline bool MathIsActive(short x)
604 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
608 inline bool MathIsUp(short x)
610 return x == LM_TC_UP;
614 inline bool MathIsDown(short x)
616 return x == LM_TC_DOWN;
620 inline bool MathIsScript(short x)
622 return x == LM_TC_DOWN || x == LM_TC_UP;
626 inline bool MathIsBOPS(short x)
628 return MathedLookupBOP(x) > LMB_NONE;
633 inline bool MathIsBinary(short x)
635 return x == LM_TC_BOP || x == LM_TC_BOPS;
639 inline bool MathIsSymbol(short x) {
640 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
645 MathedInset::MathedInset(char const * nm, short ot, short st):
646 name(nm), objtype(ot), size(st)
648 width = ascent = descent = 0;
652 bool MathParInset::Inside(int x, int y)
654 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
659 void MathParInset::GetXY(int & x, int & y) const
666 void MathParInset::UserSetSize(short sz)
670 flag = flag & ~LMPF_FIXED_SIZE;
676 void MathParInset::SetStyle(short sz)
678 if (Permit(LMPF_FIXED_SIZE)) {
679 if (Permit(LMPF_SCRIPT))
680 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
681 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
684 MathedInset::SetStyle(sz);