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
199 enum MathedBinaryTypes {
207 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
212 /** Abstract base class for all math objects.
213 A math insets is for use of the math editor only, it isn't a
214 general LyX inset. It's used to represent all the math objects.
215 The formulaInset (a LyX inset) encapsulates a math inset.
219 /// A math inset has a name (usually its LaTeX name), type and font-size
220 MathedInset(string const & nm, short ot, short st);
223 MathedInset(MathedInset *);
225 virtual ~MathedInset() {}
228 virtual void draw(Painter &, int x, int baseline) = 0;
230 /// Write LaTeX and Lyx code
231 virtual void Write(std::ostream &, bool fragile) = 0;
233 /// Reproduces itself
234 virtual MathedInset * Clone() = 0;
236 /// Compute the size of the object
237 virtual void Metrics() = 0;
239 virtual int Ascent() const { return ascent; }
241 virtual int Descent() const { return descent; }
243 virtual int Width() const { return width; }
245 virtual int Height() const { return ascent + descent; }
248 virtual bool GetLimits() const { return false; }
250 virtual void SetLimits(bool) {}
253 string const & GetName() const { return name; }
255 short GetType() const { return objtype; }
257 short GetStyle() const { return size; }
259 //Man: Avoid to use these functions if it's not strictly necessary
261 virtual void SetType(short t) { objtype = t; }
263 virtual void SetStyle(short st) { size = st; } // Metrics();
265 virtual void SetName(string const & n) { name = n; }
267 static int workWidth;
287 /// In a near future maybe we use a better fonts renderer than X
288 void drawStr(Painter &, short, int, int, int, string const &);
290 friend class MathedCursor;
292 friend void mathed_init_fonts();
298 /// Paragraph permissions
301 /// If false can use a non-standard size
303 /// If true can insert newlines
305 /// If true can use tabs
307 /// If true can insert new columns
308 LMPF_ALLOW_NEW_COL = 8,
309 /// Smaller than current size (frac)
311 /// Script size (subscript, stackrel)
316 /** The math paragraph base class, base to all editable math objects */
317 class MathParInset: public MathedInset {
320 MathParInset(short st = LM_ST_TEXT, string const & nm = string(),
321 short ot = LM_OT_MIN);
324 MathParInset(MathParInset *);
326 virtual ~MathParInset();
328 virtual MathedInset * Clone();
330 /// Draw the object on a drawable
331 virtual void draw(Painter &, int x, int baseline);
334 virtual void Write(std::ostream &, bool fragile);
337 virtual void Metrics();
339 virtual void UserSetSize(short);
341 /// Data is stored in a LyXArray
342 virtual void SetData(MathedArray *);
344 virtual MathedArray * GetData() { return array; }
346 /// Paragraph position
347 virtual void GetXY(int &, int &) const;
349 virtual void setXY(int x, int y) { xo = x; yo = y; }
351 virtual void SetFocus(int, int) {}
353 virtual bool Inside(int, int);
355 // Tab stuff used by Matrix.
357 virtual void SetAlign(char, string const &) {}
359 virtual int GetColumns() const { return 1; }
361 virtual int GetRows() const { return 1; }
363 virtual bool isMatrix() const { return false; }
364 // Vertical switching
366 virtual bool setArgumentIdx(int i) { return (i == 0); }
368 virtual bool setNextArgIdx() { return false; }
370 virtual int getArgumentIdx() const { return 0; }
372 virtual int getMaxArgumentIdx() const { return 0; }
374 virtual void SetStyle(short);
376 virtual MathedRowSt * getRowSt() const { return 0; }
378 virtual void setRowSt(MathedRowSt *) {}
380 virtual bool Permit(short f) { return bool(f & flag); }
383 /// Paragraph data is stored here
385 /// Cursor start position
394 virtual void setFlag(MathedParFlag f) { flag |= f; }
396 friend class InsetFormula;
398 friend class MathedXIter;
400 friend class MathedCursor;
402 friend MathedArray * mathed_parse(unsigned flags = 0,
404 MathParInset ** p = 0);
408 /** The physical structure of a row and aditional information is stored here.
409 It allows to manage the extra info independently of the paragraph data.
410 Only used for multiline paragraphs.
417 : asc_(0), desc_(0), y_(0), widths_(n + 1, 0),
418 numbered_(true), next_(0)
420 /// Should be const but...
421 MathedRowSt * getNext() const { return next_; }
422 /// ...we couldn't use this.
423 void setNext(MathedRowSt * n) { next_ = n; }
425 string const & getLabel() const { return label_; }
427 bool isNumbered() const { return numbered_; }
429 int getBaseline() const { return y_; }
431 void setBaseline(int b) { y_ = b; }
433 int ascent() const { return asc_; }
435 int descent() const { return desc_; }
437 void ascent(int a) { asc_ = a; }
439 void descent(int d) { desc_ = d; }
441 int getTab(int i) const { return widths_[i]; }
443 void setLabel(string const & l) { label_ = l; }
445 void setNumbered(bool nf) { numbered_ = nf; }
447 void setTab(int i, int t) { widths_[i] = t; }
454 std::vector<int> widths_;
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(MathedArray *);
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 MathedArray * mathed_parse(unsigned flags, MathedArray * 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_ALIGNN
687 || type == LM_OT_ALIGNATN;
691 bool is_numbered(short int type)
693 return is_singlely_numbered(type) || is_multi_numbered(type);
697 bool is_multicolumn(short int type)
699 return type == LM_OT_ALIGN || type == LM_OT_ALIGNN
700 || type == LM_OT_ALIGNAT || type == LM_OT_ALIGNATN;