]> git.lyx.org Git - lyx.git/blob - src/mathed/math_defs.h
use the new sstream return non-pods as const, use string instead of char * in a lot...
[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 #define MATH_COLSEP 8
49 ///
50 #define 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 /// Standard LaTeX Math Environments
67 enum MathedEnvironment {
68         ///
69         LM_EN_INTEXT = 0,
70         ///
71         LM_EN_DISPLAY,
72         ///
73         LM_EN_EQUATION,
74         ///
75         LM_EN_EQNARRAYAST,
76         ///
77         LM_EN_EQNARRAY,
78         ///
79         LM_EN_ARRAY
80 };
81
82
83 /** The restrictions of a standard LaTeX math paragraph
84   allows to get a small number of text codes (<30) */
85 enum MathedTextCodes  {
86         /// This must be >= 0
87         LM_TC_MIN = 0,
88         /// Open and Close group
89         LM_TC_OPEN,
90         ///
91         LM_TC_CLOSE,
92         /// Tabulator
93         LM_TC_TAB,
94         /// New line
95         LM_TC_CR,
96         /// Math Inset
97         LM_TC_INSET,
98         /// Super and sub scripts
99         LM_TC_UP,
100         ///
101         LM_TC_DOWN,
102         /// Editable Math Inset
103         LM_TC_ACTIVE_INSET,
104         /// Editable Text Inset
105         LM_TC_TEXT_INSET,
106         ///
107         LM_FONT_BEGIN,
108         /// Internal code for constants
109         LM_TC_CONST,
110         /// Internal code for variables
111         LM_TC_VAR,
112         ///
113         LM_TC_RM,
114         ///
115         LM_TC_CAL,
116         ///
117         LM_TC_BF,
118         ///
119         LM_TC_SF,
120         ///
121         LM_TC_TT,
122         ///
123         LM_TC_IT,
124         ///
125         LM_TC_TEXTRM,
126         /// Math mode TeX characters ",;:{}"
127         LM_TC_TEX,
128         /// Special characters "{}&#_%"
129         LM_TC_SPECIAL,
130         /// Internal code for operators
131         LM_TC_BOP,
132         /// Internal code for symbols
133         LM_TC_SYMB,
134         ///
135         LM_TC_BOPS,
136         ///
137         LM_TC_BSYM,
138         ///
139         LM_FONT_END,
140         
141         /// This must be < 32 
142         LM_TC_MAX
143 };
144
145 ///
146 std::ostream & operator<<(std::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(string const & nm, short ot, short st);
225     ///
226     explicit
227     MathedInset(MathedInset *);
228     ///
229     virtual ~MathedInset() {}
230
231     /// Draw the object
232     virtual void draw(Painter &, int x, int baseline) = 0;      
233
234     /// Write LaTeX and Lyx code
235     virtual void Write(std::ostream &, bool fragile) = 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     string 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(string const & n) { name = n; }
270  protected:
271     ///
272     string name;
273     ///
274     short objtype;
275     ///
276     int width;
277     ///
278     int ascent;
279     ///
280     int descent;
281     ///
282     short size;
283     /// Default metrics
284     static int df_asc;
285     ///
286     static int df_des;
287     ///
288     static int df_width;
289     /// In a near future maybe we use a better fonts renderer than X
290     void drawStr(Painter &, short, int, int, int, byte const *, int);
291     ///
292     friend class MathedCursor;
293     ///
294     friend void mathed_init_fonts();
295 };
296
297 struct MathedRowSt;
298
299
300 /// Paragraph permissions
301 enum MathedParFlag {
302         LMPF_BASIC = 0,
303         /// If false can use a non-standard size
304         LMPF_FIXED_SIZE = 1,
305         /// If true can insert newlines 
306         LMPF_ALLOW_CR  = 2,
307         /// If true can use tabs
308         LMPF_ALLOW_TAB = 4,
309         /// If true can insert new columns
310         LMPF_ALLOW_NEW_COL = 8,
311         /// Smaller than current size (frac)
312         LMPF_SMALLER = 16,
313         /// Script size (subscript, stackrel)
314         LMPF_SCRIPT = 32
315 };
316
317
318 /** The math paragraph base class, base to all editable math objects */
319 class MathParInset: public MathedInset  {
320  public: 
321     ///
322     MathParInset(short st = LM_ST_TEXT, string const & nm = string(),
323                  short ot = LM_OT_MIN);
324     ///
325     explicit
326     MathParInset(MathParInset *);
327     ///
328     virtual ~MathParInset();
329     ///
330     virtual MathedInset * Clone();
331
332     /// Draw the object on a drawable
333     virtual void draw(Painter &, int x, int baseline);
334
335     /// Write LaTeX code
336     virtual void Write(std::ostream &, bool fragile);
337
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, string const &) {}
360     ///
361     virtual int GetColumns() { return 1; }
362     ///
363     virtual int GetRows() { return 1; }
364     ///
365     virtual bool isMatrix() { return false; }
366     // Vertical switching
367     ///
368     virtual bool setArgumentIdx(int i) { return (i == 0); }
369     ///
370     virtual bool setNextArgIdx() { return false; }
371     ///
372     virtual int  getArgumentIdx() { return 0; }
373     ///
374     virtual int  getMaxArgumentIdx() { return 0; }
375     ///
376     virtual void SetStyle(short);
377     ///
378     virtual MathedRowSt * getRowSt() const { return 0; }
379     ///
380     virtual void setRowSt(MathedRowSt *) {}
381     ///
382     virtual bool Permit(short f) { return bool(f & flag); }
383     
384  protected:
385     /// Paragraph data is stored here
386     LyxArrayBase * array;
387     /// Cursor start position
388     int xo;
389     ///
390     int yo;
391     /// 
392     short flag;
393
394  private:
395     ///
396     virtual void setFlag(MathedParFlag f) { flag |= f; }
397     ///
398     friend class InsetFormula;
399     ///
400     friend class MathedXIter;
401     ///
402     friend class MathedCursor;
403     ///
404     friend LyxArrayBase * mathed_parse(unsigned flags = 0,
405                                        LyxArrayBase * a = 0,
406                                        MathParInset ** p = 0);
407 };
408
409
410 /** The physical structure of a row and aditional information is stored here.
411     It allows to manage the extra info independently of the paragraph data.  
412     Only used for multiline paragraphs.
413  */
414 struct MathedRowSt {    
415     ///
416     explicit
417     MathedRowSt(int n) {
418             w = new int[n + 1]; // this leaks
419             asc = desc = y = 0;
420             for (int i = 0 ; i < n + 1 ; ++i)
421               w[i] = 0;
422             next = 0;
423             //label = 0;
424             numbered = true;
425     }
426     ///
427     ~MathedRowSt() {
428             delete[] w;
429             //delete[] label;
430     }
431     /// Should be const but...
432     MathedRowSt * getNext() const  { return next; }
433     /// ...we couldn't use this.
434     void setNext(MathedRowSt * n) { next = n; }
435     ///
436     string const & getLabel() const { return label; }
437     ///
438     bool isNumbered() const { return numbered; }
439     ///
440     int  getBaseline() const { return y; }    
441     ///
442     int  getTab(int i) { return w[i]; }
443     /// 
444     void setLabel(string const & l) { label = l; }
445     ///
446     void setNumbered(bool nf) { numbered = nf; }
447     ///
448     void setTab(int i, int t) { w[i] = t; }
449     
450  private:
451     /// Vericals 
452     int asc, desc, y;
453     /// widths 
454     int * w;
455     /// 
456     string label;
457     ///
458     bool numbered;
459     ///
460     MathedRowSt * next;
461     ///
462     friend class MathMatrixInset;
463     ///
464     friend class MathedXIter;
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(LyxArrayBase *);
492     ///
493     void SetAlign(char, char const *);
494     ///
495     char * GetAlign(char * vv) {
496         *vv = v_align;
497         return h_align;
498     }
499     ///
500     int GetColumns() { return nc; }
501     ///
502     int GetRows() { return nr; }
503     ///
504     virtual bool isMatrix() { return true; }
505
506     /// Use this to manage the extra information independently of paragraph
507     MathedRowSt * getRowSt() const { return row; }
508     ///
509     void setRowSt(MathedRowSt * r) { row = r; }
510     
511  protected:
512     ///  Number of columns & rows
513     int nc;
514     ///
515     int nr;
516     /// tab sizes
517     int * ws;   
518     /// 
519     char v_align; // add approp. type
520     ///
521     char * h_align;
522     /// Vertical structure
523     MathedRowSt * row;
524
525 };
526
527
528
529 /*************************  Prototypes  **********************************/
530 /// 
531 LyxArrayBase * mathed_parse(unsigned flags, LyxArrayBase * data,
532                             MathParInset ** mt);
533 ///
534 void mathed_write(MathParInset *, std::ostream &, int *, bool fragile,
535                   string const & label = string());
536
537 ///
538 void mathed_parser_file(std::istream &, int);
539 ///
540 int mathed_parser_lineno();
541 ///
542 int MathedLookupBOP(short);
543
544 /************************ Inline functions ********************************/
545
546 ///
547 inline
548 bool MathIsInset(short x)
549 {
550         return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
551 }
552
553 ///
554 inline
555 bool MathIsFont(short x)
556 {
557         return LM_TC_CONST <= x && x <= LM_TC_BSYM;
558 }
559
560 ///
561 inline
562 bool MathIsAlphaFont(short x)
563 {
564         return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
565 }
566
567 ///
568 inline
569 bool MathIsActive(short x)
570 {
571         return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
572 }
573
574 ///
575 inline
576 bool MathIsUp(short x)
577 {
578         return x == LM_TC_UP;
579 }
580
581 ///
582 inline
583 bool MathIsDown(short x)
584 {
585         return x == LM_TC_DOWN;
586 }
587
588 ///
589 inline
590 bool MathIsScript(short x)
591 {
592         return x == LM_TC_DOWN || x == LM_TC_UP;
593 }
594
595 ///
596 inline
597 bool MathIsBOPS(short x)
598 {
599         return MathedLookupBOP(x) > LMB_NONE;
600 }
601
602 ///
603 inline
604 bool MathIsBinary(short x)
605 {
606     return x == LM_TC_BOP || x == LM_TC_BOPS;
607 }
608
609 ///
610 inline
611 bool MathIsSymbol(short x) {
612     return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
613 }
614      
615
616 inline
617 MathedInset::MathedInset(string const & nm, short ot, short st):
618   name(nm), objtype(ot), size(st) 
619 {
620    width = ascent = descent = 0;
621 }
622
623
624 inline
625 bool MathParInset::Inside(int x, int y) 
626 {
627   return (x >= xo && x <= xo + width && y <= yo + descent && y >= yo - ascent);
628 }
629
630
631 inline
632 void MathParInset::GetXY(int & x, int & y) const
633 {
634    x = xo; y = yo;
635 }
636
637
638 inline
639 void MathParInset::UserSetSize(short sz)
640 {
641    if (sz >= 0) {
642        size = sz;      
643        flag = flag & ~LMPF_FIXED_SIZE;
644    }
645 }
646
647
648 inline
649 void MathParInset::SetStyle(short sz) 
650 {
651     if (Permit(LMPF_FIXED_SIZE)) {
652         if (Permit(LMPF_SCRIPT)) 
653           sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
654         if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
655             ++sz;
656         } 
657         MathedInset::SetStyle(sz);
658     }
659 }
660
661 #endif