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