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