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