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