]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
more mathed cleanup
[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 #include FORMS_H_LOCATION
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "LString.h"
26 #include "math_macro.h"
27 #include "array.h"
28 #include "math_iter.h"
29 #include "math_inset.h"
30 #include "math_accentinset.h"
31 #include "math_deliminset.h"
32 #include "math_fracinset.h"
33 #include "math_rowst.h"
34 #include "support/lstrings.h"
35 #include "debug.h"
36 #include "mathed/support.h"
37 #include "math_macrotemplate.h"
38 #include "macro_support.h"
39
40 using std::ostream;
41 using std::endl;
42
43 ostream & operator<<(ostream & o, MathedTextCodes mtc)
44 {
45         return o << int(mtc);
46 }
47
48
49 //extern int mathed_string_width(short type, int style, string const & s);
50 //extern int mathed_string_height(short, int, string const &, int &, int &);
51
52 MathMacro::MathMacro(MathMacroTemplate * t): 
53         MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
54 {
55         nargs = tmplate->getNoArgs();
56         tcode = tmplate->getTCode();
57         args_.resize(nargs);
58         for (int i = 0; i < nargs; ++i) {
59                 args_[i].row = 0;
60         }
61         idx = 0;
62         SetName(tmplate->GetName());
63 }
64
65
66 MathMacro::MathMacro(MathMacro * m): 
67         MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
68 {
69         tmplate = m->tmplate;
70         nargs = tmplate->getNoArgs();
71         tcode = tmplate->getTCode();
72         args_.resize(nargs);
73         idx = 0;
74         SetName(tmplate->GetName());
75         for (int i = 0; i < tmplate->nargs; ++i) {
76                 m->setArgumentIdx(i);
77                 MathedIter it(m->GetData());
78                 args_[i].row   = m->args_[i].row;
79                 args_[i].array = it.Copy();
80         }
81 }
82
83 MathMacro::~MathMacro()
84 {
85         for (idx = 0; idx < nargs; ++idx) {
86                 MathedIter it(args_[idx].array);
87                 it.Clear();
88                 delete args_[idx].row;
89         }
90 }
91
92
93 MathedInset * MathMacro::Clone()
94 {
95         return new MathMacro(this);
96 }
97
98
99 void MathMacro::Metrics()
100 {
101         if (nargs > 0)
102                 tmplate->update(this);
103         tmplate->SetStyle(size);
104         tmplate->Metrics();
105         width = tmplate->Width();
106         ascent = tmplate->Ascent();
107         descent = tmplate->Descent();
108 }
109
110
111 void MathMacro::draw(Painter & pain, int x, int y)
112 {
113         xo = x;  yo = y;
114         Metrics();
115         tmplate->update(this);
116         tmplate->SetStyle(size);
117         tmplate->draw(pain, x, y);
118         for (int i = 0; i < nargs; ++i) {
119                 tmplate->GetMacroXY(i, args_[i].x, args_[i].y);
120         }
121 }
122
123
124 bool MathMacro::setArgumentIdx(int i)
125 {
126         if (i >= 0 && i < nargs) {
127                 idx = i;
128                 return true;
129         } else
130                 return false;
131 }
132
133
134 int MathMacro::getArgumentIdx() const 
135
136         return idx; 
137 }
138
139
140 int MathMacro::getMaxArgumentIdx() const 
141
142         return nargs - 1; 
143
144
145
146 MathedArray * MathMacro::GetData() 
147
148         return args_[idx].array; 
149
150
151
152 int MathMacro::GetColumns() const
153 {
154         return tmplate->getMacroPar(idx)->GetColumns();
155 }
156
157
158 void MathMacro::GetXY(int & x, int & y) const
159 {
160         x = args_[idx].x;  y = args_[idx].y;
161 }
162
163
164 bool MathMacro::Permit(short f) const
165 {
166         return (nargs > 0) ?
167                 tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
168 }
169
170
171 void MathMacro::SetFocus(int x, int y)
172 {
173         tmplate->update(this);
174         tmplate->SetMacroFocus(idx, x, y);
175 }
176
177
178 void MathMacro::SetData(MathedArray * a)
179 {
180         args_[idx].array = a;
181 }
182
183
184
185
186 MathedRowSt * MathMacro::getRowSt() const
187 {
188         return args_[idx].row;
189 }
190
191
192 MathedTextCodes MathMacro::getTCode() const
193 {
194         return tcode;
195 }
196         
197
198 void MathMacro::Write(ostream & os, bool fragile)
199 {
200         if (tmplate->flags & MMF_Exp) {
201                 lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
202                                       << ' ' << MMF_Exp << endl; 
203                 tmplate->update(this);
204                 tmplate->Write(os, fragile);
205         } else {
206                 if (tmplate->flags & MMF_Env) {
207                         os << "\\begin{"
208                            << name
209                            << "} ";
210                 } else {
211                         os << '\\' << name;
212                 }
213 //      if (options) { 
214 //        file += '[';
215 //        file += options;
216 //        file += ']';
217 //      }
218         
219                 if (!(tmplate->flags & MMF_Env) && nargs > 0) 
220                         os << '{';
221         
222                 for (int i = 0; i < nargs; ++i) {
223                         array = args_[i].array;
224                         MathParInset::Write(os, fragile);
225                         if (i < nargs - 1)  
226                                 os << "}{";
227                 }   
228                 if (tmplate->flags & MMF_Env) {
229                         os << "\\end{"
230                            << name
231                            << '}';
232                 } else {
233                         if (nargs > 0) 
234                                 os << '}';
235                         else
236                                 os << ' ';
237                 }
238         }
239 }