]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotemplate.C
small mathed change add the find_if
[lyx.git] / src / mathed / math_macrotemplate.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_macrotemplate.h"
8 #include "math_macro.h"
9 #include "macro_support.h"
10 #include "support/LOstream.h"
11
12 using std::ostream;
13
14
15 MathMacroTemplate::MathMacroTemplate(string const & nm, int na, int flg):
16         MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
17         flags_(flg), nargs_(na)
18 {
19         if (nargs_ > 0) {
20                 tcode_ = LM_TC_ACTIVE_INSET;
21                 args_.resize(nargs_);
22                 for (int i = 0; i < nargs_; ++i) {
23                         args_[i].setNumber(i + 1);
24                 }
25         } else {
26                 tcode_ = LM_TC_INSET;
27                 // Here is  nargs != args_.size()
28                 //args = 0;
29         }
30 }
31
32
33 //MathMacroTemplate::~MathMacroTemplate()
34 //{}
35
36
37 void  MathMacroTemplate::setTCode(MathedTextCodes t)
38 {
39         tcode_ = t;
40 }
41
42
43 MathedTextCodes MathMacroTemplate::getTCode() const
44 {
45         return tcode_;
46 }
47
48
49 int MathMacroTemplate::getNoArgs() const
50 {
51         return nargs_;
52 }
53
54
55 void MathMacroTemplate::setEditMode(bool ed)
56 {
57         if (ed) {
58                 flags_ |= MMF_Edit;
59                 for (int i = 0; i < nargs_; ++i) {
60                         args_[i].setExpand(false);
61                 }
62         } else {
63                 flags_ &= ~MMF_Edit;
64                 for (int i = 0; i < nargs_; ++i) {
65                         args_[i].setExpand(true);
66                 }
67         }
68 }
69
70
71 void MathMacroTemplate::draw(Painter & pain, int x, int y)
72 {
73         int x2;
74         int y2;
75         bool expnd = (nargs_ > 0) ? args_[0].getExpand() : false;
76         if (flags_ & MMF_Edit) {
77                 for (int i = 0; i < nargs_; ++i) {
78                         args_[i].setExpand(false);
79                 }
80                 x2 = x;
81                 y2 = y;
82         } else {
83                 for (int i = 0; i < nargs_; ++i) {
84                         args_[i].setExpand(true);
85                 }
86                 x2 = xo();
87                 y2 = yo();
88         }
89         MathParInset::draw(pain, x, y);
90         xo(x2);
91         yo(y2);
92
93         for (int i = 0; i < nargs_; ++i) {
94                 args_[i].setExpand(expnd);
95         }
96 }
97
98
99 void MathMacroTemplate::Metrics()
100 {
101         bool expnd = (nargs_ > 0) ? args_[0].getExpand() : false;
102     
103         if (flags_ & MMF_Edit) {
104                 for (int i = 0; i < nargs_; ++i) {
105                         args_[i].setExpand(false);
106                 }
107         } else {
108                 for (int i = 0; i < nargs_; ++i) {
109                         args_[i].setExpand(true);
110                 }
111         }
112         MathParInset::Metrics();
113     
114         for (int i = 0; i < nargs_; ++i) {
115                 args_[i].setExpand(expnd);
116         }
117 }
118
119
120 void MathMacroTemplate::update(MathMacro * macro)
121 {
122         int idx = (macro) ? macro->getArgumentIdx() : 0;
123         for (int i = 0; i < nargs_; ++i) {
124                 if (macro) {
125                         macro->setArgumentIdx(i);
126                         args_[i].setData(macro->GetData());
127                         MathedRowSt * row = macro->getRowSt();
128                         args_[i].setRowSt(row);
129                 }
130         }       
131         if (macro)
132                 macro->setArgumentIdx(idx);
133 }
134
135
136 void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
137 {
138         os << "\n\\newcommand{\\" << name << "}";
139
140         if (nargs_ > 0 ) 
141                 os << "[" << nargs_ << "]";
142
143         os << "{";
144
145         for (int i = 0; i < nargs_; ++i) {
146                 args_[i].setExpand(false);
147         }        
148         Write(os, fragile);
149         os << "}\n";
150 }
151
152
153 void MathMacroTemplate::setArgument(MathedArray * a, int i)
154 {
155         args_[i].setData(*a);
156 }
157
158
159 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
160 {
161         args_[i].GetXY(x, y);
162 }
163
164
165 MathParInset * MathMacroTemplate::getMacroPar(int i) const
166 {
167         if (i >= 0 && i < nargs_) 
168                 return const_cast<MathParInset *>
169                         (static_cast<MathParInset const *>(&args_[i]));
170         else 
171                 return 0;
172 }
173
174
175 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
176 {
177         for (int i = 0; i < nargs_; ++i) {
178                 if (args_[i].Inside(x, y)) {
179                         idx = i;
180                         break;
181                 }
182         }
183 }