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