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