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