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.
40 MATH_ALIGN_BOTTOM = 4,
50 /// Standard Math Sizes (Math mode styles)
63 /// Standard LaTeX Math Environments
64 enum MathedEnvironment {
80 /** The restrictions of a standard LaTeX math paragraph
81 allows to get a small number of text codes (<30) */
82 enum MathedTextCodes {
85 /// Open and Close group
95 /// Super and sub scripts
99 /// Editable Math Inset
101 /// Editable Text Inset
105 /// Internal code for constants
107 /// Internal code for variables
123 /// Math mode TeX characters ",;:{}"
125 /// Special characters "{}&#_%"
127 /// Internal code for operators
129 /// Internal code for symbols
138 /// This must be < 32
142 ostream & operator<<(ostream &, MathedTextCodes mtc);
145 #define LM_TC_NORMAL LM_TC_VAR
148 /// Types of lyx-math insets
149 enum MathedInsetTypes {
152 /// A simple paragraph
154 /// A simple numbered paragraph
156 /// A multiline paragraph
158 /// A multiline numbered paragraph
199 enum MathedBinaryTypes {
207 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(FILE * file) = 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(FILE * file);
339 virtual void Write(string & file);
341 virtual void Metrics();
343 virtual void UserSetSize(short);
345 /// Data is stored in a LyXArray
346 virtual void SetData(LyxArrayBase *);
348 virtual LyxArrayBase * GetData() { return array; }
350 /// Paragraph position
351 virtual void GetXY(int &, int &) const;
353 virtual void setXY(int x, int y) { xo = x; yo = y; }
355 virtual void SetFocus(int, int) {}
357 virtual bool Inside(int, int);
359 // Tab stuff used by Matrix.
361 virtual void SetAlign(char, char const *) {}
363 // virtual int GetTabPos() { return 0; }
365 // virtual int GetTab(int) { return 0; }
367 virtual int GetColumns() { return 1; }
369 virtual int GetRows() { return 1; }
371 virtual bool isMatrix() { return false; }
372 // /// These functions should report an error
373 // virtual char const* GetLabel() { return 0; }
374 // virtual char const* GetLabel(int) { return 0; }
376 // Vertical switching
378 virtual bool setArgumentIdx(int i) { return (i == 0); }
380 virtual bool setNextArgIdx() { return false; }
382 virtual int getArgumentIdx() { return 0; }
384 virtual int getMaxArgumentIdx() { return 0; }
386 // virtual void SetLabel(char const *) {}
388 virtual void SetStyle(short);
390 virtual MathedRowSt * getRowSt() const { return 0; }
392 virtual void setRowSt(MathedRowSt *) {}
394 virtual bool Permit(short f) { return bool(f & flag); }
397 /// Paragraph data is stored here
398 LyxArrayBase * array;
399 /// Cursor start position
406 virtual void setFlag(MathedParFlag f) { flag |= f; }
408 friend class InsetFormula;
410 friend class MathedXIter;
412 friend class MathedCursor;
414 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
415 LyxArrayBase * a = 0,
416 MathParInset ** p = 0);
420 /* The physical structure of a row and aditional information is stored here.
421 It allows to manage the extra info independently of the paragraph data.
422 Only used for multiline paragraphs.
427 w = new int[n + 1]; // this leaks
435 if (label) delete[] label;
437 /// Should be const but...
438 MathedRowSt * getNext() const { return next; }
439 /// ...we couldn't use this.
440 void setNext(MathedRowSt * n) { next = n; }
442 char const * getLabel() const { return label; }
444 bool isNumbered() const { return numbered; }
446 int getBaseline() const { return y; }
448 int getTab(int i) { return w[i]; }
450 void setLabel(char * l) { label = l; }
452 void setNumbered(bool nf) { numbered = nf; }
454 void setTab(int i, int t) { w[i] = t; }
468 friend class MathMatrixInset;
470 friend class MathedXIter;
474 /** Multiline math paragraph base class.
475 This is the base to all multiline editable math objects
476 like array and eqnarray.
478 class MathMatrixInset: public MathParInset {
481 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
483 MathMatrixInset(MathMatrixInset *);
485 MathedInset * Clone();
487 virtual ~MathMatrixInset();
491 void Write(FILE * file);
493 void Write(string & file);
497 void SetData(LyxArrayBase *);
499 void SetAlign(char, char const *);
501 char * GetAlign(char * vv) {
508 int GetColumns() { return nc; }
510 int GetRows() { return nr; }
512 virtual bool isMatrix() { return true; }
514 /// Use this to manage the extra information independently of paragraph
515 MathedRowSt * getRowSt() const { return row; }
517 void setRowSt(MathedRowSt * r) { row = r; }
520 /// Number of columns & rows
525 char v_align; // add approp. signedness
528 /// Vertical structure
535 /************************* Prototypes **********************************/
537 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
540 void mathed_write(MathParInset *, FILE *, int *, char fragile,
541 char const * label = 0);
543 void mathed_write(MathParInset *, string &, int *, char fragile,
544 char const * label = 0);
546 void mathed_parser_file(FILE *, int);
548 int mathed_parser_lineno();
550 int MathedLookupBOP(short);
552 /************************ Inline functions ********************************/
554 inline bool MathIsInset(short x)
556 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
560 inline bool MathIsFont(short x)
562 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
566 inline bool MathIsAlphaFont(short x)
568 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
572 inline bool MathIsActive(short x)
574 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
578 inline bool MathIsUp(short x)
580 return x == LM_TC_UP;
584 inline bool MathIsDown(short x)
586 return x == LM_TC_DOWN;
590 inline bool MathIsScript(short x)
592 return x == LM_TC_DOWN || x == LM_TC_UP;
596 inline bool MathIsBOPS(short x)
598 return MathedLookupBOP(x) > LMB_NONE;
603 inline bool MathIsBinary(short x)
605 return x == LM_TC_BOP || x == LM_TC_BOPS;
609 inline bool MathIsSymbol(short x) {
610 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
615 MathedInset::MathedInset(char const * nm, short ot, short st):
616 name(nm), objtype(ot), size(st)
618 width = ascent = descent = 0;
622 bool MathParInset::Inside(int x, int y)
624 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
629 void MathParInset::GetXY(int & x, int & y) const
636 void MathParInset::UserSetSize(short sz)
640 flag = flag & ~LMPF_FIXED_SIZE;
646 void MathParInset::SetStyle(short sz)
648 if (Permit(LMPF_FIXED_SIZE)) {
649 if (Permit(LMPF_SCRIPT))
650 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
651 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
654 MathedInset::SetStyle(sz);