]> git.lyx.org Git - lyx.git/blob - src/mathed/math_defs.h
All changes I made to have text-insets working. Look at 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: (c) 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 "support/LIstream.h"
28
29 #include "LString.h"
30 #include "debug.h"
31
32 #include "array.h"
33
34 class Painter;
35
36
37 ///
38 enum math_align {
39         ///
40         MATH_ALIGN_LEFT = 1,
41         ///
42         MATH_ALIGN_RIGHT = 2,
43         ///
44         MATH_ALIGN_BOTTOM = 4,
45         ///
46         MATH_ALIGN_TOP = 8
47 };
48 ///
49 #define MATH_COLSEP 8
50 ///
51 #define MATH_ROWSEP 8
52
53
54 /// Standard Math Sizes (Math mode styles)
55 enum MathedStyles {
56         ///
57         LM_ST_DISPLAY = 0,
58         ///
59         LM_ST_TEXT,
60         ///
61         LM_ST_SCRIPT,
62         ///
63         LM_ST_SCRIPTSCRIPT
64 };
65
66
67 /// Standard LaTeX Math Environments
68 enum MathedEnvironment {
69         ///
70         LM_EN_INTEXT = 0,
71         ///
72         LM_EN_DISPLAY,
73         ///
74         LM_EN_EQUATION,
75         ///
76         LM_EN_EQNARRAYAST,
77         ///
78         LM_EN_EQNARRAY,
79         ///
80         LM_EN_ARRAY
81 };
82
83
84 /** The restrictions of a standard LaTeX math paragraph
85   allows to get a small number of text codes (<30) */
86 enum MathedTextCodes  {
87         /// This must be >= 0
88         LM_TC_MIN = 0,
89         /// Open and Close group
90         LM_TC_OPEN,
91         ///
92         LM_TC_CLOSE,
93         /// Tabulator
94         LM_TC_TAB,
95         /// New line
96         LM_TC_CR,
97         /// Math Inset
98         LM_TC_INSET,
99         /// Super and sub scripts
100         LM_TC_UP,
101         ///
102         LM_TC_DOWN,
103         /// Editable Math Inset
104         LM_TC_ACTIVE_INSET,
105         /// Editable Text Inset
106         LM_TC_TEXT_INSET,
107         ///
108         LM_FONT_BEGIN,
109         /// Internal code for constants
110         LM_TC_CONST,
111         /// Internal code for variables
112         LM_TC_VAR,
113         ///
114         LM_TC_RM,
115         ///
116         LM_TC_CAL,
117         ///
118         LM_TC_BF,
119         ///
120         LM_TC_SF,
121         ///
122         LM_TC_TT,
123         ///
124         LM_TC_IT,
125         ///
126         LM_TC_TEXTRM,
127         /// Math mode TeX characters ",;:{}"
128         LM_TC_TEX,
129         /// Special characters "{}&#_%"
130         LM_TC_SPECIAL,
131         /// Internal code for operators
132         LM_TC_BOP,
133         /// Internal code for symbols
134         LM_TC_SYMB,
135         ///
136         LM_TC_BOPS,
137         ///
138         LM_TC_BSYM,
139         ///
140         LM_FONT_END,
141         
142         /// This must be < 32 
143         LM_TC_MAX
144 };
145
146 ostream & operator<<(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     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(ostream &) = 0;
235
236     /// Write LaTeX and Lyx code
237     virtual void Write(string & file) = 0;
238    
239     /// Reproduces itself
240     virtual MathedInset * Clone() = 0;
241    
242     /// Compute the size of the object
243     virtual void Metrics() = 0; 
244     /// 
245     virtual int Ascent() const { return ascent; }
246     ///
247     virtual int Descent() const { return descent; }
248     ///
249     virtual int Width() const { return width; }
250     ///
251     virtual int Height() const { return ascent + descent; }
252     
253     ///
254     virtual bool GetLimits() const { return false; }
255     ///
256     virtual void SetLimits(bool) {}   
257    
258     ///
259     char const * GetName() const { return name; }
260     ///
261     short GetType() const { return objtype; }
262     ///
263     short GetStyle() const { return size; }
264           
265     //Man:  Avoid to use these functions if it's not strictly necessary 
266     ///
267     virtual void  SetType(short t) { objtype = t; }
268     ///
269     virtual void  SetStyle(short st) { size = st; } // Metrics();
270     ///
271     virtual void  SetName(char const * n) { name = n; }
272  protected:
273     ///
274     char const * name;
275     ///
276     short objtype;
277     ///
278     int width;
279     ///
280     int ascent;
281     ///
282     int descent;
283     ///
284     short size;
285     /// Default metrics
286     static int df_asc, df_des, df_width;
287
288     /// In a near future maybe we use a better fonts renderer than X
289     void drawStr(Painter &, short, int, int, int, byte *, 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     MathParInset(MathParInset *);
325     ///
326     virtual ~MathParInset();
327     ///
328     virtual MathedInset * Clone();
329
330     /// Draw the object on a drawable
331     virtual void draw(Painter &, int x, int baseline);
332
333     /// Write LaTeX code
334     virtual void Write(ostream &);
335
336     /// Write LaTeX code
337     virtual void Write(string & file);
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, yo;
399     /// 
400     short flag;
401
402  private:
403     ///
404     virtual void setFlag(MathedParFlag f) { flag |= f; }
405         ///
406     friend class InsetFormula;
407         ///
408     friend class MathedXIter;
409         ///
410     friend class MathedCursor;
411         ///
412     friend LyxArrayBase * mathed_parse(unsigned flags = 0,
413                                        LyxArrayBase * a = 0,
414                                        MathParInset ** p = 0);
415 };
416
417
418 /* The physical structure of a row and aditional information is stored here.
419     It allows to manage the extra info independently of the paragraph data.  
420     Only used for multiline paragraphs.
421  */
422 struct MathedRowSt {    
423     /// 
424     MathedRowSt(int n) {
425             w = new int[n + 1]; // this leaks
426             asc = desc = y = 0;
427             for (int i = 0 ; i < n + 1 ; ++i)
428               w[i] = 0;
429             next = 0;
430             label = 0;
431             numbered = true;
432     }
433     ///
434     ~MathedRowSt() {
435             delete[] w;
436             delete[] label;
437     }
438     /// Should be const but...
439     MathedRowSt * getNext() const  { return next; }
440     /// ...we couldn't use this.
441     void setNext(MathedRowSt * n) { next = n; }
442     ///
443     char const * getLabel() const { return label; }
444     ///
445     bool isNumbered() const { return numbered; }
446     ///
447     int  getBaseline() const { return y; }    
448     ///
449     int  getTab(int i) { return w[i]; }
450     /// 
451     void setLabel(char * l) { label = l; }
452     ///
453     void setNumbered(bool nf) { numbered = nf; }
454     ///
455     void setTab(int i, int t) { w[i] = t; }
456     
457  private:
458     /// Vericals 
459     int asc, desc, y;
460     /// widths 
461     int * w;
462     /// 
463     char * label;
464     ///
465     bool numbered;
466     ///
467     MathedRowSt * next;
468         ///
469     friend class MathMatrixInset;
470         ///
471     friend class MathedXIter;
472 };
473
474
475 /** Multiline math paragraph base class.
476     This is the base to all multiline editable math objects
477     like array and eqnarray. 
478  */
479 class MathMatrixInset: public MathParInset {
480  public: 
481     ///
482     MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
483     ///
484     MathMatrixInset(MathMatrixInset *);
485     ///
486     MathedInset * Clone();
487     ///
488     virtual ~MathMatrixInset();
489     ///
490     void draw(Painter &, int, int);
491     ///
492     void Write(ostream &);
493     ///
494     void Write(string & file);
495     ///
496     void Metrics();
497     ///
498     void SetData(LyxArrayBase *);
499     ///
500     void SetAlign(char, char const *);
501     ///
502     char * GetAlign(char * vv) {
503         *vv = v_align;
504         return h_align;
505     }
506 //    ///
507 //    int GetTab(int);
508     ///
509     int GetColumns() { return nc; }
510     ///
511     int GetRows() { return nr; }
512     ///
513     virtual bool isMatrix() { return true; }
514
515     /// Use this to manage the extra information independently of paragraph
516     MathedRowSt * getRowSt() const { return row; }
517         ///
518     void setRowSt(MathedRowSt * r) { row = r; }
519     
520  protected:
521     ///  Number of columns & rows
522     int nc, nr;
523     /// tab sizes
524     int * ws;   
525     /// 
526     char v_align; // add approp. signedness
527     ///
528     char * h_align;
529     /// Vertical structure
530     MathedRowSt * row;
531
532 };
533
534
535
536 /*************************  Prototypes  **********************************/
537 /// 
538 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
539                             MathParInset ** mt);
540 ///
541 void mathed_write(MathParInset *, ostream &, int *, char fragile,
542                   char const * label = 0);
543
544 ///
545 void mathed_write(MathParInset *, string &, int *, char fragile,
546                   char const * label = 0);
547 ///
548 void mathed_parser_file(istream &, int);
549 ///
550 int mathed_parser_lineno();
551 ///
552 int MathedLookupBOP(short);
553
554 /************************ Inline functions ********************************/
555 ///
556 inline bool MathIsInset(short x)
557 {
558         return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
559 }
560
561 ///
562 inline bool MathIsFont(short x)
563 {
564         return LM_TC_CONST <= x && x <= LM_TC_BSYM;
565 }
566
567 ///
568 inline bool MathIsAlphaFont(short x)
569 {
570         return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
571 }
572
573 ///
574 inline bool MathIsActive(short x)
575 {
576         return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
577 }
578
579 ///
580 inline bool MathIsUp(short x)
581 {
582         return x == LM_TC_UP;
583 }
584
585 ///
586 inline bool MathIsDown(short x)
587 {
588         return x == LM_TC_DOWN;
589 }
590
591 ///
592 inline bool MathIsScript(short x)
593 {
594         return x == LM_TC_DOWN || x == LM_TC_UP;
595 }
596
597 ///
598 inline bool MathIsBOPS(short x)
599 {
600         return MathedLookupBOP(x) > LMB_NONE;
601 }
602
603
604 ///
605 inline bool MathIsBinary(short x)
606 {
607     return x == LM_TC_BOP || x == LM_TC_BOPS;
608 }
609
610 ///
611 inline bool MathIsSymbol(short x) {
612     return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
613 }
614      
615
616 inline 
617 MathedInset::MathedInset(char const * nm, short ot, short st):
618   name(nm), objtype(ot), size(st) 
619 {
620    width = ascent = descent = 0;
621 }
622
623 inline
624 bool MathParInset::Inside(int x, int y) 
625 {
626   return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
627 }
628
629
630 inline
631 void MathParInset::GetXY(int & x, int & y) const
632 {
633    x = xo; y = yo;
634 }
635
636
637 inline
638 void MathParInset::UserSetSize(short sz)
639 {
640    if (sz >= 0) {
641        size = sz;      
642        flag = flag & ~LMPF_FIXED_SIZE;
643    }
644 }
645
646
647 inline
648 void MathParInset::SetStyle(short sz) 
649 {
650     if (Permit(LMPF_FIXED_SIZE)) {
651         if (Permit(LMPF_SCRIPT)) 
652           sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
653         if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
654             ++sz;
655         } 
656         MathedInset::SetStyle(sz);
657     }
658 }
659
660 #endif