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,
48 static int const MATH_COLSEP = 8;
50 static int const MATH_ROWSEP = 8;
53 /// Standard Math Sizes (Math mode styles)
66 /** The restrictions of a standard LaTeX math paragraph
67 allows to get a small number of text codes (<30) */
68 enum MathedTextCodes {
71 /// Open and Close group
81 /// Super and sub scripts
85 /// Editable Math Inset
87 /// Editable Text Inset
91 /// Internal code for constants
93 /// Internal code for variables
109 /// Math mode TeX characters ",;:{}"
111 /// Special characters "{}&#_%"
113 /// Internal code for operators
115 /// Internal code for symbols
124 /// This must be < 32
129 std::ostream & operator<<(std::ostream &, MathedTextCodes mtc);
132 #define LM_TC_NORMAL LM_TC_VAR
135 /// Types of lyx-math insets
136 enum MathedInsetTypes {
139 /// A simple paragraph
141 /// A simple numbered paragraph
143 /// A multiline paragraph
145 /// A multiline numbered paragraph
195 enum MathedBinaryTypes {
203 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
208 /** Abstract base class for all math objects.
209 A math insets is for use of the math editor only, it isn't a
210 general LyX inset. It's used to represent all the math objects.
211 The formulaInset (a LyX inset) encapsulates a math inset.
215 /// A math inset has a name (usually its LaTeX name), type and font-size
216 MathedInset(string const & nm, short ot, short st);
219 MathedInset(MathedInset *);
221 virtual ~MathedInset() {}
224 virtual void draw(Painter &, int x, int baseline) = 0;
226 /// Write LaTeX and Lyx code
227 virtual void Write(std::ostream &, bool fragile) = 0;
229 /// Reproduces itself
230 virtual MathedInset * Clone() = 0;
232 /// Compute the size of the object
233 virtual void Metrics() = 0;
235 virtual int Ascent() const { return ascent; }
237 virtual int Descent() const { return descent; }
239 virtual int Width() const { return width; }
241 virtual int Height() const { return ascent + descent; }
244 virtual bool GetLimits() const { return false; }
246 virtual void SetLimits(bool) {}
249 string const & GetName() const { return name; }
251 short GetType() const { return objtype; }
253 short GetStyle() const { return size; }
255 //Man: Avoid to use these functions if it's not strictly necessary
257 virtual void SetType(short t) { objtype = t; }
259 virtual void SetStyle(short st) { size = st; } // Metrics();
261 virtual void SetName(string const & n) { name = n; }
281 /// In a near future maybe we use a better fonts renderer than X
282 void drawStr(Painter &, short, int, int, int, string const &);
284 friend class MathedCursor;
286 friend void mathed_init_fonts();
292 /// Paragraph permissions
295 /// If false can use a non-standard size
297 /// If true can insert newlines
299 /// If true can use tabs
301 /// If true can insert new columns
302 LMPF_ALLOW_NEW_COL = 8,
303 /// Smaller than current size (frac)
305 /// Script size (subscript, stackrel)
310 /** The math paragraph base class, base to all editable math objects */
311 class MathParInset: public MathedInset {
314 MathParInset(short st = LM_ST_TEXT, string const & nm = string(),
315 short ot = LM_OT_MIN);
318 MathParInset(MathParInset *);
320 virtual ~MathParInset();
322 virtual MathedInset * Clone();
324 /// Draw the object on a drawable
325 virtual void draw(Painter &, int x, int baseline);
328 virtual void Write(std::ostream &, bool fragile);
331 virtual void Metrics();
333 virtual void UserSetSize(short);
335 /// Data is stored in a LyXArray
336 virtual void SetData(LyxArrayBase *);
338 virtual LyxArrayBase * GetData() { return array; }
340 /// Paragraph position
341 virtual void GetXY(int &, int &) const;
343 virtual void setXY(int x, int y) { xo = x; yo = y; }
345 virtual void SetFocus(int, int) {}
347 virtual bool Inside(int, int);
349 // Tab stuff used by Matrix.
351 virtual void SetAlign(char, string const &) {}
353 virtual int GetColumns() const { return 1; }
355 virtual int GetRows() const { return 1; }
357 virtual bool isMatrix() const { return false; }
358 // Vertical switching
360 virtual bool setArgumentIdx(int i) { return (i == 0); }
362 virtual bool setNextArgIdx() { return false; }
364 virtual int getArgumentIdx() const { return 0; }
366 virtual int getMaxArgumentIdx() const { return 0; }
368 virtual void SetStyle(short);
370 virtual MathedRowSt * getRowSt() const { return 0; }
372 virtual void setRowSt(MathedRowSt *) {}
374 virtual bool Permit(short f) { return bool(f & flag); }
377 /// Paragraph data is stored here
378 LyxArrayBase * array;
379 /// Cursor start position
388 virtual void setFlag(MathedParFlag f) { flag |= f; }
390 friend class InsetFormula;
392 friend class MathedXIter;
394 friend class MathedCursor;
396 friend LyxArrayBase * mathed_parse(unsigned flags = 0,
397 LyxArrayBase * a = 0,
398 MathParInset ** p = 0);
402 /** The physical structure of a row and aditional information is stored here.
403 It allows to manage the extra info independently of the paragraph data.
404 Only used for multiline paragraphs.
410 w = new int[n + 1]; // this leaks
412 for (int i = 0 ; i < n + 1 ; ++i)
421 /// Should be const but...
422 MathedRowSt * getNext() const { return next; }
423 /// ...we couldn't use this.
424 void setNext(MathedRowSt * n) { next = n; }
426 string const & getLabel() const { return label; }
428 bool isNumbered() const { return numbered; }
430 int getBaseline() const { return y; }
432 void setBaseline(int b) { y = b; }
434 int ascent() const { return asc; }
436 int descent() const { return desc; }
438 void ascent(int a) { asc = a; }
440 void descent(int d) { desc = d; }
442 int getTab(int i) const { return w[i]; }
444 void setLabel(string const & l) { label = l; }
446 void setNumbered(bool nf) { numbered = nf; }
448 void setTab(int i, int t) { w[i] = t; }
464 /** Multiline math paragraph base class.
465 This is the base to all multiline editable math objects
466 like array and eqnarray.
468 class MathMatrixInset: public MathParInset {
472 MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
475 MathMatrixInset(MathMatrixInset *);
477 MathedInset * Clone();
479 virtual ~MathMatrixInset();
481 void draw(Painter &, int, int);
483 void Write(std::ostream &, bool fragile);
487 void SetData(LyxArrayBase *);
489 void SetAlign(char, string const &);
491 int GetColumns() const { return nc; }
493 int GetRows() const { return nr; }
495 virtual bool isMatrix() const { return true; }
497 /// Use this to manage the extra information independently of paragraph
498 MathedRowSt * getRowSt() const { return row; }
500 void setRowSt(MathedRowSt * r) { row = r; }
503 /// Number of columns & rows
510 char v_align; // add approp. type
512 string h_align; // a vector would perhaps be more correct
513 /// Vertical structure
520 /************************* Prototypes **********************************/
522 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
525 void mathed_write(MathParInset *, std::ostream &, int *, bool fragile,
526 string const & label = string());
529 void mathed_parser_file(std::istream &, int);
531 int mathed_parser_lineno();
533 int MathedLookupBOP(short);
535 /************************ Inline functions ********************************/
539 bool MathIsInset(short x)
541 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
546 bool MathIsFont(short x)
548 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
553 bool MathIsAlphaFont(short x)
555 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
560 bool MathIsActive(short x)
562 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
567 bool MathIsUp(short x)
569 return x == LM_TC_UP;
574 bool MathIsDown(short x)
576 return x == LM_TC_DOWN;
581 bool MathIsScript(short x)
583 return x == LM_TC_DOWN || x == LM_TC_UP;
588 bool MathIsBOPS(short x)
590 return MathedLookupBOP(x) > LMB_NONE;
595 bool MathIsBinary(short x)
597 return x == LM_TC_BOP || x == LM_TC_BOPS;
602 bool MathIsSymbol(short x) {
603 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
608 MathedInset::MathedInset(string const & nm, short ot, short st):
609 name(nm), objtype(ot), size(st)
611 width = ascent = descent = 0;
616 bool MathParInset::Inside(int x, int y)
618 return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
623 void MathParInset::GetXY(int & x, int & y) const
630 void MathParInset::UserSetSize(short sz)
634 flag = flag & ~LMPF_FIXED_SIZE;
640 void MathParInset::SetStyle(short sz)
642 if (Permit(LMPF_FIXED_SIZE)) {
643 if (Permit(LMPF_SCRIPT))
644 sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
645 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
648 MathedInset::SetStyle(sz);
653 bool is_eqn_type(short int type)
655 return type >= LM_OT_MIN && type < LM_OT_MATRIX;
660 bool is_matrix_type(short int type)
662 return type == LM_OT_MATRIX;
666 bool is_multiline(short int type)
668 return type >= LM_OT_MPAR && type < LM_OT_MATRIX;
672 inline bool is_ams(short int type)
674 return type > LM_OT_MPARN && type < LM_OT_MATRIX;
678 bool is_singlely_numbered(short int type)
680 return type == LM_OT_PARN || type == LM_OT_MULTLINEN;
684 bool is_multi_numbered(short int type)
686 return type == LM_OT_MPARN || type == LM_OT_ALIGNATN;
690 bool is_numbered(short int type)
692 return is_singlely_numbered(type) || is_multi_numbered(type);
696 bool is_multicolumn(short int type)
698 return type == LM_OT_ALIGNAT || type == LM_OT_ALIGNATN;