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