]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
mathed65.diff
[lyx.git] / src / mathed / math_macro.C
1 // -*- C++ -*-
2 /*
3  *  File:        math_macro.C
4  *  Purpose:     Implementation 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
18 #include <config.h>
19
20 #ifdef __GNUG__
21 #pragma implementation
22 #endif
23
24 #include "math_macro.h"
25 #include "array.h"
26 #include "math_iter.h"
27 #include "math_inset.h"
28 #include "math_accentinset.h"
29 #include "math_deliminset.h"
30 #include "math_fracinset.h"
31 #include "math_rowst.h"
32 #include "support/lstrings.h"
33 #include "debug.h"
34 #include "mathed/support.h"
35 #include "math_macrotemplate.h"
36 #include "macro_support.h"
37 #include "Painter.h"
38
39 using namespace std;
40
41 ostream & operator<<(ostream & o, MathedTextCodes mtc)
42 {
43         return o << int(mtc);
44 }
45
46
47 MathMacro::MathMacro(MathMacroTemplate const & t)
48         : MathParInset(LM_ST_TEXT, t.GetName(), LM_OT_MACRO),
49           tmplate_(const_cast<MathMacroTemplate *>(&t)),
50                 args_(t.nargs()),
51                 idx_(t.nargs() ? 0 : -1)
52 {
53         array = tmplate_->GetData();
54         for (int i = 0; i < nargs(); ++i) 
55                 args_[i].reset(new MathParInset);
56 }
57
58
59 MathedInset * MathMacro::Clone()
60 {
61         return new MathMacro(*this);
62 }
63
64
65 void MathMacro::expand()
66 {
67         expanded_.reset(static_cast<MathParInset *>(tmplate_->Clone()));
68         expanded_->substitute(this);
69 }
70
71
72
73 MathParInset const * MathMacro::arg(int i) const
74 {
75         if (i < 0 || i >= nargs()) {
76                 lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
77                 lyx::Assert(0);
78                 return 0;
79         }
80         return i >= 0 ? args_[i].get() : static_cast<MathParInset const *>(this);
81 }
82
83
84 MathParInset * MathMacro::arg(int i) 
85 {
86         if (i < 0 || i >= nargs()) {
87                 lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
88                 lyx::Assert(0);
89                 return 0;
90         }
91         return i >= 0 ? args_[i].get() : static_cast<MathParInset *>(this);
92 }
93
94
95 MathMacroTemplate * MathMacro::tmplate() const
96 {
97         return const_cast<MathMacroTemplate *>(tmplate_);
98 }
99
100
101 extern bool is_mathcursor_inside(MathParInset *);
102
103 void MathMacro::Metrics()
104 {
105         if (is_mathcursor_inside(this)) {
106                 tmplate_->Metrics();
107                 width   = tmplate_->Width()   + 4;
108                 ascent  = tmplate_->Ascent()  + 2;
109                 descent = tmplate_->Descent() + 2;
110
111                 width +=  mathed_string_width(LM_TC_TEXTRM, size(), GetName()) + 10;
112
113                 for (int i = 0; i < nargs(); ++i) {
114                         MathParInset * p = arg(i);
115                         p->Metrics();
116                         if (p->Width() + 30 > width) 
117                                 width = p->Width() + 30;
118                         descent += p->Height() + 10;
119                 }
120         } else {
121                 expand();
122                 expanded_->Metrics();
123                 width   = expanded_->Width()   + 6;
124                 ascent  = expanded_->Ascent()  + 3;
125                 descent = expanded_->Descent() + 3;
126         }
127 }
128
129
130 void MathMacro::draw(Painter & pain, int x, int y)
131 {
132         LColor::color col;
133
134         if (is_mathcursor_inside(this)) {
135                 int h = y + Descent() - 2;
136                 for (int i = nargs() - 1; i >= 0; --i) {
137                         MathParInset * p = arg(i);
138                         h -= p->Descent() + 5;
139                         p->draw(pain, x + 30, h);
140                         char str[] = "#1:";
141                         str[1] += i;
142                         drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
143                         h -= p->Ascent() + 5;
144                 }
145
146                 h -= tmplate_->Descent();
147                 int w =  mathed_string_width(LM_TC_TEXTRM, size(), GetName());
148                 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, GetName());
149                 tmplate_->draw(pain, x + w + 12, h);
150
151                 col = LColor::red;
152         } else {
153                 expanded_->draw(pain, x + 3, y);
154                 col = LColor::black;
155         }
156
157         int w = Width();
158         int a = Ascent();
159         int h = Height();
160         pain.rectangle(x + 1, y - a + 1, w - 2, h - 2, col);
161 }
162
163
164 bool MathMacro::setArgumentIdx(int i)
165 {
166         if (i >= 0 && 0 < (nargs() - i)) {
167                 idx_ = i;
168                 return true;
169         } else
170                 return false;
171         idx_ = i;
172         return true;
173 }
174
175
176 int MathMacro::getArgumentIdx() const 
177
178         //lyxerr << "MathMacro::getArgumentIdx: res: " << idx_ << endl;
179         return idx_;
180 }
181
182
183 int MathMacro::getMaxArgumentIdx() const 
184
185         return nargs() - 1;
186 }
187
188
189
190 int MathMacro::nargs() const 
191
192         return args_.size();
193
194
195
196 MathedArray & MathMacro::GetData() 
197
198         //lyxerr << "MathMacro::GetData: " << *this << endl;
199         return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData(); 
200
201
202
203 MathedArray const & MathMacro::GetData() const
204
205         //lyxerr << "MathMacro::GetData: " << *this << endl;
206         return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData(); 
207
208
209
210 int MathMacro::GetColumns() const
211 {
212         return idx_ >= 0 ? arg(idx_)->GetColumns() : MathParInset::GetColumns();
213 }
214
215
216 void MathMacro::GetXY(int & x, int & y) const
217 {
218         if (idx_ >= 0)
219                 arg(idx_)->GetXY(x, y);
220         else
221                 MathParInset::GetXY(x, y);
222 }
223
224
225 bool MathMacro::Permit(short f) const
226 {
227         return idx_ >= 0 ? arg(idx_)->Permit(f) : MathParInset::Permit(f);
228 }
229
230
231 void MathMacro::SetFocus(int x, int y)
232 {
233         idx_ = -1;
234         for (int i = 0; i < nargs(); ++i) {
235                 if (arg(i)->Inside(x, y)) {
236                         idx_ = i;
237                         break;
238                 }
239         }
240 }
241
242 void MathMacro::setData(MathedArray const & a, int i)
243 {
244         arg(i)->setData(a);
245 }
246
247
248 void MathMacro::setData(MathedArray const & a)
249 {
250         if (idx_ >= 0)
251                 arg(idx_)->setData(a);
252         else
253                 array = a;
254 }
255
256
257 MathedTextCodes MathMacro::getTCode() const
258 {
259         return nargs() ? LM_TC_ACTIVE_INSET : LM_TC_INSET;
260         //return LM_TC_INSET;
261 }
262         
263 void MathMacro::dump(ostream & os) const
264 {
265         os << "\n macro: '" << this << "'\n";
266         os << " name: '" << name << "'\n";
267         os << " idx: '" << idx_ << "'\n";
268         os << " data: '" << array << "'\n";
269         os << " nargs: '" << nargs() << "'\n";
270         for (int i = 0; i < nargs(); ++i)
271                 os << "  " << arg(i) << ": " << arg(i)->GetData() << endl;
272         os << endl;
273 }
274
275 void MathMacro::Write(ostream & os, bool fragile)
276 {
277         os << '\\' << name;
278         for (int i = 0; i < nargs(); ++i) {
279                 os << '{';
280                 arg(i)->Write(os, fragile);
281                 os << '}';
282         }
283         if (nargs() == 0) 
284                 os << ' ';
285 }
286
287
288 void MathMacro::WriteNormal(ostream & os)
289 {
290         os << "{macro " << name << " ";
291         for (int i = 0; i < nargs(); ++i) 
292                 arg(i)->WriteNormal(os);
293         os << "} ";
294 }