]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
fix the matrix reading other small 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
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 const & m)
62         : MathParInset(LM_ST_TEXT, m.GetName(), LM_OT_MACRO),
63           tmplate_(m.tmplate_), idx_(0)
64 {
65         nargs_ = tmplate_->getNoArgs();
66         tcode_ = tmplate_->getTCode();
67         SetName(tmplate_->GetName());
68
69         std::vector<MacroArgumentBase>::const_iterator cit = m.args_.begin();
70         std::vector<MacroArgumentBase>::const_iterator end = m.args_.end();
71         
72         //args_.resize(nargs_);
73         //for (int i = 0; i < tmplate_->getNoArgs(); ++i) {
74         //      args_[i].row   = m->args_[i].row;
75         //      args_[i].array = m->args_[i].array;
76         //}
77         for (; cit != end; ++cit) {
78                 args_.push_back(*cit);
79         }
80 }
81
82
83 MathedInset * MathMacro::Clone()
84 {
85         return new MathMacro(*this);
86 }
87
88
89 void MathMacro::Metrics()
90 {
91         if (nargs_ > 0)
92                 tmplate_->update(this);
93         tmplate_->SetStyle(size());
94         tmplate_->Metrics();
95         width = tmplate_->Width();
96         ascent = tmplate_->Ascent();
97         descent = tmplate_->Descent();
98 }
99
100
101 void MathMacro::draw(Painter & pain, int x, int y)
102 {
103         xo(x);
104         yo(y);
105         Metrics();
106         tmplate_->update(this);
107         tmplate_->SetStyle(size());
108         tmplate_->draw(pain, x, y);
109         for (int i = 0; i < nargs_; ++i) {
110                 tmplate_->GetMacroXY(i, args_[i].x, args_[i].y);
111         }
112 }
113
114
115 bool MathMacro::setArgumentIdx(int i)
116 {
117         if (i >= 0 && i < nargs_) {
118                 idx_ = i;
119                 return true;
120         } else
121                 return false;
122 }
123
124
125 int MathMacro::getArgumentIdx() const 
126
127         return idx_;
128 }
129
130
131 int MathMacro::getMaxArgumentIdx() const 
132
133         return nargs_ - 1; 
134
135
136
137 MathedArray & MathMacro::GetData() 
138
139         return args_[idx_].array; 
140
141
142
143 MathedArray const & MathMacro::GetData() const
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) :
166                 MathParInset::Permit(f);
167 }
168
169
170 void MathMacro::SetFocus(int x, int y)
171 {
172         tmplate_->update(this);
173         tmplate_->SetMacroFocus(idx_, x, y);
174 }
175
176
177 void MathMacro::setData(MathedArray const & a)
178 {
179         args_[idx_].array = a;
180 }
181
182
183
184
185 MathedRowSt * MathMacro::getRowSt() const
186 {
187         return args_[idx_].row;
188 }
189
190
191 MathedTextCodes MathMacro::getTCode() const
192 {
193         return tcode_;
194 }
195         
196
197 void MathMacro::Write(ostream & os, bool fragile)
198 {
199         if (tmplate_->flags() & MMF_Exp) {
200                 lyxerr[Debug::MATHED] << "Expand " << tmplate_->flags()
201                                       << ' ' << MMF_Exp << endl; 
202                 tmplate_->update(this);
203                 tmplate_->Write(os, fragile);
204         } else {
205                 if (tmplate_->flags() & MMF_Env) {
206                         os << "\\begin{"
207                            << name
208                            << "} ";
209                 } else {
210                         os << '\\' << name;
211                 }
212 //      if (options) { 
213 //        file += '[';
214 //        file += options;
215 //        file += ']';
216 //      }
217         
218                 if (!(tmplate_->flags() & MMF_Env) && nargs_ > 0) 
219                         os << '{';
220         
221                 for (int i = 0; i < nargs_; ++i) {
222                         array = args_[i].array;
223                         MathParInset::Write(os, fragile);
224                         if (i < nargs_ - 1)  
225                                 os << "}{";
226                 }   
227                 if (tmplate_->flags() & MMF_Env) {
228                         os << "\\end{"
229                            << name
230                            << '}';
231                 } else {
232                         if (nargs_ > 0) 
233                                 os << '}';
234                         else
235                                 os << ' ';
236                 }
237         }
238 }