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