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.
27 #include "support/LIstream.h"
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 ostream & operator<<(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);
225 MathedInset(MathedInset *);
227 virtual ~MathedInset() {}
230 virtual void draw(Painter &, int x, int baseline) = 0;
232 /// Write LaTeX and Lyx code
233 virtual void Write(ostream &) = 0;
235 /// Reproduces itself
236 virtual MathedInset * Clone() = 0;
238 /// Compute the size of the object
239 virtual void Metrics() = 0;
241 virtual int Ascent() const { return ascent; }
243 virtual int Descent() const { return descent; }
245 virtual int Width() const { return width; }
247 virtual int Height() const { return ascent + descent; }
250 virtual bool GetLimits() const { return false; }
252 virtual void SetLimits(bool) {}
255 char const * GetName() const { return name; }
257 short GetType() const { return objtype; }
259 short GetStyle() const { return size; }
261 //Man: Avoid to use these functions if it's not strictly necessary
263 virtual void SetType(short t) { objtype = t; }
265 virtual void SetStyle(short st) { size = st; } // Metrics();
267 virtual void SetName(char const * n) { name = n; }
282 static int df_asc, df_des, df_width;
284 /// In a near future maybe we use a better fonts renderer than X
285 void drawStr(Painter &, short, int, int, int, byte const *, int);
287 friend class MathedCursor;
289 friend void mathed_init_fonts();
295 /// Paragraph permissions
298 /// If false can use a non-standard size
300 /// If true can insert newlines
302 /// If true can use tabs
304 /// If true can insert new columns
305 LMPF_ALLOW_NEW_COL = 8,
306 /// Smaller than current size (frac)
308 /// Script size (subscript, stackrel)
313 /** The math paragraph base class, base to all editable math objects */
314 class MathParInset: public MathedInset {
317 MathParInset(short st = LM_ST_TEXT, char const * nm = 0,
318 short ot = LM_OT_MIN);
320 MathParInset(MathParInset *);
322 virtual ~MathParInset();
324 virtual MathedInset * Clone();
326 /// Draw the object on a drawable
327 virtual void draw(Painter &, int x, int baseline);
330 virtual void Write(ostream &);
333 virtual void Metrics();
335 virtual void UserSetSize(short);
337 /// Data is stored in a LyXArray
338 virtual void SetData(LyxArrayBase *);
340 virtual LyxArrayBase * GetData() { return array; }
342 /// Paragraph position
343 virtual void GetXY(int &, int &) const;
345 virtual void setXY(int x, int y) { xo = x; yo = y; }
347 virtual void SetFocus(int, int) {}
349 virtual bool Inside(int, int);
351 // Tab stuff used by Matrix.
353 virtual void SetAlign(char, char const *) {}
355 // virtual int GetTabPos() { return 0; }
357 // virtual int GetTab(int) { return 0; }
359 virtual int GetColumns() { return 1; }
361 virtual int GetRows() { return 1; }
363 virtual bool isMatrix() { return false; }
364 // /// These functions should report an error
365 // virtual char const* GetLabel() { return 0; }
366 // virtual char const* GetLabel(int) { return 0; }
368 // Vertical switching
370 virtual bool setArgumentIdx(int i) { return (i == 0); }
372 virtual bool setNextArgIdx() { return false; }
374 virtual int getArgumentIdx() { return 0; }
376 virtual int getMaxArgumentIdx() { return 0; }
378 // virtual void SetLabel(char const *) {}
380 virtual void SetStyle(short);
382 virtual MathedRowSt * getRowSt() const { return 0; }
384 virtual void setRowSt(MathedRowSt *) {}
386 virtual bool Permit(short f) { return bool(f & flag); }
389 /// Paragraph data is stored here
390 LyxArrayBase * array;
391 /// Cursor start position
398 virtual void setFlag(MathedParFlag f) { flag |= f; }
400 friend class InsetFormula;
402 friend class MathedXIter;
404 friend class MathedCursor;
406 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
407 LyxArrayBase * a = 0,
408 MathParInset ** p = 0);
412 /* The physical structure of a row and aditional information is stored here.
413 It allows to manage the extra info independently of the paragraph data.
414 Only used for multiline paragraphs.
419 w = new int[n + 1]; // this leaks
421 for (int i = 0 ; i < n + 1 ; ++i)
432 /// Should be const but...
433 MathedRowSt * getNext() const { return next; }
434 /// ...we couldn't use this.
435 void setNext(MathedRowSt * n) { next = n; }
437 char const * getLabel() const { return label; }
439 bool isNumbered() const { return numbered; }
441 int getBaseline() const { return y; }
443 int getTab(int i) { return w[i]; }
445 void setLabel(char * l) { label = l; }
447 void setNumbered(bool nf) { numbered = nf; }
449 void setTab(int i, int t) { w[i] = t; }
463 friend class MathMatrixInset;
465 friend class MathedXIter;
469 /** Multiline math paragraph base class.
470 This is the base to all multiline editable math objects
471 like array and eqnarray.
473 class MathMatrixInset: public MathParInset {
476 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
478 MathMatrixInset(MathMatrixInset *);
480 MathedInset * Clone();
482 virtual ~MathMatrixInset();
484 void draw(Painter &, int, int);
486 void Write(ostream &);
490 void SetData(LyxArrayBase *);
492 void SetAlign(char, char const *);
494 char * GetAlign(char * vv) {
501 int GetColumns() { return nc; }
503 int GetRows() { return nr; }
505 virtual bool isMatrix() { return true; }
507 /// Use this to manage the extra information independently of paragraph
508 MathedRowSt * getRowSt() const { return row; }
510 void setRowSt(MathedRowSt * r) { row = r; }
513 /// Number of columns & rows
518 char v_align; // add approp. signedness
521 /// Vertical structure
528 /************************* Prototypes **********************************/
530 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
533 void mathed_write(MathParInset *, ostream &, int *, char fragile,
534 char const * label = 0);
537 void mathed_parser_file(istream &, int);
539 int mathed_parser_lineno();
541 int MathedLookupBOP(short);
543 /************************ Inline functions ********************************/
546 bool MathIsInset(short x)
548 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
553 bool MathIsFont(short x)
555 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
560 bool MathIsAlphaFont(short x)
562 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
567 bool MathIsActive(short x)
569 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
574 bool MathIsUp(short x)
576 return x == LM_TC_UP;
581 bool MathIsDown(short x)
583 return x == LM_TC_DOWN;
588 bool MathIsScript(short x)
590 return x == LM_TC_DOWN || x == LM_TC_UP;
595 bool MathIsBOPS(short x)
597 return MathedLookupBOP(x) > LMB_NONE;
603 bool MathIsBinary(short x)
605 return x == LM_TC_BOP || x == LM_TC_BOPS;
610 bool MathIsSymbol(short x) {
611 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
616 MathedInset::MathedInset(char const * nm, short ot, short st):
617 name(nm), objtype(ot), size(st)
619 width = ascent = descent = 0;
623 bool MathParInset::Inside(int x, int y)
625 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
630 void MathParInset::GetXY(int & x, int & y) const
637 void MathParInset::UserSetSize(short sz)
641 flag = flag & ~LMPF_FIXED_SIZE;
647 void MathParInset::SetStyle(short sz)
649 if (Permit(LMPF_FIXED_SIZE)) {
650 if (Permit(LMPF_SCRIPT))
651 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
652 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
655 MathedInset::SetStyle(sz);