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: 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.
43 MATH_ALIGN_BOTTOM = 4,
53 /// Standard Math Sizes (Math mode styles)
66 /// Standard LaTeX Math Environments
67 enum MathedEnvironment {
83 /** The restrictions of a standard LaTeX math paragraph
84 allows to get a small number of text codes (<30) */
85 enum MathedTextCodes {
88 /// Open and Close group
98 /// Super and sub scripts
102 /// Editable Math Inset
104 /// Editable Text Inset
108 /// Internal code for constants
110 /// Internal code for variables
126 /// Math mode TeX characters ",;:{}"
128 /// Special characters "{}&#_%"
130 /// Internal code for operators
132 /// Internal code for symbols
141 /// This must be < 32
145 std::ostream & operator<<(std::ostream &, MathedTextCodes mtc);
148 #define LM_TC_NORMAL LM_TC_VAR
151 /// Types of lyx-math insets
152 enum MathedInsetTypes {
155 /// A simple paragraph
157 /// A simple numbered paragraph
159 /// A multiline paragraph
161 /// A multiline numbered paragraph
202 enum MathedBinaryTypes {
210 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
215 /** Abstract base class for all math objects.
216 A math insets is for use of the math editor only, it isn't a
217 general LyX inset. It's used to represent all the math objects.
218 The formulaInset (a LyX inset) encapsulates a math inset.
222 /// A math inset has a name (usually its LaTeX name), type and font-size
223 MathedInset(char const * nm, short ot, short st);
226 MathedInset(MathedInset *);
228 virtual ~MathedInset() {}
231 virtual void draw(Painter &, int x, int baseline) = 0;
233 /// Write LaTeX and Lyx code
234 virtual void Write(std::ostream &, bool fragile) = 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; }
288 /// In a near future maybe we use a better fonts renderer than X
289 void drawStr(Painter &, short, int, int, int, byte const *, int);
291 friend class MathedCursor;
293 friend void mathed_init_fonts();
299 /// Paragraph permissions
302 /// If false can use a non-standard size
304 /// If true can insert newlines
306 /// If true can use tabs
308 /// If true can insert new columns
309 LMPF_ALLOW_NEW_COL = 8,
310 /// Smaller than current size (frac)
312 /// Script size (subscript, stackrel)
317 /** The math paragraph base class, base to all editable math objects */
318 class MathParInset: public MathedInset {
321 MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
322 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(Painter &, int x, int baseline);
335 virtual void Write(std::ostream &, bool fragile);
338 virtual void Metrics();
340 virtual void UserSetSize(short);
342 /// Data is stored in a LyXArray
343 virtual void SetData(LyxArrayBase *);
345 virtual LyxArrayBase * GetData() { return array; }
347 /// Paragraph position
348 virtual void GetXY(int &, int &) const;
350 virtual void setXY(int x, int y) { xo = x; yo = y; }
352 virtual void SetFocus(int, int) {}
354 virtual bool Inside(int, int);
356 // Tab stuff used by Matrix.
358 virtual void SetAlign(char, char const *) {}
360 // virtual int GetTabPos() { return 0; }
362 // virtual int GetTab(int) { return 0; }
364 virtual int GetColumns() { return 1; }
366 virtual int GetRows() { return 1; }
368 virtual bool isMatrix() { return false; }
369 // /// These functions should report an error
370 // virtual char const* GetLabel() { return 0; }
371 // virtual char const* GetLabel(int) { return 0; }
373 // Vertical switching
375 virtual bool setArgumentIdx(int i) { return (i == 0); }
377 virtual bool setNextArgIdx() { return false; }
379 virtual int getArgumentIdx() { return 0; }
381 virtual int getMaxArgumentIdx() { return 0; }
383 // virtual void SetLabel(char const *) {}
385 virtual void SetStyle(short);
387 virtual MathedRowSt * getRowSt() const { return 0; }
389 virtual void setRowSt(MathedRowSt *) {}
391 virtual bool Permit(short f) { return bool(f & flag); }
394 /// Paragraph data is stored here
395 LyxArrayBase * array;
396 /// 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.
427 w = new int[n + 1]; // this leaks
429 for (int i = 0 ; i < n + 1 ; ++i)
440 /// Should be const but...
441 MathedRowSt * getNext() const { return next; }
442 /// ...we couldn't use this.
443 void setNext(MathedRowSt * n) { next = n; }
445 char const * getLabel() const { return label; }
447 bool isNumbered() const { return numbered; }
449 int getBaseline() const { return y; }
451 int getTab(int i) { return w[i]; }
453 void setLabel(char * l) { label = l; }
455 void setNumbered(bool nf) { numbered = nf; }
457 void setTab(int i, int t) { w[i] = t; }
471 friend class MathMatrixInset;
473 friend class MathedXIter;
477 /** Multiline math paragraph base class.
478 This is the base to all multiline editable math objects
479 like array and eqnarray.
481 class MathMatrixInset: public MathParInset {
485 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
488 MathMatrixInset(MathMatrixInset *);
490 MathedInset * Clone();
492 virtual ~MathMatrixInset();
494 void draw(Painter &, int, int);
496 void Write(std::ostream &, bool fragile);
500 void SetData(LyxArrayBase *);
502 void SetAlign(char, char const *);
504 char * GetAlign(char * vv) {
511 int GetColumns() { return nc; }
513 int GetRows() { return nr; }
515 virtual bool isMatrix() { return true; }
517 /// Use this to manage the extra information independently of paragraph
518 MathedRowSt * getRowSt() const { return row; }
520 void setRowSt(MathedRowSt * r) { row = r; }
523 /// 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 *, std::ostream &, int *, bool fragile,
546 char const * label = 0);
549 void mathed_parser_file(std::istream &, int);
551 int mathed_parser_lineno();
553 int MathedLookupBOP(short);
555 /************************ Inline functions ********************************/
558 bool MathIsInset(short x)
560 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
565 bool MathIsFont(short x)
567 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
572 bool MathIsAlphaFont(short x)
574 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
579 bool MathIsActive(short x)
581 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
586 bool MathIsUp(short x)
588 return x == LM_TC_UP;
593 bool MathIsDown(short x)
595 return x == LM_TC_DOWN;
600 bool MathIsScript(short x)
602 return x == LM_TC_DOWN || x == LM_TC_UP;
607 bool MathIsBOPS(short x)
609 return MathedLookupBOP(x) > LMB_NONE;
615 bool MathIsBinary(short x)
617 return x == LM_TC_BOP || x == LM_TC_BOPS;
622 bool MathIsSymbol(short x) {
623 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
628 MathedInset::MathedInset(char const * nm, short ot, short st):
629 name(nm), objtype(ot), size(st)
631 width = ascent = descent = 0;
635 bool MathParInset::Inside(int x, int y)
637 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
642 void MathParInset::GetXY(int & x, int & y) const
649 void MathParInset::UserSetSize(short sz)
653 flag = flag & ~LMPF_FIXED_SIZE;
659 void MathParInset::SetStyle(short sz)
661 if (Permit(LMPF_FIXED_SIZE)) {
662 if (Permit(LMPF_SCRIPT))
663 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
664 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
667 MathedInset::SetStyle(sz);