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