]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
small cleanup read 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         int const n = tmplate_->getNoArgs();
53         
54         tcode_ = tmplate_->getTCode();
55
56         for (int i = 0; i < n; ++i) {
57                 args_.push_back(MathMacroArgument(t->args_[i]));
58         }
59         //for (int i = 0; i < nargs_; ++i) {
60         //      MathMacroArgument * ma = new MathMacroArgument(*t->args_[i]);
61         //      args_.push_back(boost::shared_ptr<MathMacroArgument>(ma));
62         //}
63         
64         idx_ = 0;
65         SetName(tmplate_->GetName());
66 }
67
68
69 MathedInset * MathMacro::Clone()
70 {
71         return new MathMacro(*this);
72 }
73
74
75 void MathMacro::Metrics()
76 {
77         if (args_.size() > 0)
78                 tmplate_->update(*this);
79         tmplate_->SetStyle(size());
80         tmplate_->Metrics();
81         width = tmplate_->Width();
82         ascent = tmplate_->Ascent();
83         descent = tmplate_->Descent();
84 }
85
86
87 void MathMacro::draw(Painter & pain, int x, int y)
88 {
89         xo(x);
90         yo(y);
91         Metrics();
92         tmplate_->SetStyle(size());
93         tmplate_->draw(pain, x, y);
94 }
95
96
97 bool MathMacro::setArgumentIdx(int i)
98 {
99         if (i >= 0 && i < args_.size()) {
100                 idx_ = i;
101                 return true;
102         } else
103                 return false;
104 }
105
106
107 int MathMacro::getArgumentIdx() const 
108
109         return idx_;
110 }
111
112
113 int MathMacro::getMaxArgumentIdx() const 
114
115         return args_.size() - 1;
116
117
118
119 MathedArray & MathMacro::GetData() 
120
121         return args_[idx_].GetData(); 
122
123
124
125 MathedArray const & MathMacro::GetData() const
126
127         return args_[idx_].GetData(); 
128
129
130
131 int MathMacro::GetColumns() const
132 {
133         return tmplate_->getMacroPar(idx_)->GetColumns();
134 }
135
136
137 void MathMacro::GetXY(int & x, int & y) const
138 {
139         const_cast<MathMacro*>(this)->Metrics();
140         tmplate_->GetMacroXY(idx_, x, y);
141 }
142
143
144 bool MathMacro::Permit(short f) const
145 {
146         return (args_.size() > 0) ?
147                 tmplate_->getMacroPar(idx_)->Permit(f) :
148                 MathParInset::Permit(f);
149 }
150
151
152 void MathMacro::SetFocus(int x, int y)
153 {
154         Metrics();
155         tmplate_->SetMacroFocus(idx_, x, y);
156 }
157
158
159 void MathMacro::setData(MathedArray const & a)
160 {
161         args_[idx_].setData(a);
162 }
163
164
165 MathedTextCodes MathMacro::getTCode() const
166 {
167         return tcode_;
168 }
169         
170
171 void MathMacro::Write(ostream & os, bool fragile)
172 {
173         os << '\\' << name;
174
175         int const n = args_.size();
176         
177         if (n > 0) {
178                 os << '{';
179                 
180                 for (int i = 0; i < n; ++i) {
181                         array = args_[i].GetData();
182                         MathParInset::Write(os, fragile);
183                         if (i < n - 1)  
184                                 os << "}{";
185                 }
186                 os << '}';
187         } else
188                 os << ' ';
189 }
190
191
192 MathMacroArgument const & MathMacro::getArg(int i) const
193 {
194         return args_[i];
195 }
196 //boost::shared_ptr<MathMacroArgument> MathMacro::getArg(int i)
197 //{
198 //      return args_[i];
199 //}
200