]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
mathed33.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
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                 args_[i].row   = m->args_[i].row;
73                 args_[i].array = m->GetData();
74         }
75 }
76
77
78 MathMacro::~MathMacro()
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 int MathMacro::GetColumns() const
144 {
145         return tmplate_->getMacroPar(idx_)->GetColumns();
146 }
147
148
149 void MathMacro::GetXY(int & x, int & y) const
150 {
151         x = args_[idx_].x;
152         y = args_[idx_].y;
153 }
154
155
156 bool MathMacro::Permit(short f) const
157 {
158         return (nargs_ > 0) ?
159                 tmplate_->getMacroPar(idx_)->Permit(f) : MathParInset::Permit(f);
160 }
161
162
163 void MathMacro::SetFocus(int x, int y)
164 {
165         tmplate_->update(this);
166         tmplate_->SetMacroFocus(idx_, x, y);
167 }
168
169
170 void MathMacro::setData(MathedArray const & a)
171 {
172         args_[idx_].array = a;
173 }
174
175
176
177
178 MathedRowSt * MathMacro::getRowSt() const
179 {
180         return args_[idx_].row;
181 }
182
183
184 MathedTextCodes MathMacro::getTCode() const
185 {
186         return tcode_;
187 }
188         
189
190 void MathMacro::Write(ostream & os, bool fragile)
191 {
192         if (tmplate_->flags() & MMF_Exp) {
193                 lyxerr[Debug::MATHED] << "Expand " << tmplate_->flags()
194                                       << ' ' << MMF_Exp << endl; 
195                 tmplate_->update(this);
196                 tmplate_->Write(os, fragile);
197         } else {
198                 if (tmplate_->flags() & MMF_Env) {
199                         os << "\\begin{"
200                            << name
201                            << "} ";
202                 } else {
203                         os << '\\' << name;
204                 }
205 //      if (options) { 
206 //        file += '[';
207 //        file += options;
208 //        file += ']';
209 //      }
210         
211                 if (!(tmplate_->flags() & MMF_Env) && nargs_ > 0) 
212                         os << '{';
213         
214                 for (int i = 0; i < nargs_; ++i) {
215                         array = args_[i].array;
216                         MathParInset::Write(os, fragile);
217                         if (i < nargs_ - 1)  
218                                 os << "}{";
219                 }   
220                 if (tmplate_->flags() & MMF_Env) {
221                         os << "\\end{"
222                            << name
223                            << '}';
224                 } else {
225                         if (nargs_ > 0) 
226                                 os << '}';
227                         else
228                                 os << ' ';
229                 }
230         }
231 }