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"
42 MATH_ALIGN_BOTTOM = 4,
52 /// Standard Math Sizes (Math mode styles)
65 /// Standard LaTeX Math Environments
66 enum MathedEnvironment {
82 /** The restrictions of a standard LaTeX math paragraph
83 allows to get a small number of text codes (<30) */
84 enum MathedTextCodes {
87 /// Open and Close group
97 /// Super and sub scripts
101 /// Editable Math Inset
103 /// Editable Text Inset
107 /// Internal code for constants
109 /// Internal code for variables
125 /// Math mode TeX characters ",;:{}"
127 /// Special characters "{}&#_%"
129 /// Internal code for operators
131 /// Internal code for symbols
140 /// This must be < 32
144 ostream & operator<<(ostream &, MathedTextCodes mtc);
147 #define LM_TC_NORMAL LM_TC_VAR
150 /// Types of lyx-math insets
151 enum MathedInsetTypes {
154 /// A simple paragraph
156 /// A simple numbered paragraph
158 /// A multiline paragraph
160 /// A multiline numbered paragraph
201 enum MathedBinaryTypes {
209 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
214 /** Abstract base class for all math objects.
215 A math insets is for use of the math editor only, it isn't a
216 general LyX inset. It's used to represent all the math objects.
217 The formulaInset (a LyX inset) encapsulates a math inset.
221 /// A math inset has a name (usually its LaTeX name), type and font-size
222 MathedInset(char const * nm, short ot, short st);
224 MathedInset(MathedInset *);
226 virtual ~MathedInset() {}
229 virtual void Draw(int x, int baseline) = 0;
231 /// Write LaTeX and Lyx code
232 virtual void Write(ostream &) = 0;
234 /// Write LaTeX and Lyx code
235 virtual void Write(string & file) = 0;
237 /// Reproduces itself
238 virtual MathedInset * Clone() = 0;
240 /// Compute the size of the object
241 virtual void Metrics() = 0;
243 virtual int Ascent() const { return ascent; }
245 virtual int Descent() const { return descent; }
247 virtual int Width() const { return width; }
249 virtual int Height() const { return ascent + descent; }
252 virtual bool GetLimits() const { return false; }
254 virtual void SetLimits(bool) {}
257 char const * GetName() const { return name; }
259 short GetType() const { return objtype; }
261 short GetStyle() const { return size; }
263 //Man: Avoid to use these functions if it's not strictly necessary
265 virtual void SetType(short t) { objtype = t; }
267 virtual void SetStyle(short st) { size = st; } // Metrics();
269 virtual void SetName(char const * n) { name = n; }
271 void setDrawable(long unsigned int d) { pm = d; }
286 /// This works while only one process can draw unless
287 /// the process have their own data
288 static long unsigned int pm;
290 static int df_asc, df_des, df_width;
292 /// In a near future maybe we use a better fonts renderer than X
293 void drawStr(short, int, int, int, byte *, int);
295 friend class MathedCursor;
297 friend void mathed_init_fonts();
303 /// Paragraph permissions
306 /// If false can use a non-standard size
308 /// If true can insert newlines
310 /// If true can use tabs
312 /// If true can insert new columns
313 LMPF_ALLOW_NEW_COL = 8,
314 /// Smaller than current size (frac)
316 /// Script size (subscript, stackrel)
321 /** The math paragraph base class, base to all editable math objects */
322 class MathParInset: public MathedInset {
325 MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
326 short ot = LM_OT_MIN);
328 MathParInset(MathParInset *);
330 virtual ~MathParInset();
332 virtual MathedInset * Clone();
334 /// Draw the object on a drawable
335 virtual void Draw(int x, int baseline);
338 virtual void Write(ostream &);
341 virtual void Write(string & file);
343 virtual void Metrics();
345 virtual void UserSetSize(short);
347 /// Data is stored in a LyXArray
348 virtual void SetData(LyxArrayBase *);
350 virtual LyxArrayBase * GetData() { return array; }
352 /// Paragraph position
353 virtual void GetXY(int &, int &) const;
355 virtual void setXY(int x, int y) { xo = x; yo = y; }
357 virtual void SetFocus(int, int) {}
359 virtual bool Inside(int, int);
361 // Tab stuff used by Matrix.
363 virtual void SetAlign(char, char const *) {}
365 // virtual int GetTabPos() { return 0; }
367 // virtual int GetTab(int) { return 0; }
369 virtual int GetColumns() { return 1; }
371 virtual int GetRows() { return 1; }
373 virtual bool isMatrix() { return false; }
374 // /// These functions should report an error
375 // virtual char const* GetLabel() { return 0; }
376 // virtual char const* GetLabel(int) { return 0; }
378 // Vertical switching
380 virtual bool setArgumentIdx(int i) { return (i == 0); }
382 virtual bool setNextArgIdx() { return false; }
384 virtual int getArgumentIdx() { return 0; }
386 virtual int getMaxArgumentIdx() { return 0; }
388 // virtual void SetLabel(char const *) {}
390 virtual void SetStyle(short);
392 virtual MathedRowSt * getRowSt() const { return 0; }
394 virtual void setRowSt(MathedRowSt *) {}
396 virtual bool Permit(short f) { return bool(f & flag); }
399 /// Paragraph data is stored here
400 LyxArrayBase * array;
401 /// Cursor start position
408 virtual void setFlag(MathedParFlag f) { flag |= f; }
410 friend class InsetFormula;
412 friend class MathedXIter;
414 friend class MathedCursor;
416 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
417 LyxArrayBase * a = 0,
418 MathParInset ** p = 0);
422 /* The physical structure of a row and aditional information is stored here.
423 It allows to manage the extra info independently of the paragraph data.
424 Only used for multiline paragraphs.
429 w = new int[n + 1]; // this leaks
431 for (int i = 0 ; i < n + 1 ; ++i)
440 if (label) delete[] label;
442 /// Should be const but...
443 MathedRowSt * getNext() const { return next; }
444 /// ...we couldn't use this.
445 void setNext(MathedRowSt * n) { next = n; }
447 char const * getLabel() const { return label; }
449 bool isNumbered() const { return numbered; }
451 int getBaseline() const { return y; }
453 int getTab(int i) { return w[i]; }
455 void setLabel(char * l) { label = l; }
457 void setNumbered(bool nf) { numbered = nf; }
459 void setTab(int i, int t) { w[i] = t; }
473 friend class MathMatrixInset;
475 friend class MathedXIter;
479 /** Multiline math paragraph base class.
480 This is the base to all multiline editable math objects
481 like array and eqnarray.
483 class MathMatrixInset: public MathParInset {
486 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
488 MathMatrixInset(MathMatrixInset *);
490 MathedInset * Clone();
492 virtual ~MathMatrixInset();
496 void Write(ostream &);
498 void Write(string & file);
502 void SetData(LyxArrayBase *);
504 void SetAlign(char, char const *);
506 char * GetAlign(char * vv) {
513 int GetColumns() { return nc; }
515 int GetRows() { return nr; }
517 virtual bool isMatrix() { return true; }
519 /// Use this to manage the extra information independently of paragraph
520 MathedRowSt * getRowSt() const { return row; }
522 void setRowSt(MathedRowSt * r) { row = r; }
525 /// Number of columns & rows
530 char v_align; // add approp. signedness
533 /// Vertical structure
540 /************************* Prototypes **********************************/
542 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
545 void mathed_write(MathParInset *, ostream &, int *, char fragile,
546 char const * label = 0);
549 void mathed_write(MathParInset *, string &, int *, char fragile,
550 char const * label = 0);
552 void mathed_parser_file(istream &, int);
554 int mathed_parser_lineno();
556 int MathedLookupBOP(short);
558 /************************ Inline functions ********************************/
560 inline bool MathIsInset(short x)
562 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
566 inline bool MathIsFont(short x)
568 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
572 inline bool MathIsAlphaFont(short x)
574 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
578 inline bool MathIsActive(short x)
580 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
584 inline bool MathIsUp(short x)
586 return x == LM_TC_UP;
590 inline bool MathIsDown(short x)
592 return x == LM_TC_DOWN;
596 inline bool MathIsScript(short x)
598 return x == LM_TC_DOWN || x == LM_TC_UP;
602 inline bool MathIsBOPS(short x)
604 return MathedLookupBOP(x) > LMB_NONE;
609 inline bool MathIsBinary(short x)
611 return x == LM_TC_BOP || x == LM_TC_BOPS;
615 inline bool MathIsSymbol(short x) {
616 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
621 MathedInset::MathedInset(char const * nm, short ot, short st):
622 name(nm), objtype(ot), size(st)
624 width = ascent = descent = 0;
628 bool MathParInset::Inside(int x, int y)
630 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
635 void MathParInset::GetXY(int & x, int & y) const
642 void MathParInset::UserSetSize(short sz)
646 flag = flag & ~LMPF_FIXED_SIZE;
652 void MathParInset::SetStyle(short sz)
654 if (Permit(LMPF_FIXED_SIZE)) {
655 if (Permit(LMPF_SCRIPT))
656 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
657 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
660 MathedInset::SetStyle(sz);