]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.h
use the new sstream return non-pods as const, use string instead of char * in a lot...
[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() {
109             expnd_mode = false;
110             number = 1;
111             SetType(LM_OT_MACRO_ARG);
112     }
113     ///
114     explicit
115     MathMacroArgument(int);
116     ///
117     ~MathMacroArgument() { lyxerr << "help, destroyme!" << std::endl; }
118     ///
119     MathedInset * Clone() { return this; }
120     ///
121     void Metrics();
122     ///
123     void draw(Painter &, int x, int baseline);
124     ///
125     void Write(std::ostream &, bool fragile);
126     ///
127     void setNumber(int n) { number = n; }
128     /// Is expanded or not
129     void setExpand(bool e) { expnd_mode = e; }
130     /// Is expanded or not
131     bool getExpand() { return expnd_mode; }
132 private:
133     ///
134     bool expnd_mode;
135     ///
136     int number;
137 };
138
139
140 /// This class contains the macro definition
141 class MathMacroTemplate: public MathParInset {
142 public:
143     /// A template constructor needs all the data
144     explicit
145     MathMacroTemplate(char const *, int na = 0, int f = 0);
146     ///
147     ~MathMacroTemplate();
148     ///
149     void draw(Painter &, int, int);
150     ///
151     void Metrics();
152     ///
153     void WriteDef(std::ostream &, bool fragile);
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 public:
196     ///
197     explicit
198     MathMacroTable(int);
199     ///
200     ~MathMacroTable();
201     ///
202     void addTemplate(MathMacroTemplate *);
203     ///
204     MathMacro * getMacro(string const &) const;
205     ///
206     MathMacroTemplate * getTemplate(string 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(string const & name) const
266 {
267         MathMacroTemplate * mt = getTemplate(name);
268         return (mt) ? new MathMacro(mt): 0;
269 }
270
271
272 #endif