]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
fix macro, 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 * 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 MathedInset * MathMacro::Clone()
79 {
80         return new MathMacro(this);
81 }
82
83
84 void MathMacro::Metrics()
85 {
86         if (nargs_ > 0)
87                 tmplate_->update(this);
88         tmplate_->SetStyle(size());
89         tmplate_->Metrics();
90         width = tmplate_->Width();
91         ascent = tmplate_->Ascent();
92         descent = tmplate_->Descent();
93 }
94
95
96 void MathMacro::draw(Painter & pain, int x, int y)
97 {
98         xo(x);
99         yo(y);
100         Metrics();
101         tmplate_->update(this);
102         tmplate_->SetStyle(size());
103         tmplate_->draw(pain, x, y);
104         for (int i = 0; i < nargs_; ++i) {
105                 tmplate_->GetMacroXY(i, args_[i].x, args_[i].y);
106         }
107 }
108
109
110 bool MathMacro::setArgumentIdx(int i)
111 {
112         if (i >= 0 && i < nargs_) {
113                 idx_ = i;
114                 return true;
115         } else
116                 return false;
117 }
118
119
120 int MathMacro::getArgumentIdx() const 
121
122         return idx_;
123 }
124
125
126 int MathMacro::getMaxArgumentIdx() const 
127
128         return nargs_ - 1; 
129
130
131
132 MathedArray & MathMacro::GetData() 
133
134         return args_[idx_].array; 
135
136
137
138 int MathMacro::GetColumns() const
139 {
140         return tmplate_->getMacroPar(idx_)->GetColumns();
141 }
142
143
144 void MathMacro::GetXY(int & x, int & y) const
145 {
146         x = args_[idx_].x;
147         y = args_[idx_].y;
148 }
149
150
151 bool MathMacro::Permit(short f) const
152 {
153         return (nargs_ > 0) ?
154                 tmplate_->getMacroPar(idx_)->Permit(f) : MathParInset::Permit(f);
155 }
156
157
158 void MathMacro::SetFocus(int x, int y)
159 {
160         tmplate_->update(this);
161         tmplate_->SetMacroFocus(idx_, x, y);
162 }
163
164
165 void MathMacro::setData(MathedArray const & a)
166 {
167         args_[idx_].array = a;
168 }
169
170
171
172
173 MathedRowSt * MathMacro::getRowSt() const
174 {
175         return args_[idx_].row;
176 }
177
178
179 MathedTextCodes MathMacro::getTCode() const
180 {
181         return tcode_;
182 }
183         
184
185 void MathMacro::Write(ostream & os, bool fragile)
186 {
187         if (tmplate_->flags() & MMF_Exp) {
188                 lyxerr[Debug::MATHED] << "Expand " << tmplate_->flags()
189                                       << ' ' << MMF_Exp << endl; 
190                 tmplate_->update(this);
191                 tmplate_->Write(os, fragile);
192         } else {
193                 if (tmplate_->flags() & MMF_Env) {
194                         os << "\\begin{"
195                            << name
196                            << "} ";
197                 } else {
198                         os << '\\' << name;
199                 }
200 //      if (options) { 
201 //        file += '[';
202 //        file += options;
203 //        file += ']';
204 //      }
205         
206                 if (!(tmplate_->flags() & MMF_Env) && nargs_ > 0) 
207                         os << '{';
208         
209                 for (int i = 0; i < nargs_; ++i) {
210                         array = args_[i].array;
211                         MathParInset::Write(os, fragile);
212                         if (i < nargs_ - 1)  
213                                 os << "}{";
214                 }   
215                 if (tmplate_->flags() & MMF_Env) {
216                         os << "\\end{"
217                            << name
218                            << '}';
219                 } else {
220                         if (nargs_ > 0) 
221                                 os << '}';
222                         else
223                                 os << ' ';
224                 }
225         }
226 }