]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
more mathed changes, read the changelog
[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(boost::shared_ptr<MathMacroTemplate> const & t)
48         : MathParInset(LM_ST_TEXT, "", LM_OT_MACRO),
49           tmplate_(t)
50 {
51         nargs_ = tmplate_->getNoArgs();
52         tcode_ = tmplate_->getTCode();
53         args_.resize(nargs_);
54         idx_ = 0;
55         SetName(tmplate_->GetName());
56 }
57
58
59 MathMacro::MathMacro(MathMacro const & m)
60         : MathParInset(LM_ST_TEXT, m.GetName(), LM_OT_MACRO),
61           tmplate_(m.tmplate_), idx_(0)
62 {
63         nargs_ = tmplate_->getNoArgs();
64         tcode_ = tmplate_->getTCode();
65         SetName(tmplate_->GetName());
66
67         std::vector<MacroArgumentBase>::const_iterator cit = m.args_.begin();
68         std::vector<MacroArgumentBase>::const_iterator end = m.args_.end();
69         
70         //args_.resize(nargs_);
71         //for (int i = 0; i < tmplate_->getNoArgs(); ++i) {
72         //      args_[i].row   = m->args_[i].row;
73         //      args_[i].array = m->args_[i].array;
74         //}
75         for (; cit != end; ++cit) {
76                 args_.push_back(*cit);
77         }
78 }
79
80
81 MathedInset * MathMacro::Clone()
82 {
83         return new MathMacro(*this);
84 }
85
86
87 void MathMacro::Metrics()
88 {
89         if (nargs_ > 0)
90                 tmplate_->update(this);
91         tmplate_->SetStyle(size());
92         tmplate_->Metrics();
93         width = tmplate_->Width();
94         ascent = tmplate_->Ascent();
95         descent = tmplate_->Descent();
96 }
97
98
99 void MathMacro::draw(Painter & pain, int x, int y)
100 {
101         xo(x);
102         yo(y);
103         Metrics();
104         tmplate_->SetStyle(size());
105         tmplate_->draw(pain, x, y);
106 }
107
108
109 bool MathMacro::setArgumentIdx(int i)
110 {
111         if (i >= 0 && i < nargs_) {
112                 idx_ = i;
113                 return true;
114         } else
115                 return false;
116 }
117
118
119 int MathMacro::getArgumentIdx() const 
120
121         return idx_;
122 }
123
124
125 int MathMacro::getMaxArgumentIdx() const 
126
127         return nargs_ - 1; 
128
129
130
131 MathedArray & MathMacro::GetData() 
132
133         return args_[idx_].array; 
134
135
136
137 MathedArray const & MathMacro::GetData() const
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 #if 0
152         x = args_[idx_].x_;
153         y = args_[idx_].y_;
154 #else
155         const_cast<MathMacro*>(this)->Metrics();
156         tmplate_->GetMacroXY(idx_, x, y);
157 #endif
158 }
159
160
161 bool MathMacro::Permit(short f) const
162 {
163         return (nargs_ > 0) ?
164                 tmplate_->getMacroPar(idx_)->Permit(f) :
165                 MathParInset::Permit(f);
166 }
167
168
169 void MathMacro::SetFocus(int x, int y)
170 {
171         Metrics();
172         tmplate_->SetMacroFocus(idx_, x, y);
173 }
174
175
176 void MathMacro::setData(MathedArray const & a)
177 {
178         args_[idx_].array = a;
179 }
180
181
182 MathedRowSt * MathMacro::getRowSt() const
183 {
184         return args_[idx_].row;
185 }
186
187
188 MathedTextCodes MathMacro::getTCode() const
189 {
190         return tcode_;
191 }
192         
193
194 void MathMacro::Write(ostream & os, bool fragile)
195 {
196         os << '\\' << name;
197
198         if (nargs_ > 0) {
199                 os << '{';
200                 
201                 for (int i = 0; i < nargs_; ++i) {
202                         array = args_[i].array;
203                         MathParInset::Write(os, fragile);
204                         if (i < nargs_ - 1)  
205                                 os << "}{";
206                 }
207                 os << '}';
208         } else
209                 os << ' ';
210 }