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.
46 MATH_ALIGN_BOTTOM = 4,
56 /// Standard Math Sizes (Math mode styles)
69 /// Standard LaTeX Math Environments
70 enum MathedEnvironment {
86 /** The restrictions of a standard LaTeX math paragraph
87 allows to get a small number of text codes (<30) */
88 enum MathedTextCodes {
91 /// Open and Close group
101 /// Super and sub scripts
105 /// Editable Math Inset
107 /// Editable Text Inset
111 /// Internal code for constants
113 /// Internal code for variables
129 /// Math mode TeX characters ",;:{}"
131 /// Special characters "{}&#_%"
133 /// Internal code for operators
135 /// Internal code for symbols
144 /// This must be < 32
148 ostream & operator<<(ostream &, MathedTextCodes mtc);
151 #define LM_TC_NORMAL LM_TC_VAR
154 /// Types of lyx-math insets
155 enum MathedInsetTypes {
158 /// A simple paragraph
160 /// A simple numbered paragraph
162 /// A multiline paragraph
164 /// A multiline numbered paragraph
205 enum MathedBinaryTypes {
213 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
218 /** Abstract base class for all math objects.
219 A math insets is for use of the math editor only, it isn't a
220 general LyX inset. It's used to represent all the math objects.
221 The formulaInset (a LyX inset) encapsulates a math inset.
225 /// A math inset has a name (usually its LaTeX name), type and font-size
226 MathedInset(char const * nm, short ot, short st);
228 MathedInset(MathedInset *);
230 virtual ~MathedInset() {}
233 virtual void draw(Painter &, int x, int baseline) = 0;
235 /// Write LaTeX and Lyx code
236 virtual void Write(ostream &) = 0;
238 /// Reproduces itself
239 virtual MathedInset * Clone() = 0;
241 /// Compute the size of the object
242 virtual void Metrics() = 0;
244 virtual int Ascent() const { return ascent; }
246 virtual int Descent() const { return descent; }
248 virtual int Width() const { return width; }
250 virtual int Height() const { return ascent + descent; }
253 virtual bool GetLimits() const { return false; }
255 virtual void SetLimits(bool) {}
258 char const * GetName() const { return name; }
260 short GetType() const { return objtype; }
262 short GetStyle() const { return size; }
264 //Man: Avoid to use these functions if it's not strictly necessary
266 virtual void SetType(short t) { objtype = t; }
268 virtual void SetStyle(short st) { size = st; } // Metrics();
270 virtual void SetName(char const * n) { name = n; }
285 static int df_asc, df_des, df_width;
287 /// In a near future maybe we use a better fonts renderer than X
288 void drawStr(Painter &, short, int, int, int, byte const *, int);
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, char const * nm = 0,
321 short ot = LM_OT_MIN);
323 MathParInset(MathParInset *);
325 virtual ~MathParInset();
327 virtual MathedInset * Clone();
329 /// Draw the object on a drawable
330 virtual void draw(Painter &, int x, int baseline);
333 virtual void Write(ostream &);
336 virtual void Metrics();
338 virtual void UserSetSize(short);
340 /// Data is stored in a LyXArray
341 virtual void SetData(LyxArrayBase *);
343 virtual LyxArrayBase * GetData() { return array; }
345 /// Paragraph position
346 virtual void GetXY(int &, int &) const;
348 virtual void setXY(int x, int y) { xo = x; yo = y; }
350 virtual void SetFocus(int, int) {}
352 virtual bool Inside(int, int);
354 // Tab stuff used by Matrix.
356 virtual void SetAlign(char, char const *) {}
358 // virtual int GetTabPos() { return 0; }
360 // virtual int GetTab(int) { return 0; }
362 virtual int GetColumns() { return 1; }
364 virtual int GetRows() { return 1; }
366 virtual bool isMatrix() { return false; }
367 // /// These functions should report an error
368 // virtual char const* GetLabel() { return 0; }
369 // virtual char const* GetLabel(int) { return 0; }
371 // Vertical switching
373 virtual bool setArgumentIdx(int i) { return (i == 0); }
375 virtual bool setNextArgIdx() { return false; }
377 virtual int getArgumentIdx() { return 0; }
379 virtual int getMaxArgumentIdx() { return 0; }
381 // virtual void SetLabel(char const *) {}
383 virtual void SetStyle(short);
385 virtual MathedRowSt * getRowSt() const { return 0; }
387 virtual void setRowSt(MathedRowSt *) {}
389 virtual bool Permit(short f) { return bool(f & flag); }
392 /// Paragraph data is stored here
393 LyxArrayBase * array;
394 /// Cursor start position
401 virtual void setFlag(MathedParFlag f) { flag |= f; }
403 friend class InsetFormula;
405 friend class MathedXIter;
407 friend class MathedCursor;
409 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
410 LyxArrayBase * a = 0,
411 MathParInset ** p = 0);
415 /* The physical structure of a row and aditional information is stored here.
416 It allows to manage the extra info independently of the paragraph data.
417 Only used for multiline paragraphs.
422 w = new int[n + 1]; // this leaks
424 for (int i = 0 ; i < n + 1 ; ++i)
435 /// Should be const but...
436 MathedRowSt * getNext() const { return next; }
437 /// ...we couldn't use this.
438 void setNext(MathedRowSt * n) { next = n; }
440 char const * getLabel() const { return label; }
442 bool isNumbered() const { return numbered; }
444 int getBaseline() const { return y; }
446 int getTab(int i) { return w[i]; }
448 void setLabel(char * l) { label = l; }
450 void setNumbered(bool nf) { numbered = nf; }
452 void setTab(int i, int t) { w[i] = t; }
466 friend class MathMatrixInset;
468 friend class MathedXIter;
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 {
479 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(ostream &);
493 void SetData(LyxArrayBase *);
495 void SetAlign(char, char const *);
497 char * GetAlign(char * vv) {
504 int GetColumns() { return nc; }
506 int GetRows() { return nr; }
508 virtual bool isMatrix() { return true; }
510 /// Use this to manage the extra information independently of paragraph
511 MathedRowSt * getRowSt() const { return row; }
513 void setRowSt(MathedRowSt * r) { row = r; }
516 /// Number of columns & rows
521 char v_align; // add approp. signedness
524 /// Vertical structure
531 /************************* Prototypes **********************************/
533 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
536 void mathed_write(MathParInset *, ostream &, int *, char fragile,
537 char const * label = 0);
540 void mathed_parser_file(istream &, int);
542 int mathed_parser_lineno();
544 int MathedLookupBOP(short);
546 /************************ Inline functions ********************************/
549 bool MathIsInset(short x)
551 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
556 bool MathIsFont(short x)
558 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
563 bool MathIsAlphaFont(short x)
565 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
570 bool MathIsActive(short x)
572 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
577 bool MathIsUp(short x)
579 return x == LM_TC_UP;
584 bool MathIsDown(short x)
586 return x == LM_TC_DOWN;
591 bool MathIsScript(short x)
593 return x == LM_TC_DOWN || x == LM_TC_UP;
598 bool MathIsBOPS(short x)
600 return MathedLookupBOP(x) > LMB_NONE;
606 bool MathIsBinary(short x)
608 return x == LM_TC_BOP || x == LM_TC_BOPS;
613 bool MathIsSymbol(short x) {
614 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
619 MathedInset::MathedInset(char const * nm, short ot, short st):
620 name(nm), objtype(ot), size(st)
622 width = ascent = descent = 0;
626 bool MathParInset::Inside(int x, int y)
628 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
633 void MathParInset::GetXY(int & x, int & y) const
640 void MathParInset::UserSetSize(short sz)
644 flag = flag & ~LMPF_FIXED_SIZE;
650 void MathParInset::SetStyle(short sz)
652 if (Permit(LMPF_FIXED_SIZE)) {
653 if (Permit(LMPF_SCRIPT))
654 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
655 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
658 MathedInset::SetStyle(sz);