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