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.
41 MATH_ALIGN_BOTTOM = 4,
51 /// Standard Math Sizes (Math mode styles)
64 /// Standard LaTeX Math Environments
65 enum MathedEnvironment {
81 /** The restrictions of a standard LaTeX math paragraph
82 allows to get a small number of text codes (<30) */
83 enum MathedTextCodes {
86 /// Open and Close group
96 /// Super and sub scripts
100 /// Editable Math Inset
102 /// Editable Text Inset
106 /// Internal code for constants
108 /// Internal code for variables
124 /// Math mode TeX characters ",;:{}"
126 /// Special characters "{}&#_%"
128 /// Internal code for operators
130 /// Internal code for symbols
139 /// This must be < 32
143 ostream & operator<<(ostream &, MathedTextCodes mtc);
146 #define LM_TC_NORMAL LM_TC_VAR
149 /// Types of lyx-math insets
150 enum MathedInsetTypes {
153 /// A simple paragraph
155 /// A simple numbered paragraph
157 /// A multiline paragraph
159 /// A multiline numbered paragraph
200 enum MathedBinaryTypes {
208 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
213 /** Abstract base class for all math objects.
214 A math insets is for use of the math editor only, it isn't a
215 general LyX inset. It's used to represent all the math objects.
216 The formulaInset (a LyX inset) encapsulates a math inset.
220 /// A math inset has a name (usually its LaTeX name), type and font-size
221 MathedInset(char const * nm, short ot, short st);
223 MathedInset(MathedInset *);
225 virtual ~MathedInset() {}
228 virtual void Draw(int x, int baseline) = 0;
230 /// Write LaTeX and Lyx code
231 virtual void Write(ostream &) = 0;
233 /// Write LaTeX and Lyx code
234 virtual void Write(string & file) = 0;
236 /// Reproduces itself
237 virtual MathedInset * Clone() = 0;
239 /// Compute the size of the object
240 virtual void Metrics() = 0;
242 virtual int Ascent() const { return ascent; }
244 virtual int Descent() const { return descent; }
246 virtual int Width() const { return width; }
248 virtual int Height() const { return ascent + descent; }
251 virtual bool GetLimits() const { return false; }
253 virtual void SetLimits(bool) {}
256 char const * GetName() const { return name; }
258 short GetType() const { return objtype; }
260 short GetStyle() const { return size; }
262 //Man: Avoid to use these functions if it's not strictly necessary
264 virtual void SetType(short t) { objtype = t; }
266 virtual void SetStyle(short st) { size = st; } // Metrics();
268 virtual void SetName(char const * n) { name = n; }
270 void setDrawable(long unsigned int d) { pm = d; }
285 /// This works while only one process can draw unless
286 /// the process have their own data
287 static long unsigned int pm;
289 static int df_asc, df_des, df_width;
291 /// In a near future maybe we use a better fonts renderer than X
292 void drawStr(short, int, int, int, byte *, int);
294 friend class MathedCursor;
296 friend void mathed_init_fonts();
302 /// Paragraph permissions
305 /// If false can use a non-standard size
307 /// If true can insert newlines
309 /// If true can use tabs
311 /// If true can insert new columns
312 LMPF_ALLOW_NEW_COL = 8,
313 /// Smaller than current size (frac)
315 /// Script size (subscript, stackrel)
320 /** The math paragraph base class, base to all editable math objects */
321 class MathParInset: public MathedInset {
324 MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
325 short ot = LM_OT_MIN);
327 MathParInset(MathParInset *);
329 virtual ~MathParInset();
331 virtual MathedInset * Clone();
333 /// Draw the object on a drawable
334 virtual void Draw(int x, int baseline);
337 virtual void Write(ostream &);
340 virtual void Write(string & file);
342 virtual void Metrics();
344 virtual void UserSetSize(short);
346 /// Data is stored in a LyXArray
347 virtual void SetData(LyxArrayBase *);
349 virtual LyxArrayBase * GetData() { return array; }
351 /// Paragraph position
352 virtual void GetXY(int &, int &) const;
354 virtual void setXY(int x, int y) { xo = x; yo = y; }
356 virtual void SetFocus(int, int) {}
358 virtual bool Inside(int, int);
360 // Tab stuff used by Matrix.
362 virtual void SetAlign(char, char const *) {}
364 // virtual int GetTabPos() { return 0; }
366 // virtual int GetTab(int) { return 0; }
368 virtual int GetColumns() { return 1; }
370 virtual int GetRows() { return 1; }
372 virtual bool isMatrix() { return false; }
373 // /// These functions should report an error
374 // virtual char const* GetLabel() { return 0; }
375 // virtual char const* GetLabel(int) { return 0; }
377 // Vertical switching
379 virtual bool setArgumentIdx(int i) { return (i == 0); }
381 virtual bool setNextArgIdx() { return false; }
383 virtual int getArgumentIdx() { return 0; }
385 virtual int getMaxArgumentIdx() { return 0; }
387 // virtual void SetLabel(char const *) {}
389 virtual void SetStyle(short);
391 virtual MathedRowSt * getRowSt() const { return 0; }
393 virtual void setRowSt(MathedRowSt *) {}
395 virtual bool Permit(short f) { return bool(f & flag); }
398 /// Paragraph data is stored here
399 LyxArrayBase * array;
400 /// Cursor start position
407 virtual void setFlag(MathedParFlag f) { flag |= f; }
409 friend class InsetFormula;
411 friend class MathedXIter;
413 friend class MathedCursor;
415 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
416 LyxArrayBase * a = 0,
417 MathParInset ** p = 0);
421 /* The physical structure of a row and aditional information is stored here.
422 It allows to manage the extra info independently of the paragraph data.
423 Only used for multiline paragraphs.
428 w = new int[n + 1]; // this leaks
436 if (label) delete[] label;
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();
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);
544 //void mathed_write(MathParInset *, FILE *, int *, char fragile,
545 // char const * label = 0);
547 void mathed_write(MathParInset *, string &, int *, char fragile,
548 char const * label = 0);
550 void mathed_parser_file(FILE *, int);
552 int mathed_parser_lineno();
554 int MathedLookupBOP(short);
556 /************************ Inline functions ********************************/
558 inline bool MathIsInset(short x)
560 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
564 inline bool MathIsFont(short x)
566 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
570 inline bool MathIsAlphaFont(short x)
572 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
576 inline bool MathIsActive(short x)
578 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
582 inline bool MathIsUp(short x)
584 return x == LM_TC_UP;
588 inline bool MathIsDown(short x)
590 return x == LM_TC_DOWN;
594 inline bool MathIsScript(short x)
596 return x == LM_TC_DOWN || x == LM_TC_UP;
600 inline bool MathIsBOPS(short x)
602 return MathedLookupBOP(x) > LMB_NONE;
607 inline bool MathIsBinary(short x)
609 return x == LM_TC_BOP || x == LM_TC_BOPS;
613 inline 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);