]> git.lyx.org Git - lyx.git/blob - src/mathed/math_defs.h
the remove reinterpret_cast patch from Andre Poenitz
[lyx.git] / src / mathed / math_defs.h
1 // -*- C++ -*-
2 /*
3  *  File:        math_defs.h
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.
8  *
9  *  Dependencies: Xlib
10  *
11  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
12  *
13  *   Version: 0.8beta, Mathed & Lyx project.
14  *
15  *   You are free to use and modify this code under the terms of
16  *   the GNU General Public Licence version 2 or later.
17  * 
18  */
19
20 #ifndef MATH_DEFS
21 #define MATH_DEFS
22
23 #ifdef __GNUG__
24 #pragma interface
25 #endif
26
27 #include <iosfwd>
28
29 #include "LString.h"
30 #include "debug.h"
31
32 #include "array.h"
33
34 class Painter;
35
36 ///
37 enum math_align {
38         ///
39         MATH_ALIGN_LEFT = 1,
40         ///
41         MATH_ALIGN_RIGHT = 2,
42         ///
43         MATH_ALIGN_BOTTOM = 4,
44         ///
45         MATH_ALIGN_TOP = 8
46 };
47 ///
48 static int const MATH_COLSEP = 8;
49 ///
50 static int const MATH_ROWSEP = 8;
51
52
53 /// Standard Math Sizes (Math mode styles)
54 enum MathedStyles {
55         ///
56         LM_ST_DISPLAY = 0,
57         ///
58         LM_ST_TEXT,
59         ///
60         LM_ST_SCRIPT,
61         ///
62         LM_ST_SCRIPTSCRIPT
63 };
64
65
66 /** The restrictions of a standard LaTeX math paragraph
67   allows to get a small number of text codes (<30) */
68 enum MathedTextCodes  {
69         /// This must be >= 0
70         LM_TC_MIN = 0,
71         /// Open and Close group
72         LM_TC_OPEN,
73         ///
74         LM_TC_CLOSE,
75         /// Tabulator
76         LM_TC_TAB,
77         /// New line
78         LM_TC_CR,
79         /// Math Inset
80         LM_TC_INSET,
81         /// Super and sub scripts
82         LM_TC_UP,
83         ///
84         LM_TC_DOWN,
85         /// Editable Math Inset
86         LM_TC_ACTIVE_INSET,
87         /// Editable Text Inset
88         LM_TC_TEXT_INSET,
89         ///
90         LM_FONT_BEGIN,
91         /// Internal code for constants
92         LM_TC_CONST,
93         /// Internal code for variables
94         LM_TC_VAR,
95         ///
96         LM_TC_RM,
97         ///
98         LM_TC_CAL,
99         ///
100         LM_TC_BF,
101         ///
102         LM_TC_SF,
103         ///
104         LM_TC_TT,
105         ///
106         LM_TC_IT,
107         ///
108         LM_TC_TEXTRM,
109         /// Math mode TeX characters ",;:{}"
110         LM_TC_TEX,
111         /// Special characters "{}&#_%"
112         LM_TC_SPECIAL,
113         /// Internal code for operators
114         LM_TC_BOP,
115         /// Internal code for symbols
116         LM_TC_SYMB,
117         ///
118         LM_TC_BOPS,
119         ///
120         LM_TC_BSYM,
121         ///
122         LM_FONT_END,
123         
124         /// This must be < 32 
125         LM_TC_MAX
126 };
127
128 ///
129 std::ostream & operator<<(std::ostream &, MathedTextCodes mtc);
130
131 ///
132 #define LM_TC_NORMAL LM_TC_VAR
133  
134        
135 /// Types of lyx-math insets 
136 enum MathedInsetTypes  {
137         ///
138         LM_OT_MIN = 0,
139         /// A simple paragraph
140         LM_OT_PAR,
141         /// A simple numbered paragraph
142         LM_OT_PARN,
143         /// A multiline paragraph
144         LM_OT_MPAR,
145         /// A multiline numbered paragraph
146         LM_OT_MPARN,
147         ///
148         LM_OT_ALIGNAT,
149         ///
150         LM_OT_ALIGNATN,
151         ///
152         LM_OT_MULTLINE,
153         ///
154         LM_OT_MULTLINEN,
155         /// An array
156         LM_OT_MATRIX,
157
158         /// A big operator
159         LM_OT_BIGOP,
160         /// A LaTeX macro
161         LM_OT_UNDEF,
162         ///
163         LM_OT_FUNC,
164         ///
165         LM_OT_FUNCLIM,
166         ///
167         LM_OT_SCRIPT,
168         ///
169         LM_OT_SPACE,
170         ///
171         LM_OT_DOTS,
172         /// A fraction
173         LM_OT_FRAC,
174         ///
175         LM_OT_ATOP,
176         ///
177         LM_OT_STACKREL,
178         /// A radical
179         LM_OT_SQRT,
180         /// A delimiter
181         LM_OT_DELIM,
182         /// A decoration
183         LM_OT_DECO,
184         /// An accent
185         LM_OT_ACCENT,
186         ///
187         LM_OT_MACRO,
188         ///
189         LM_OT_MACRO_ARG,
190         ///
191         LM_OT_MAX
192 };
193
194 ///
195 enum MathedBinaryTypes {
196         ///
197         LMB_NONE = 0,
198         ///
199         LMB_RELATION,
200         ///
201         LMB_OPERATOR,
202         ///
203         LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
204 };
205
206 class MathParInset;
207
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.
212  */
213 class MathedInset  {
214  public: 
215     /// A math inset has a name (usually its LaTeX name), type and font-size
216     MathedInset(string const & nm, short ot, short st);
217     ///
218     explicit
219     MathedInset(MathedInset *);
220     ///
221     virtual ~MathedInset() {}
222
223     /// Draw the object
224     virtual void draw(Painter &, int x, int baseline) = 0;      
225
226     /// Write LaTeX and Lyx code
227     virtual void Write(std::ostream &, bool fragile) = 0;
228
229     /// Reproduces itself
230     virtual MathedInset * Clone() = 0;
231    
232     /// Compute the size of the object
233     virtual void Metrics() = 0; 
234     /// 
235     virtual int Ascent() const { return ascent; }
236     ///
237     virtual int Descent() const { return descent; }
238     ///
239     virtual int Width() const { return width; }
240     ///
241     virtual int Height() const { return ascent + descent; }
242     
243     ///
244     virtual bool GetLimits() const { return false; }
245     ///
246     virtual void SetLimits(bool) {}   
247    
248     ///
249     string const & GetName() const { return name; }
250     ///
251     short GetType() const { return objtype; }
252     ///
253     short GetStyle() const { return size; }
254           
255     //Man:  Avoid to use these functions if it's not strictly necessary 
256     ///
257     virtual void  SetType(short t) { objtype = t; }
258     ///
259     virtual void  SetStyle(short st) { size = st; } // Metrics();
260     ///
261     virtual void  SetName(string const & n) { name = n; }
262  protected:
263     ///
264     string name;
265     ///
266     short objtype;
267     ///
268     int width;
269     ///
270     int ascent;
271     ///
272     int descent;
273     ///
274     short size;
275     /// Default metrics
276     static int df_asc;
277     ///
278     static int df_des;
279     ///
280     static int df_width;
281     /// In a near future maybe we use a better fonts renderer than X
282     void drawStr(Painter &, short, int, int, int, string const &);
283     ///
284     friend class MathedCursor;
285     ///
286     friend void mathed_init_fonts();
287 };
288
289 struct MathedRowSt;
290
291
292 /// Paragraph permissions
293 enum MathedParFlag {
294         LMPF_BASIC = 0,
295         /// If false can use a non-standard size
296         LMPF_FIXED_SIZE = 1,
297         /// If true can insert newlines 
298         LMPF_ALLOW_CR  = 2,
299         /// If true can use tabs
300         LMPF_ALLOW_TAB = 4,
301         /// If true can insert new columns
302         LMPF_ALLOW_NEW_COL = 8,
303         /// Smaller than current size (frac)
304         LMPF_SMALLER = 16,
305         /// Script size (subscript, stackrel)
306         LMPF_SCRIPT = 32
307 };
308
309
310 /** The math paragraph base class, base to all editable math objects */
311 class MathParInset: public MathedInset  {
312  public: 
313     ///
314     MathParInset(short st = LM_ST_TEXT, string const & nm = string(),
315                  short ot = LM_OT_MIN);
316     ///
317     explicit
318     MathParInset(MathParInset *);
319     ///
320     virtual ~MathParInset();
321     ///
322     virtual MathedInset * Clone();
323
324     /// Draw the object on a drawable
325     virtual void draw(Painter &, int x, int baseline);
326
327     /// Write LaTeX code
328     virtual void Write(std::ostream &, bool fragile);
329
330     ///
331     virtual void Metrics();
332     ///
333     virtual void UserSetSize(short);
334  
335     /// Data is stored in a LyXArray
336     virtual void SetData(LyxArrayBase *);
337     ///
338     virtual LyxArrayBase * GetData() { return array; }
339
340     /// Paragraph position
341     virtual void GetXY(int &, int &) const;
342     ///
343     virtual void setXY(int x, int y) { xo = x;  yo = y; }
344     ///
345     virtual void SetFocus(int, int) {}
346     ///
347     virtual bool Inside(int, int);   
348    
349     // Tab stuff used by Matrix.
350     ///
351     virtual void SetAlign(char, string const &) {}
352     ///
353     virtual int GetColumns() const { return 1; }
354     ///
355     virtual int GetRows() const { return 1; }
356     ///
357     virtual bool isMatrix() const { return false; }
358     // Vertical switching
359     ///
360     virtual bool setArgumentIdx(int i) { return (i == 0); }
361     ///
362     virtual bool setNextArgIdx() { return false; }
363     ///
364     virtual int getArgumentIdx() const { return 0; }
365     ///
366     virtual int getMaxArgumentIdx() const { return 0; }
367     ///
368     virtual void SetStyle(short);
369     ///
370     virtual MathedRowSt * getRowSt() const { return 0; }
371     ///
372     virtual void setRowSt(MathedRowSt *) {}
373     ///
374     virtual bool Permit(short f) { return bool(f & flag); }
375     
376  protected:
377     /// Paragraph data is stored here
378     LyxArrayBase * array;
379     /// Cursor start position
380     int xo;
381     ///
382     int yo;
383     /// 
384     short flag;
385
386  private:
387     ///
388     virtual void setFlag(MathedParFlag f) { flag |= f; }
389     ///
390     friend class InsetFormula;
391     ///
392     friend class MathedXIter;
393     ///
394     friend class MathedCursor;
395     ///
396     friend LyxArrayBase * mathed_parse(unsigned flags = 0,
397                                        LyxArrayBase * a = 0,
398                                        MathParInset ** p = 0);
399 };
400
401
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.
405  */
406 struct MathedRowSt {    
407     ///
408     explicit
409     MathedRowSt(int n) {
410             w = new int[n + 1]; // this leaks
411             asc = desc = y = 0;
412             for (int i = 0 ; i < n + 1 ; ++i)
413               w[i] = 0;
414             next = 0;
415             numbered = true;
416     }
417     ///
418     ~MathedRowSt() {
419             delete[] w;
420     }
421     /// Should be const but...
422     MathedRowSt * getNext() const  { return next; }
423     /// ...we couldn't use this.
424     void setNext(MathedRowSt * n) { next = n; }
425     ///
426     string const & getLabel() const { return label; }
427     ///
428     bool isNumbered() const { return numbered; }
429     ///
430     int  getBaseline() const { return y; }
431     ///
432     void setBaseline(int b) { y = b; }
433     ///
434     int ascent() const { return asc; }
435     ///
436     int descent() const { return desc; }
437     ///
438     void ascent(int a) { asc = a; }
439     ///
440     void descent(int d) { desc = d; }
441     ///
442     int  getTab(int i) const { return w[i]; }
443     /// 
444     void setLabel(string const & l) { label = l; }
445     ///
446     void setNumbered(bool nf) { numbered = nf; }
447     ///
448     void setTab(int i, int t) { w[i] = t; }
449     
450  private:
451     /// Vericals 
452     int asc, desc, y;
453     /// widths 
454     int * w;
455     /// 
456     string label;
457     ///
458     bool numbered;
459     ///
460     MathedRowSt * next;
461 };
462
463
464 /** Multiline math paragraph base class.
465     This is the base to all multiline editable math objects
466     like array and eqnarray. 
467  */
468 class MathMatrixInset: public MathParInset {
469  public: 
470     ///
471     explicit
472     MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
473     ///
474     explicit
475     MathMatrixInset(MathMatrixInset *);
476     ///
477     MathedInset * Clone();
478     ///
479     virtual ~MathMatrixInset();
480     ///
481     void draw(Painter &, int, int);
482     ///
483     void Write(std::ostream &, bool fragile);
484     ///
485     void Metrics();
486     ///
487     void SetData(LyxArrayBase *);
488     ///
489     void SetAlign(char, string const &);
490     ///
491     int GetColumns() const { return nc; }
492     ///
493     int GetRows() const { return nr; }
494     ///
495     virtual bool isMatrix() const { return true; }
496
497     /// Use this to manage the extra information independently of paragraph
498     MathedRowSt * getRowSt() const { return row; }
499     ///
500     void setRowSt(MathedRowSt * r) { row = r; }
501     
502  protected:
503     ///  Number of columns & rows
504     int nc;
505     ///
506     int nr;
507     /// tab sizes
508     int * ws;   
509     /// 
510     char v_align; // add approp. type
511     ///
512     string h_align; // a vector would perhaps be more correct
513     /// Vertical structure
514     MathedRowSt * row;
515
516 };
517
518
519
520 /*************************  Prototypes  **********************************/
521 /// 
522 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
523                             MathParInset ** mt);
524 ///
525 void mathed_write(MathParInset *, std::ostream &, int *, bool fragile,
526                   string const & label = string());
527
528 ///
529 void mathed_parser_file(std::istream &, int);
530 ///
531 int mathed_parser_lineno();
532 ///
533 int MathedLookupBOP(short);
534
535 /************************ Inline functions ********************************/
536
537 ///
538 inline
539 bool MathIsInset(short x)
540 {
541         return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
542 }
543
544 ///
545 inline
546 bool MathIsFont(short x)
547 {
548         return LM_TC_CONST <= x && x <= LM_TC_BSYM;
549 }
550
551 ///
552 inline
553 bool MathIsAlphaFont(short x)
554 {
555         return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
556 }
557
558 ///
559 inline
560 bool MathIsActive(short x)
561 {
562         return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
563 }
564
565 ///
566 inline
567 bool MathIsUp(short x)
568 {
569         return x == LM_TC_UP;
570 }
571
572 ///
573 inline
574 bool MathIsDown(short x)
575 {
576         return x == LM_TC_DOWN;
577 }
578
579 ///
580 inline
581 bool MathIsScript(short x)
582 {
583         return x == LM_TC_DOWN || x == LM_TC_UP;
584 }
585
586 ///
587 inline
588 bool MathIsBOPS(short x)
589 {
590         return MathedLookupBOP(x) > LMB_NONE;
591 }
592
593 ///
594 inline
595 bool MathIsBinary(short x)
596 {
597     return x == LM_TC_BOP || x == LM_TC_BOPS;
598 }
599
600 ///
601 inline
602 bool MathIsSymbol(short x) {
603     return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
604 }
605      
606
607 inline
608 MathedInset::MathedInset(string const & nm, short ot, short st):
609   name(nm), objtype(ot), size(st) 
610 {
611    width = ascent = descent = 0;
612 }
613
614
615 inline
616 bool MathParInset::Inside(int x, int y) 
617 {
618   return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
619 }
620
621
622 inline
623 void MathParInset::GetXY(int & x, int & y) const
624 {
625    x = xo; y = yo;
626 }
627
628
629 inline
630 void MathParInset::UserSetSize(short sz)
631 {
632    if (sz >= 0) {
633        size = sz;      
634        flag = flag & ~LMPF_FIXED_SIZE;
635    }
636 }
637
638
639 inline
640 void MathParInset::SetStyle(short sz) 
641 {
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) {
646             ++sz;
647         } 
648         MathedInset::SetStyle(sz);
649     }
650 }
651
652 inline
653 bool is_eqn_type(short int type)
654 {
655         return type >= LM_OT_MIN && type < LM_OT_MATRIX;
656 }
657
658
659 inline
660 bool is_matrix_type(short int type)
661 {
662         return type == LM_OT_MATRIX;
663 }
664
665 inline
666 bool is_multiline(short int type)
667 {
668         return type >= LM_OT_MPAR && type < LM_OT_MATRIX;
669 }
670
671
672 inline bool is_ams(short int type)
673 {
674         return type > LM_OT_MPARN && type < LM_OT_MATRIX;
675 }
676
677 inline
678 bool is_singlely_numbered(short int type)
679 {
680         return type == LM_OT_PARN || type == LM_OT_MULTLINEN;
681 }
682
683 inline
684 bool is_multi_numbered(short int type)
685 {
686         return type == LM_OT_MPARN || type == LM_OT_ALIGNATN;
687 }
688
689 inline
690 bool is_numbered(short int type)
691 {
692         return is_singlely_numbered(type) || is_multi_numbered(type);
693 }
694
695 inline
696 bool is_multicolumn(short int type)
697 {
698         return type == LM_OT_ALIGNAT || type == LM_OT_ALIGNATN;
699 }
700
701 #endif