]> git.lyx.org Git - features.git/blob - src/mathed/math_macro.h
the freespacing patch from Kayvan, draw the math empty delim with onoffdash, asure...
[features.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: 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(Painter &, int, int);
47     ///
48     void Metrics();
49         ///
50     MathedInset * Clone();
51         ///
52     void Write(ostream &);
53 #ifndef USE_OSTREAM_ONLY
54         ///
55     void Write(string &);
56 #endif
57         ///
58     bool setArgumentIdx(int);
59         ///
60     int  getArgumentIdx();
61         ///
62     int  getMaxArgumentIdx();
63         ///
64     int GetColumns();
65         ///
66     void GetXY(int &, int &) const;
67         ///
68     void SetFocus(int, int);
69         ///
70     LyxArrayBase * GetData();
71         ///
72     MathedRowSt * getRowSt() const { return args[idx].row; }
73         ///
74     void SetData(LyxArrayBase *);
75         ///
76     MathedTextCodes getTCode() { return tcode; }
77         ///
78     bool Permit(short);
79     
80 private:
81         ///
82         MathMacroTemplate * tmplate;
83         ///
84         struct MacroArgumentBase {
85                 /// Position of the macro
86                 int x, y;
87                 ///
88                 MathedRowSt * row;
89                 ///
90                 LyxArrayBase * array;
91                 ///
92                 MacroArgumentBase() { x = y = 0;  array = 0; row = 0; }
93         };
94         MacroArgumentBase * args;
95         ///
96         int idx;
97         ///
98         int nargs;
99         ///
100         MathedTextCodes tcode;
101         ///
102         friend class MathMacroTemplate;
103 };
104
105
106 /// An argument
107 class MathMacroArgument: public MathParInset {
108 public:
109     ///
110     MathMacroArgument() { expnd_mode = false; number = 1;  SetType(LM_OT_MACRO_ARG); }
111     ///
112     MathMacroArgument(int);
113     ///
114         ~MathMacroArgument() { lyxerr << "help, destroyme!" << endl; }
115     ///
116     MathedInset * Clone() { return this; }
117         ///
118     void Metrics();
119         ///
120     void draw(Painter &, int x, int baseline);
121         ///
122     void Write(ostream &);
123 #ifndef USE_OSTREAM_ONLY
124         ///
125     void Write(string &);
126 #endif
127     ///
128     void setNumber(int n) { number = n; }
129     /// Is expanded or not
130     void setExpand(bool e) { expnd_mode = e; }
131     /// Is expanded or not
132     bool getExpand() { return expnd_mode; }
133     
134 private:
135         ///
136     bool expnd_mode;
137         ///
138     int number;
139 };
140
141
142 /// This class contains the macro definition
143 class MathMacroTemplate: public MathParInset {
144 public:
145     /// A template constructor needs all the data
146     MathMacroTemplate(char const *, int na = 0, int f = 0);
147         ///
148     ~MathMacroTemplate();
149         ///
150     void draw(Painter &, int, int);
151         ///
152     void Metrics();
153         ///
154     void WriteDef(ostream &);
155 #ifndef USE_OSTREAM_ONLY
156         ///
157     void WriteDef(string &);
158 #endif
159     /// useful for special insets
160     void  setTCode(MathedTextCodes t) { tcode = t; }
161     ///
162     MathedTextCodes getTCode() { return tcode; }
163     /// 
164     void setArgument(LyxArrayBase *, int i= 0);
165     /// Number of arguments
166     int getNoArgs() { return nargs; }
167     ///
168     void GetMacroXY(int, int &, int &) const;
169     ///
170     MathParInset * getMacroPar(int) const;
171     ///
172     void SetMacroFocus(int &, int, int);
173         ///
174     void setEditMode(bool);
175
176     /// Replace the appropriate arguments with a specific macro's data
177     void update(MathMacro * m = 0);
178       
179 private:
180     ///
181     short flags;
182     ///
183     MathedTextCodes tcode;
184     ///
185     MathMacroArgument * args;
186     ///
187     int nargs;
188     ///
189     friend class MathMacro;
190 };
191      
192
193 ///
194 typedef MathMacro * MathMacroP;
195 ///
196 typedef MathMacroTemplate * MathMacroTemplateP;
197
198 ///
199 class MathMacroTable {
200 public:
201         ///
202     MathMacroTable(int);
203         ///
204     ~MathMacroTable();
205         ///
206     void addTemplate(MathMacroTemplate *);
207         ///
208     MathMacro * getMacro(char const *) const;
209         ///
210     MathMacroTemplate * getTemplate(char const *) const;
211         ///
212     void builtinMacros();
213         ///
214     static MathMacroTable mathMTable;
215         ///
216     static bool built;
217     
218 private:
219         ///
220     const int max_macros;
221         ///
222     int num_macros;
223         ///
224     MathMacroTemplateP * macro_table;
225 };
226
227
228
229 /*-----------------------  inlines  -------------------------*/
230
231 inline
232 bool MathMacro::setArgumentIdx(int i)
233 {
234     if (i >= 0 && i < nargs) {
235         idx = i;
236         return true;
237     } else
238       return false;
239 }
240
241 inline
242 int  MathMacro::getArgumentIdx() 
243
244     return idx; 
245 }
246
247 inline
248 int  MathMacro::getMaxArgumentIdx() 
249
250     return nargs - 1; 
251
252
253
254 inline
255 LyxArrayBase * MathMacro::GetData() 
256
257     return args[idx].array; 
258
259
260
261 inline
262 void MathMacro::SetData(LyxArrayBase * a)
263 {
264    args[idx].array = a;
265 }
266
267
268 inline 
269 MathMacro * MathMacroTable::getMacro(char const * name) const
270 {
271         MathMacroTemplate * mt = getTemplate(name);
272         return (mt) ? new MathMacro(mt): 0;
273 }
274
275
276 #endif