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.
39 MATH_ALIGN_BOTTOM = 4,
49 /// Standard Math Sizes (Math mode styles)
62 /// Standard LaTeX Math Environments
63 enum MathedEnvironment {
79 /** The restrictions of a standard LaTeX math paragraph
80 allows to get a small number of text codes (<30) */
81 enum MathedTextCodes {
84 /// Open and Close group
94 /// Super and sub scripts
98 /// Editable Math Inset
100 /// Editable Text Inset
104 /// Internal code for constants
106 /// Internal code for variables
122 /// Math mode TeX characters ",;:{}"
124 /// Special characters "{}&#_%"
126 /// Internal code for operators
128 /// Internal code for symbols
137 /// This must be < 32
141 ostream & operator<<(ostream &, MathedTextCodes mtc);
144 #define LM_TC_NORMAL LM_TC_VAR
147 /// Types of lyx-math insets
148 enum MathedInsetTypes {
151 /// A simple paragraph
153 /// A simple numbered paragraph
155 /// A multiline paragraph
157 /// A multiline numbered paragraph
198 enum MathedBinaryTypes {
206 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
211 /** Abstract base class for all math objects.
212 A math insets is for use of the math editor only, it isn't a
213 general LyX inset. It's used to represent all the math objects.
214 The formulaInset (a LyX inset) encapsulates a math inset.
218 /// A math inset has a name (usually its LaTeX name), type and font-size
219 MathedInset(char const * nm, short ot, short st);
221 MathedInset(MathedInset *);
223 virtual ~MathedInset() {}
226 virtual void Draw(int x, int baseline) = 0;
228 /// Write LaTeX and Lyx code
229 virtual void Write(ostream &) = 0;
231 /// Write LaTeX and Lyx code
232 virtual void Write(string & file) = 0;
234 /// Reproduces itself
235 virtual MathedInset * Clone() = 0;
237 /// Compute the size of the object
238 virtual void Metrics() = 0;
240 virtual int Ascent() const { return ascent; }
242 virtual int Descent() const { return descent; }
244 virtual int Width() const { return width; }
246 virtual int Height() const { return ascent + descent; }
249 virtual bool GetLimits() const { return false; }
251 virtual void SetLimits(bool) {}
254 char const * GetName() const { return name; }
256 short GetType() const { return objtype; }
258 short GetStyle() const { return size; }
260 //Man: Avoid to use these functions if it's not strictly necessary
262 virtual void SetType(short t) { objtype = t; }
264 virtual void SetStyle(short st) { size = st; } // Metrics();
266 virtual void SetName(char const * n) { name = n; }
268 void setDrawable(long unsigned int d) { pm = d; }
283 /// This works while only one process can draw unless
284 /// the process have their own data
285 static long unsigned int pm;
287 static int df_asc, df_des, df_width;
289 /// In a near future maybe we use a better fonts renderer than X
290 void drawStr(short, int, int, int, byte *, int);
292 friend class MathedCursor;
294 friend void mathed_init_fonts();
300 /// Paragraph permissions
303 /// If false can use a non-standard size
305 /// If true can insert newlines
307 /// If true can use tabs
309 /// If true can insert new columns
310 LMPF_ALLOW_NEW_COL = 8,
311 /// Smaller than current size (frac)
313 /// Script size (subscript, stackrel)
318 /** The math paragraph base class, base to all editable math objects */
319 class MathParInset: public MathedInset {
322 MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
323 short ot = LM_OT_MIN);
325 MathParInset(MathParInset *);
327 virtual ~MathParInset();
329 virtual MathedInset * Clone();
331 /// Draw the object on a drawable
332 virtual void Draw(int x, int baseline);
335 virtual void Write(ostream &);
338 virtual void Write(string & file);
340 virtual void Metrics();
342 virtual void UserSetSize(short);
344 /// Data is stored in a LyXArray
345 virtual void SetData(LyxArrayBase *);
347 virtual LyxArrayBase * GetData() { return array; }
349 /// Paragraph position
350 virtual void GetXY(int &, int &) const;
352 virtual void setXY(int x, int y) { xo = x; yo = y; }
354 virtual void SetFocus(int, int) {}
356 virtual bool Inside(int, int);
358 // Tab stuff used by Matrix.
360 virtual void SetAlign(char, char const *) {}
362 // virtual int GetTabPos() { return 0; }
364 // virtual int GetTab(int) { return 0; }
366 virtual int GetColumns() { return 1; }
368 virtual int GetRows() { return 1; }
370 virtual bool isMatrix() { return false; }
371 // /// These functions should report an error
372 // virtual char const* GetLabel() { return 0; }
373 // virtual char const* GetLabel(int) { return 0; }
375 // Vertical switching
377 virtual bool setArgumentIdx(int i) { return (i == 0); }
379 virtual bool setNextArgIdx() { return false; }
381 virtual int getArgumentIdx() { return 0; }
383 virtual int getMaxArgumentIdx() { return 0; }
385 // virtual void SetLabel(char const *) {}
387 virtual void SetStyle(short);
389 virtual MathedRowSt * getRowSt() const { return 0; }
391 virtual void setRowSt(MathedRowSt *) {}
393 virtual bool Permit(short f) { return bool(f & flag); }
396 /// Paragraph data is stored here
397 LyxArrayBase * array;
398 /// Cursor start position
405 virtual void setFlag(MathedParFlag f) { flag |= f; }
407 friend class InsetFormula;
409 friend class MathedXIter;
411 friend class MathedCursor;
413 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
414 LyxArrayBase * a = 0,
415 MathParInset ** p = 0);
419 /* The physical structure of a row and aditional information is stored here.
420 It allows to manage the extra info independently of the paragraph data.
421 Only used for multiline paragraphs.
426 w = new int[n + 1]; // this leaks
428 for (int i = 0 ; i < n + 1 ; ++i)
437 if (label) delete[] label;
439 /// Should be const but...
440 MathedRowSt * getNext() const { return next; }
441 /// ...we couldn't use this.
442 void setNext(MathedRowSt * n) { next = n; }
444 char const * getLabel() const { return label; }
446 bool isNumbered() const { return numbered; }
448 int getBaseline() const { return y; }
450 int getTab(int i) { return w[i]; }
452 void setLabel(char * l) { label = l; }
454 void setNumbered(bool nf) { numbered = nf; }
456 void setTab(int i, int t) { w[i] = t; }
470 friend class MathMatrixInset;
472 friend class MathedXIter;
476 /** Multiline math paragraph base class.
477 This is the base to all multiline editable math objects
478 like array and eqnarray.
480 class MathMatrixInset: public MathParInset {
483 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
485 MathMatrixInset(MathMatrixInset *);
487 MathedInset * Clone();
489 virtual ~MathMatrixInset();
493 void Write(ostream &);
495 void Write(string & file);
499 void SetData(LyxArrayBase *);
501 void SetAlign(char, char const *);
503 char * GetAlign(char * vv) {
510 int GetColumns() { return nc; }
512 int GetRows() { return nr; }
514 virtual bool isMatrix() { return true; }
516 /// Use this to manage the extra information independently of paragraph
517 MathedRowSt * getRowSt() const { return row; }
519 void setRowSt(MathedRowSt * r) { row = r; }
522 /// Number of columns & rows
527 char v_align; // add approp. signedness
530 /// Vertical structure
537 /************************* Prototypes **********************************/
539 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
542 void mathed_write(MathParInset *, ostream &, int *, char fragile,
543 char const * label = 0);
546 void mathed_write(MathParInset *, string &, int *, char fragile,
547 char const * label = 0);
549 void mathed_parser_file(istream &, int);
551 int mathed_parser_lineno();
553 int MathedLookupBOP(short);
555 /************************ Inline functions ********************************/
557 inline bool MathIsInset(short x)
559 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
563 inline bool MathIsFont(short x)
565 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
569 inline bool MathIsAlphaFont(short x)
571 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
575 inline bool MathIsActive(short x)
577 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
581 inline bool MathIsUp(short x)
583 return x == LM_TC_UP;
587 inline bool MathIsDown(short x)
589 return x == LM_TC_DOWN;
593 inline bool MathIsScript(short x)
595 return x == LM_TC_DOWN || x == LM_TC_UP;
599 inline bool MathIsBOPS(short x)
601 return MathedLookupBOP(x) > LMB_NONE;
606 inline bool MathIsBinary(short x)
608 return x == LM_TC_BOP || x == LM_TC_BOPS;
612 inline bool MathIsSymbol(short x) {
613 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
618 MathedInset::MathedInset(char const * nm, short ot, short st):
619 name(nm), objtype(ot), size(st)
621 width = ascent = descent = 0;
625 bool MathParInset::Inside(int x, int y)
627 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
632 void MathParInset::GetXY(int & x, int & y) const
639 void MathParInset::UserSetSize(short sz)
643 flag = flag & ~LMPF_FIXED_SIZE;
649 void MathParInset::SetStyle(short sz)
651 if (Permit(LMPF_FIXED_SIZE)) {
652 if (Permit(LMPF_SCRIPT))
653 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
654 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
657 MathedInset::SetStyle(sz);