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.
29 #include "support/LIstream.h"
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() {}
234 virtual void draw(Painter &, int x, int baseline) = 0;
237 virtual void Draw(int x, int baseline) = 0;
240 /// Write LaTeX and Lyx code
241 virtual void Write(ostream &) = 0;
243 /// Write LaTeX and Lyx code
244 virtual void Write(string & file) = 0;
246 /// Reproduces itself
247 virtual MathedInset * Clone() = 0;
249 /// Compute the size of the object
250 virtual void Metrics() = 0;
252 virtual int Ascent() const { return ascent; }
254 virtual int Descent() const { return descent; }
256 virtual int Width() const { return width; }
258 virtual int Height() const { return ascent + descent; }
261 virtual bool GetLimits() const { return false; }
263 virtual void SetLimits(bool) {}
266 char const * GetName() const { return name; }
268 short GetType() const { return objtype; }
270 short GetStyle() const { return size; }
272 //Man: Avoid to use these functions if it's not strictly necessary
274 virtual void SetType(short t) { objtype = t; }
276 virtual void SetStyle(short st) { size = st; } // Metrics();
278 virtual void SetName(char const * n) { name = n; }
281 void setDrawable(long unsigned int d) { pm = d; }
298 /// This works while only one process can draw unless
299 /// the process have their own data
300 static unsigned long pm;
303 static int df_asc, df_des, df_width;
305 /// In a near future maybe we use a better fonts renderer than X
307 void drawStr(Painter &, short, int, int, int, byte *, int);
309 void drawStr(short, int, int, int, byte *, int);
312 friend class MathedCursor;
314 friend void mathed_init_fonts();
320 /// Paragraph permissions
323 /// If false can use a non-standard size
325 /// If true can insert newlines
327 /// If true can use tabs
329 /// If true can insert new columns
330 LMPF_ALLOW_NEW_COL = 8,
331 /// Smaller than current size (frac)
333 /// Script size (subscript, stackrel)
338 /** The math paragraph base class, base to all editable math objects */
339 class MathParInset: public MathedInset {
342 MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
343 short ot = LM_OT_MIN);
345 MathParInset(MathParInset *);
347 virtual ~MathParInset();
349 virtual MathedInset * Clone();
351 /// Draw the object on a drawable
353 virtual void draw(Painter &, int x, int baseline);
355 virtual void Draw(int x, int baseline);
359 virtual void Write(ostream &);
362 virtual void Write(string & file);
364 virtual void Metrics();
366 virtual void UserSetSize(short);
368 /// Data is stored in a LyXArray
369 virtual void SetData(LyxArrayBase *);
371 virtual LyxArrayBase * GetData() { return array; }
373 /// Paragraph position
374 virtual void GetXY(int &, int &) const;
376 virtual void setXY(int x, int y) { xo = x; yo = y; }
378 virtual void SetFocus(int, int) {}
380 virtual bool Inside(int, int);
382 // Tab stuff used by Matrix.
384 virtual void SetAlign(char, char const *) {}
386 // virtual int GetTabPos() { return 0; }
388 // virtual int GetTab(int) { return 0; }
390 virtual int GetColumns() { return 1; }
392 virtual int GetRows() { return 1; }
394 virtual bool isMatrix() { return false; }
395 // /// These functions should report an error
396 // virtual char const* GetLabel() { return 0; }
397 // virtual char const* GetLabel(int) { return 0; }
399 // Vertical switching
401 virtual bool setArgumentIdx(int i) { return (i == 0); }
403 virtual bool setNextArgIdx() { return false; }
405 virtual int getArgumentIdx() { return 0; }
407 virtual int getMaxArgumentIdx() { return 0; }
409 // virtual void SetLabel(char const *) {}
411 virtual void SetStyle(short);
413 virtual MathedRowSt * getRowSt() const { return 0; }
415 virtual void setRowSt(MathedRowSt *) {}
417 virtual bool Permit(short f) { return bool(f & flag); }
420 /// Paragraph data is stored here
421 LyxArrayBase * array;
422 /// Cursor start position
429 virtual void setFlag(MathedParFlag f) { flag |= f; }
431 friend class InsetFormula;
433 friend class MathedXIter;
435 friend class MathedCursor;
437 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
438 LyxArrayBase * a = 0,
439 MathParInset ** p = 0);
443 /* The physical structure of a row and aditional information is stored here.
444 It allows to manage the extra info independently of the paragraph data.
445 Only used for multiline paragraphs.
450 w = new int[n + 1]; // this leaks
452 for (int i = 0 ; i < n + 1 ; ++i)
463 /// Should be const but...
464 MathedRowSt * getNext() const { return next; }
465 /// ...we couldn't use this.
466 void setNext(MathedRowSt * n) { next = n; }
468 char const * getLabel() const { return label; }
470 bool isNumbered() const { return numbered; }
472 int getBaseline() const { return y; }
474 int getTab(int i) { return w[i]; }
476 void setLabel(char * l) { label = l; }
478 void setNumbered(bool nf) { numbered = nf; }
480 void setTab(int i, int t) { w[i] = t; }
494 friend class MathMatrixInset;
496 friend class MathedXIter;
500 /** Multiline math paragraph base class.
501 This is the base to all multiline editable math objects
502 like array and eqnarray.
504 class MathMatrixInset: public MathParInset {
507 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
509 MathMatrixInset(MathMatrixInset *);
511 MathedInset * Clone();
513 virtual ~MathMatrixInset();
516 void draw(Painter &, int, int);
522 void Write(ostream &);
524 void Write(string & file);
528 void SetData(LyxArrayBase *);
530 void SetAlign(char, char const *);
532 char * GetAlign(char * vv) {
539 int GetColumns() { return nc; }
541 int GetRows() { return nr; }
543 virtual bool isMatrix() { return true; }
545 /// Use this to manage the extra information independently of paragraph
546 MathedRowSt * getRowSt() const { return row; }
548 void setRowSt(MathedRowSt * r) { row = r; }
551 /// Number of columns & rows
556 char v_align; // add approp. signedness
559 /// Vertical structure
566 /************************* Prototypes **********************************/
568 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
571 void mathed_write(MathParInset *, ostream &, int *, char fragile,
572 char const * label = 0);
575 void mathed_write(MathParInset *, string &, int *, char fragile,
576 char const * label = 0);
578 void mathed_parser_file(istream &, int);
580 int mathed_parser_lineno();
582 int MathedLookupBOP(short);
584 /************************ Inline functions ********************************/
586 inline bool MathIsInset(short x)
588 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
592 inline bool MathIsFont(short x)
594 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
598 inline bool MathIsAlphaFont(short x)
600 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
604 inline bool MathIsActive(short x)
606 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
610 inline bool MathIsUp(short x)
612 return x == LM_TC_UP;
616 inline bool MathIsDown(short x)
618 return x == LM_TC_DOWN;
622 inline bool MathIsScript(short x)
624 return x == LM_TC_DOWN || x == LM_TC_UP;
628 inline bool MathIsBOPS(short x)
630 return MathedLookupBOP(x) > LMB_NONE;
635 inline bool MathIsBinary(short x)
637 return x == LM_TC_BOP || x == LM_TC_BOPS;
641 inline bool MathIsSymbol(short x) {
642 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
647 MathedInset::MathedInset(char const * nm, short ot, short st):
648 name(nm), objtype(ot), size(st)
650 width = ascent = descent = 0;
654 bool MathParInset::Inside(int x, int y)
656 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
661 void MathParInset::GetXY(int & x, int & y) const
668 void MathParInset::UserSetSize(short sz)
672 flag = flag & ~LMPF_FIXED_SIZE;
678 void MathParInset::SetStyle(short sz)
680 if (Permit(LMPF_FIXED_SIZE)) {
681 if (Permit(LMPF_SCRIPT))
682 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
683 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
686 MathedInset::SetStyle(sz);