]> git.lyx.org Git - lyx.git/blob - src/mathed/math_defs.h
removed a warning from screen and added CFLAGS in lyx.spec.in.
[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 <cstdio>
28 #include "LString.h"
29 #include "debug.h"
30
31 #include "array.h"
32
33 ///
34 #define MATH_ALIGN_LEFT    1
35 ///
36 #define MATH_ALIGN_RIGHT   2
37 ///
38 #define MATH_ALIGN_BOTTOM  4
39 ///
40 #define MATH_ALIGN_TOP     8
41 ///
42 #define MATH_COLSEP 8
43 ///
44 #define MATH_ROWSEP 8
45
46
47 /// Standard Math Sizes (Math mode styles)
48 enum MathedStyles {
49         ///
50    LM_ST_DISPLAY=0,
51    ///
52    LM_ST_TEXT,
53    ///
54    LM_ST_SCRIPT,
55    ///
56    LM_ST_SCRIPTSCRIPT
57 };
58
59
60 /// Standard LaTeX Math Environments
61 enum MathedEnvironment {
62         ///
63    LM_EN_INTEXT=0,
64    ///
65    LM_EN_DISPLAY,
66    ///
67    LM_EN_EQUATION,
68    ///
69    LM_EN_EQNARRAYAST,
70    ///
71    LM_EN_EQNARRAY,
72    ///
73    LM_EN_ARRAY
74 };
75
76
77 /** The restrictions of a standard LaTeX math paragraph
78   allows to get a small number of text codes (<30) */
79 enum MathedTextCodes  {
80    /// This must be >= 0
81    LM_TC_MIN = 0,
82    /// Open and Close group
83    LM_TC_OPEN,
84    ///
85    LM_TC_CLOSE,
86    /// Tabulator
87    LM_TC_TAB,
88    /// New line
89    LM_TC_CR,
90    /// Math Inset
91    LM_TC_INSET,
92    /// Super and sub scripts
93    LM_TC_UP,
94    ///
95    LM_TC_DOWN,
96    /// Editable Math Inset
97    LM_TC_ACTIVE_INSET,
98    /// Editable Text Inset
99    LM_TC_TEXT_INSET,
100    ///
101    LM_FONT_BEGIN,
102    /// Internal code for constants
103    LM_TC_CONST,
104    /// Internal code for variables
105    LM_TC_VAR,
106    ///
107    LM_TC_RM,
108    ///
109    LM_TC_CAL,
110    ///
111    LM_TC_BF,
112    ///
113    LM_TC_SF,
114    ///
115    LM_TC_TT,
116    ///
117    LM_TC_IT,
118    ///
119    LM_TC_TEXTRM,
120    /// Math mode TeX characters ",;:{}"
121    LM_TC_TEX,
122    /// Special characters "{}&#_%"
123    LM_TC_SPECIAL,
124    /// Internal code for operators
125    LM_TC_BOP,
126    /// Internal code for symbols
127    LM_TC_SYMB,
128    ///
129    LM_TC_BOPS,
130    ///
131    LM_TC_BSYM,
132    ///
133    LM_FONT_END,
134    
135    /// This must be < 32 
136    LM_TC_MAX
137 };
138
139 ostream & operator<<(ostream &, MathedTextCodes mtc);
140
141 ///
142 #define LM_TC_NORMAL LM_TC_VAR
143  
144        
145 /// Types of lyx-math insets 
146 enum MathedInsetTypes  {
147    ///
148    LM_OT_MIN = 0,
149    /// A simple paragraph
150    LM_OT_PAR,
151    /// A simple numbered paragraph
152    LM_OT_PARN,
153    /// A multiline paragraph
154    LM_OT_MPAR,
155    /// A multiline numbered paragraph
156    LM_OT_MPARN,
157    /// An array
158    LM_OT_MATRIX,
159    /// A big operator
160    LM_OT_BIGOP,
161    /// A LaTeX macro
162    LM_OT_UNDEF,
163    ///
164    LM_OT_FUNC,
165    ///
166    LM_OT_FUNCLIM,
167    ///
168    LM_OT_SCRIPT,
169    ///
170    LM_OT_SPACE,
171    ///
172    LM_OT_DOTS,
173    /// A fraction
174    LM_OT_FRAC,
175    ///
176    LM_OT_ATOP,
177    ///
178    LM_OT_STACKREL,
179    /// A radical
180    LM_OT_SQRT,
181    /// A delimiter
182    LM_OT_DELIM,
183    /// A decoration
184    LM_OT_DECO,
185    /// An accent
186    LM_OT_ACCENT,
187    ///
188    LM_OT_MACRO,
189    ///
190    LM_OT_MACRO_ARG,
191    ///
192    LM_OT_MAX
193 };
194
195 ///
196 enum MathedBinaryTypes {
197         ///
198     LMB_NONE = 0,
199     ///
200     LMB_RELATION,
201     ///
202     LMB_OPERATOR,
203     ///
204     LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
205 };
206
207 class LString;
208 class MathedInset;
209 class MathParInset;
210
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(char const *nm, short ot, short st);
221     ///
222     MathedInset(MathedInset*);
223     ///
224     virtual ~MathedInset() { };
225     
226     /// Draw the object 
227     virtual void Draw(int x, int baseline)=0;
228     
229     /// Write LaTeX and Lyx code
230     virtual void Write(FILE *file)=0;
231     /// Write LaTeX and Lyx code
232     virtual void Write(string & file)=0;
233    
234     /// Reproduces itself
235     virtual MathedInset *Clone()=0;
236    
237     /// Compute the size of the object
238     virtual void Metrics()=0; 
239     /// 
240     virtual int Ascent() const { return ascent; }
241     ///
242     virtual int Descent() const { return descent; }
243     ///
244     virtual int Width() const { return width; }
245     ///
246     virtual int Height() const { return ascent + descent; }
247     
248     ///
249     virtual bool GetLimits() const { return false; }
250     ///
251     virtual void SetLimits(bool) { }   
252    
253     ///
254     char const *GetName() const { return name; }
255     ///
256     short GetType() const { return objtype; }
257     ///
258     short GetStyle() const { return size; }
259           
260     //Man:  Avoid to use these functions if it's not strictly necessary 
261     ///
262     virtual void  SetType(short t) { objtype = t; }
263     ///
264     virtual void  SetStyle(short st) { size = st; } // Metrics();
265     ///
266     virtual void  SetName(char const* n) { name = n; }
267     /// 
268     void setDrawable(long unsigned int d) { pm = d; }
269  
270  protected:
271     ///
272     char const *name;
273     ///
274     short objtype;
275     ///
276     int width;
277     ///
278     int ascent;
279     ///
280     int descent;
281     ///
282     short size;
283     /// This works while only one process can draw unless
284     /// the process have their own data
285     static long unsigned int pm;
286     /// Default metrics
287     static int df_asc, df_des, df_width;
288
289     /// In a near future maybe we use a better fonts renderer than X
290     void drawStr(short, int, int, int, byte*, 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, char const *nm=0, 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(int x, int baseline);
332    
333     /// Write LaTeX code
334     virtual void Write(FILE *file);
335     /// Write LaTeX code
336     virtual void Write(string &file);
337     ///
338     virtual void Metrics();
339     ///
340     virtual void UserSetSize(short);
341  
342     /// Data is stored in a LyXArray
343     virtual void SetData(LyxArrayBase *);
344     ///
345     virtual LyxArrayBase * GetData() { return array; }
346
347     /// Paragraph position
348     virtual void GetXY(int&, int&) const;
349     ///
350     virtual void setXY(int x, int y) { xo =x;  yo = y; }
351     ///
352     virtual void SetFocus(int,int) { };
353     ///
354     virtual bool Inside(int, int);   
355    
356     // Tab stuff used by Matrix.
357     ///
358     virtual void SetAlign(char, char const*) { };
359 //    ///
360 //    virtual int GetTabPos() { return 0; }
361 //    ///
362 //    virtual int GetTab(int) { return 0; }
363     ///
364     virtual int GetColumns() { return 1; }
365     ///
366     virtual int GetRows() { return 1; }
367     ///
368     virtual bool isMatrix() { return false; }
369 //    /// These functions should report an error
370 //    virtual char const* GetLabel() { return 0; }
371 //    virtual char const* GetLabel(int) { return 0; }
372
373     // Vertical switching
374     ///
375     virtual bool setArgumentIdx(int i) { return (i==0); }
376     ///
377     virtual bool setNextArgIdx() { return false; }
378     ///
379     virtual int  getArgumentIdx() { return 0; }
380     ///
381     virtual int  getMaxArgumentIdx() { return 0; }
382 //    ///
383 //    virtual void SetLabel(char const*) { }
384     ///
385     virtual void SetStyle(short);
386         ///
387     virtual MathedRowSt *getRowSt() const { return 0; }
388         ///
389     virtual void setRowSt(MathedRowSt*) { }
390         ///
391     virtual bool Permit(short f) { return (bool)(f & flag); }
392     
393  protected:
394     /// Paragraph data is stored here
395     LyxArrayBase *array;
396     /// Cursor start position
397     int xo, yo;
398     /// 
399     short flag;
400
401  private:
402     ///
403     virtual void setFlag(MathedParFlag f) { flag |= f; }
404         ///
405     friend class InsetFormula;
406         ///
407     friend class MathedXIter;
408         ///
409     friend class MathedCursor;
410         ///
411     friend LyxArrayBase *mathed_parse(unsigned flags = 0, LyxArrayBase*a=0, MathParInset**p=0);
412 };
413
414
415 /* The physical structure of a row and aditional information is stored here.
416     It allows to manage the extra info independently of the paragraph data.  
417     Only used for multiline paragraphs.
418  */
419 struct MathedRowSt {    
420     /// 
421     MathedRowSt(int n) {
422             w = new int[n+1]; // this leaks
423         next = 0;
424         label = 0;
425         numbered = true;
426     }
427     ///
428     ~MathedRowSt() {
429         delete[] w;
430         if (label) delete[] label;
431     }
432     /// Should be const but...
433     MathedRowSt* getNext() const  { return next; }
434     /// ...we couldn't use this.
435     void setNext(MathedRowSt* n) { next = n; }
436     ///
437     char const* getLabel() const { return label; }
438     ///
439     bool isNumbered() const { return numbered; }
440     ///
441     int  getBaseline() const { return y; }    
442     ///
443     int  getTab(int i) { return w[i]; }
444     /// 
445     void setLabel(char* l) { label = l; }
446     ///
447     void setNumbered(bool nf) { numbered = nf; }
448     ///
449     void setTab(int i, int t) { w[i] = t; }
450     
451  private:
452     /// Vericals 
453     int asc, desc, y;
454     /// widths 
455     int *w;
456     /// 
457     char *label;
458     ///
459     bool numbered;
460     ///
461     MathedRowSt *next;
462         ///
463     friend class MathMatrixInset;
464         ///
465     friend class MathedXIter;
466 };
467
468
469 /** Multiline math paragraph base class.
470     This is the base to all multiline editable math objects
471     like array and eqnarray. 
472  */
473 class MathMatrixInset: public MathParInset {
474  public: 
475     ///
476     MathMatrixInset(int m=1, int n=1, short st=LM_ST_TEXT);
477     ///
478     MathMatrixInset(MathMatrixInset*);
479     ///
480     MathedInset *Clone();
481     ///
482     virtual ~MathMatrixInset();
483     ///
484     void Draw(int, int);
485     ///
486     void Write(FILE *file);
487     ///
488     void Write(string &file);
489     ///
490     void Metrics();
491     ///
492     void SetData(LyxArrayBase *);
493     ///
494     void SetAlign(char, char const*);
495     ///
496     char *GetAlign(char* vv) {
497         *vv = v_align;
498         return h_align;
499     }
500 //    ///
501 //    int GetTab(int);
502     ///
503     int GetColumns() { return nc; }
504     ///
505     int GetRows() { return nr; }
506     ///
507     virtual bool isMatrix() { return true; }
508
509     /// Use this to manage the extra information independently of paragraph
510     MathedRowSt *getRowSt() const { return row; }
511         ///
512     void setRowSt(MathedRowSt* r) { row = r; }
513     
514  protected:
515     ///  Number of columns & rows
516     int nc, nr;
517     /// tab sizes
518     int *ws;   
519     /// 
520     char v_align; // add approp. signedness
521     ///
522     char* h_align;
523     /// Vertical structure
524     MathedRowSt *row;
525
526 };
527
528
529
530 /*************************  Prototypes  **********************************/
531 /// 
532 LyxArrayBase *mathed_parse(unsigned flags, LyxArrayBase *data, MathParInset **mt);
533 ///
534 void mathed_write(MathParInset*, FILE *, int*, char fragile, char const* label=0);
535 ///
536 void mathed_write(MathParInset*, string&, int*, char fragile, char const* label=0);
537 ///
538 void mathed_parser_file(FILE*, int);
539 ///
540 int mathed_parser_lineno();
541 ///
542 int MathedLookupBOP(short);
543
544 /************************ Inline functions ********************************/
545
546 ///
547 #define MathIsInset(x)  (LM_TC_INSET<=(x) && (x)<=LM_TC_ACTIVE_INSET)
548 ///
549 #define MathIsFont(x)  (LM_TC_CONST<=(x) && (x)<=LM_TC_BSYM)
550 ///
551 #define MathIsAlphaFont(x)  (LM_TC_VAR<=(x) && (x)<=LM_TC_TEXTRM)
552 ///
553 #define MathIsActive(x)  (LM_TC_INSET<(x) && (x)<=LM_TC_ACTIVE_INSET) 
554 ///
555 #define MathIsUp(x)    ((x)==LM_TC_UP) 
556 ///
557 #define MathIsDown(x)  ((x)==LM_TC_DOWN)
558 ///
559 #define MathIsScript(x)  ((x)==LM_TC_DOWN || (x)==LM_TC_UP)  
560 ///
561 #define MathIsBOPS(x)    (MathedLookupBOP(x)>LMB_NONE)
562
563
564 ///
565 inline bool MathIsBinary(short x)
566 {
567     return (x==LM_TC_BOP || x==LM_TC_BOPS);
568 }
569
570 ///
571 inline bool MathIsSymbol(short x) {
572     return (LM_TC_SYMB<=x && x<=LM_TC_BSYM);
573 }
574      
575
576 inline 
577 MathedInset::MathedInset(char const *nm, short ot, short st):
578   name(nm), objtype(ot), size(st) 
579 {
580    width = ascent = descent = 0;
581 }
582
583 inline
584 bool MathParInset::Inside(int x, int y) 
585 {
586   return (x>=xo && x<=xo+width && y<=yo+descent && y>=yo-ascent);
587 }
588
589
590 inline
591 void MathParInset::GetXY(int& x, int& y) const
592 {
593    x = xo; y = yo;
594 }
595
596
597 inline
598 void MathParInset::UserSetSize(short sz)
599 {
600    if (sz>=0) {
601        size = sz;      
602        flag = flag & ~LMPF_FIXED_SIZE;
603    }
604 }
605
606
607 inline
608 void MathParInset::SetStyle(short sz) 
609 {
610     if (Permit(LMPF_FIXED_SIZE)) {
611         if (Permit(LMPF_SCRIPT)) 
612           sz = (sz<LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
613         if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
614             sz++;
615         } 
616         MathedInset::SetStyle(sz);
617     }
618 }
619
620 #endif
621