]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.h
Cxx tweaks, fix display of \epsilon and more. Read changelog
[lyx.git] / src / mathed / math_macro.h
1 // -*- C++ -*-
2 /*
3  *  File:        math_macro.h
4  *  Purpose:     Declaration of macro class for mathed 
5  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
6  *  Created:     November 1996
7  *  Description: WYSIWYG math macros
8  *
9  *  Dependencies: Mathed
10  *
11  *  Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra
12  *
13  *   Version: 0.2, Mathed & Lyx project.
14  *
15  *   This code is under the GNU General Public Licence version 2 or later.
16  */
17 #ifndef MATH_MACRO
18 #define MATH_MACRO
19
20 #ifdef __GNUG__
21 #pragma interface
22 #endif
23
24 #include "math_defs.h"
25 #include "debug.h"
26
27 ///
28 typedef MathParInset* MathParInsetP;
29 ///
30 typedef LyxArrayBase *LyxArrayBaseP;
31
32 class MathMacroTemplate;
33
34
35 /// This class contains the data for a macro
36 class MathMacro: public MathParInset
37 {
38  public:
39     /// A macro can only be builded from an existing template
40     MathMacro(MathMacroTemplate *);
41     /// or from another macro.
42     MathMacro(MathMacro*);
43         ///
44     ~MathMacro();
45         ///
46     void Draw(int, int);
47         ///
48     void Metrics();
49         ///
50     MathedInset * Clone();
51         ///
52     void Write(FILE *);
53         ///
54     void Write(string &);
55         ///
56     bool setArgumentIdx(int);
57         ///
58     int  getArgumentIdx();
59         ///
60     int  getMaxArgumentIdx();
61         ///
62     int GetColumns();
63         ///
64     void GetXY(int&, int&) const;
65         ///
66     void SetFocus(int, int);
67         ///
68     LyxArrayBase *GetData();
69         ///
70     MathedRowSt *getRowSt() const { return args[idx].row; }
71         ///
72     void SetData(LyxArrayBase *);
73         ///
74     MathedTextCodes getTCode() { return tcode; }
75         ///
76     bool Permit(short);
77     
78  private:
79         ///
80     MathMacroTemplate *tmplate;
81         ///
82     struct MacroArgumentBase {
83         /// Position of the macro
84         int x, y;
85             ///
86         MathedRowSt *row;
87             ///
88         LyxArrayBase *array;
89             ///
90         MacroArgumentBase() { x = y = 0;  array = 0; row = 0; }
91     } *args;
92         ///
93     int idx;
94         ///
95     int nargs;
96         ///
97     MathedTextCodes tcode;
98         ///
99     friend class MathMacroTemplate;
100 };
101
102
103 /// An argument
104 class MathMacroArgument: public MathParInset
105 {
106  public:
107     ///
108     MathMacroArgument() { expnd_mode = false; number = 1;  SetType(LM_OT_MACRO_ARG); }
109     ///
110     MathMacroArgument(int);
111     ///
112         ~MathMacroArgument() { lyxerr << "help, destroyme!" << endl; }
113     ///
114     MathedInset * Clone() { return this; }
115         ///
116     void Metrics();
117         ///
118     void Draw(int x, int baseline);
119         ///
120     void Write(FILE*);
121         ///
122     void Write(string &);
123     ///
124     void setNumber(int n) { number = n; }
125     /// Is expanded or not
126     void setExpand(bool e) { expnd_mode = e; }
127     /// Is expanded or not
128     bool getExpand() { return expnd_mode; }
129     
130  private:
131         ///
132     bool expnd_mode;
133         ///
134     int number;
135 };
136
137
138 /// This class contains the macro definition
139 class MathMacroTemplate: public MathParInset
140 {
141  public:
142     /// A template constructor needs all the data
143     MathMacroTemplate(char const*, int na= 0, int f= 0);
144         ///
145     ~MathMacroTemplate();
146         ///
147     void Draw(int, int);
148         ///
149     void Metrics();
150         ///
151     void WriteDef(FILE *); 
152         ///
153     void WriteDef(string &); 
154     /// useful for special insets
155     void  setTCode(MathedTextCodes t) { tcode = t; }
156     ///
157     MathedTextCodes getTCode() { return tcode; }
158     /// 
159     void setArgument(LyxArrayBase *, int i= 0);
160     /// Number of arguments
161     int getNoArgs() { return nargs; }
162     ///
163     void GetMacroXY(int, int&, int&) const;
164     ///
165     MathParInset *getMacroPar(int) const;
166     ///
167     void SetMacroFocus(int&, int, int);
168         ///
169     void setEditMode(bool);
170
171     /// Replace the appropriate arguments with a specific macro's data
172     void update(MathMacro* m= 0);
173       
174  private:
175     ///
176     short flags;
177     ///
178     MathedTextCodes tcode;
179     ///
180     MathMacroArgument *args;
181     ///
182     int nargs;
183     ///
184     friend class MathMacro;
185 };
186      
187
188 ///
189 typedef MathMacro *MathMacroP;
190 ///
191 typedef MathMacroTemplate *MathMacroTemplateP;
192
193 ///
194 class MathMacroTable 
195 {
196  public:
197         ///
198     MathMacroTable(int);
199         ///
200     ~MathMacroTable();
201         ///
202     void addTemplate(MathMacroTemplate *);
203         ///
204     MathMacro *getMacro(char const*) const;
205         ///
206     MathMacroTemplate *getTemplate(char const*) const;
207         ///
208     void builtinMacros();
209         ///
210     static MathMacroTable mathMTable;
211         ///
212     static bool built;
213     
214  private:
215         ///
216     const int max_macros;
217         ///
218     int num_macros;
219         ///
220     MathMacroTemplateP *macro_table;
221 };
222
223
224
225 /*-----------------------  inlines  -------------------------*/
226
227 inline
228 bool MathMacro::setArgumentIdx(int i)
229 {
230     if (i>= 0 && i<nargs) {
231         idx = i;
232         return true;
233     } else
234       return false;
235 }
236
237 inline
238 int  MathMacro::getArgumentIdx() 
239
240     return idx; 
241 }
242
243 inline
244 int  MathMacro::getMaxArgumentIdx() 
245
246     return nargs-1; 
247
248
249
250 inline
251 LyxArrayBase *MathMacro::GetData() 
252
253     return args[idx].array; 
254
255
256
257 inline
258 void MathMacro::SetData(LyxArrayBase *a)
259 {
260    args[idx].array = a;
261 }
262
263
264 inline 
265 MathMacro *MathMacroTable::getMacro(char const* name) const
266  {
267      MathMacroTemplate *mt = getTemplate(name);
268      return (mt) ? new MathMacro(mt): 0;
269  }
270
271
272 #endif