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