]> git.lyx.org Git - lyx.git/blob - src/mathed/math_defs.h
mathed13.diff
[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     ///
267     static int workWidth;
268  protected:
269     ///
270     string name;
271     ///
272     short objtype;
273     ///
274     int width;
275     ///
276     int ascent;
277     ///
278     int descent;
279     ///
280     short size;
281     /// Default metrics
282     static int df_asc;
283     ///
284     static int df_des;
285     ///
286     static int df_width;
287     /// In a near future maybe we use a better fonts renderer than X
288     void drawStr(Painter &, short, int, int, int, string const &);
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, string const & nm = string(),
321                  short ot = LM_OT_MIN);
322     ///
323     explicit
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(std::ostream &, bool fragile);
335
336     ///
337     virtual void Metrics();
338     ///
339     virtual void UserSetSize(short);
340  
341     /// Data is stored in a LyXArray
342     virtual void SetData(MathedArray *);
343     ///
344     virtual MathedArray * GetData() { return array; }
345
346     /// Paragraph position
347     virtual void GetXY(int &, int &) const;
348     ///
349     virtual void setXY(int x, int y) { xo = x;  yo = y; }
350     ///
351     virtual void SetFocus(int, int) {}
352     ///
353     virtual bool Inside(int, int);   
354    
355     // Tab stuff used by Matrix.
356     ///
357     virtual void SetAlign(char, string const &) {}
358     ///
359     virtual int GetColumns() const { return 1; }
360     ///
361     virtual int GetRows() const { return 1; }
362     ///
363     virtual bool isMatrix() const { return false; }
364     // Vertical switching
365     ///
366     virtual bool setArgumentIdx(int i) { return (i == 0); }
367     ///
368     virtual bool setNextArgIdx() { return false; }
369     ///
370     virtual int getArgumentIdx() const { return 0; }
371     ///
372     virtual int getMaxArgumentIdx() const { return 0; }
373     ///
374     virtual void SetStyle(short);
375     ///
376     virtual MathedRowSt * getRowSt() const { return 0; }
377     ///
378     virtual void setRowSt(MathedRowSt *) {}
379     ///
380     virtual bool Permit(short f) const { return bool(f & flag); }
381     
382  protected:
383     /// Paragraph data is stored here
384     MathedArray * array;
385     /// Cursor start position
386     int xo;
387     ///
388     int yo;
389     /// 
390     short flag;
391
392  private:
393     ///
394     virtual void setFlag(MathedParFlag f) { flag |= f; }
395     ///
396     friend class InsetFormula;
397     ///
398     friend class MathedXIter;
399     ///
400     friend class MathedCursor;
401     ///
402     friend MathedArray * mathed_parse(unsigned flags = 0,
403                                        MathedArray * a = 0,
404                                        MathParInset ** p = 0);
405 };
406
407
408 /** The physical structure of a row and aditional information is stored here.
409     It allows to manage the extra info independently of the paragraph data.  
410     Only used for multiline paragraphs.
411  */
412 struct MathedRowSt
413 {
414         ///
415         explicit
416         MathedRowSt(int n)
417                 : asc_(0), desc_(0), y_(0), widths_(n + 1, 0),
418                   numbered_(true), next_(0)
419                 {}
420         /// Should be const but...
421         MathedRowSt * getNext() const  { return next_; }
422         /// ...we couldn't use this.
423         void setNext(MathedRowSt * n) { next_ = n; }
424         ///
425         string const & getLabel() const { return label_; }
426         ///
427         bool isNumbered() const { return numbered_; }
428         ///
429         int  getBaseline() const { return y_; }
430         ///
431         void setBaseline(int b) { y_ = b; }
432         ///
433         int ascent() const { return asc_; }
434         ///
435         int descent() const { return desc_; }
436         ///
437         void ascent(int a) { asc_ = a; }
438         ///
439         void descent(int d) { desc_ = d; }
440         ///
441         int  getTab(int i) const { return widths_[i]; }
442         /// 
443         void setLabel(string const & l) { label_ = l; }
444         ///
445         void setNumbered(bool nf) { numbered_ = nf; }
446         ///
447         void setTab(int i, int t) { widths_[i] = t; }
448 private:
449         /// Vericals 
450         int asc_;
451         int desc_;
452         int y_;
453         /// widths 
454         std::vector<int> widths_;
455         /// 
456         string label_;
457         ///
458         bool numbered_;
459         ///
460         MathedRowSt * next_;
461 };
462
463
464 /** Multiline math paragraph base class.
465     This is the base to all multiline editable math objects
466     like array and eqnarray. 
467  */
468 class MathMatrixInset: public MathParInset {
469  public: 
470     ///
471     explicit
472     MathMatrixInset(int m = 1, int n = 1, short st = LM_ST_TEXT);
473     ///
474     explicit
475     MathMatrixInset(MathMatrixInset *);
476     ///
477     MathedInset * Clone();
478     ///
479     virtual ~MathMatrixInset();
480     ///
481     void draw(Painter &, int, int);
482     ///
483     void Write(std::ostream &, bool fragile);
484     ///
485     void Metrics();
486     ///
487     void SetData(MathedArray *);
488     ///
489     void SetAlign(char, string const &);
490     ///
491     int GetColumns() const { return nc; }
492     ///
493     int GetRows() const { return nr; }
494     ///
495     virtual bool isMatrix() const { return true; }
496
497     /// Use this to manage the extra information independently of paragraph
498     MathedRowSt * getRowSt() const { return row; }
499     ///
500     void setRowSt(MathedRowSt * r) { row = r; }
501     
502  protected:
503     ///  Number of columns & rows
504     int nc;
505     ///
506     int nr;
507     /// tab sizes
508     int * ws;   
509     /// 
510     char v_align; // add approp. type
511     ///
512     string h_align; // a vector would perhaps be more correct
513     /// Vertical structure
514     MathedRowSt * row;
515
516 };
517
518
519
520 /*************************  Prototypes  **********************************/
521 /// 
522 MathedArray * mathed_parse(unsigned flags, MathedArray * data,
523                             MathParInset ** mt);
524 ///
525 void mathed_write(MathParInset *, std::ostream &, int *, bool fragile,
526                   string const & label = string());
527
528 ///
529 void mathed_parser_file(std::istream &, int);
530 ///
531 int mathed_parser_lineno();
532 ///
533 int MathedLookupBOP(short);
534
535 /************************ Inline functions ********************************/
536
537 ///
538 inline
539 bool MathIsInset(short x)
540 {
541         return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
542 }
543
544 ///
545 inline
546 bool MathIsFont(short x)
547 {
548         return LM_TC_CONST <= x && x <= LM_TC_BSYM;
549 }
550
551 ///
552 inline
553 bool MathIsAlphaFont(short x)
554 {
555         return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
556 }
557
558 ///
559 inline
560 bool MathIsActive(short x)
561 {
562         return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
563 }
564
565 ///
566 inline
567 bool MathIsUp(short x)
568 {
569         return x == LM_TC_UP;
570 }
571
572 ///
573 inline
574 bool MathIsDown(short x)
575 {
576         return x == LM_TC_DOWN;
577 }
578
579 ///
580 inline
581 bool MathIsScript(short x)
582 {
583         return x == LM_TC_DOWN || x == LM_TC_UP;
584 }
585
586 ///
587 inline
588 bool MathIsBOPS(short x)
589 {
590         return MathedLookupBOP(x) > LMB_NONE;
591 }
592
593 ///
594 inline
595 bool MathIsBinary(short x)
596 {
597     return x == LM_TC_BOP || x == LM_TC_BOPS;
598 }
599
600 ///
601 inline
602 bool MathIsSymbol(short x) {
603     return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
604 }
605      
606
607 inline
608 MathedInset::MathedInset(string const & nm, short ot, short st):
609   name(nm), objtype(ot), size(st) 
610 {
611    width = ascent = descent = 0;
612 }
613
614
615 inline
616 bool MathParInset::Inside(int x, int y) 
617 {
618   return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
619 }
620
621
622 inline
623 void MathParInset::GetXY(int & x, int & y) const
624 {
625    x = xo; y = yo;
626 }
627
628
629 inline
630 void MathParInset::UserSetSize(short sz)
631 {
632    if (sz >= 0) {
633        size = sz;      
634        flag = flag & ~LMPF_FIXED_SIZE;
635    }
636 }
637
638
639 inline
640 void MathParInset::SetStyle(short sz) 
641 {
642     if (Permit(LMPF_FIXED_SIZE)) {
643         if (Permit(LMPF_SCRIPT)) 
644           sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
645         if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
646             ++sz;
647         } 
648         MathedInset::SetStyle(sz);
649     }
650 }
651
652 inline
653 bool is_eqn_type(short int type)
654 {
655         return type >= LM_OT_MIN && type < LM_OT_MATRIX;
656 }
657
658
659 inline
660 bool is_matrix_type(short int type)
661 {
662         return type == LM_OT_MATRIX;
663 }
664
665 inline
666 bool is_multiline(short int type)
667 {
668         return type >= LM_OT_MPAR && type < LM_OT_MATRIX;
669 }
670
671
672 inline bool is_ams(short int type)
673 {
674         return type > LM_OT_MPARN && type < LM_OT_MATRIX;
675 }
676
677 inline
678 bool is_singlely_numbered(short int type)
679 {
680         return type == LM_OT_PARN || type == LM_OT_MULTLINEN;
681 }
682
683 inline
684 bool is_multi_numbered(short int type)
685 {
686         return type == LM_OT_MPARN || type == LM_OT_ALIGNN
687                 || type == LM_OT_ALIGNATN;
688 }
689
690 inline
691 bool is_numbered(short int type)
692 {
693         return is_singlely_numbered(type) || is_multi_numbered(type);
694 }
695
696 inline
697 bool is_multicolumn(short int type)
698 {
699         return type == LM_OT_ALIGN || type == LM_OT_ALIGNN
700                 || type == LM_OT_ALIGNAT || type == LM_OT_ALIGNATN;
701 }
702
703 #endif