]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotemplate.C
fix macro, small cleanup
[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         }
63         else {
64                 flags_ &= ~MMF_Edit;
65                 for (int i = 0; i < nargs_; ++i) {
66                         args_[i].setExpand(true);
67                 }
68         }
69 }
70
71
72 void MathMacroTemplate::draw(Painter & pain, int x, int y)
73 {
74         int x2;
75         int y2;
76         bool expnd = (nargs_ > 0) ? args_[0].getExpand(): false;
77         if (flags_ & MMF_Edit) {
78                 for (int i = 0; i < nargs_; ++i) {
79                         args_[i].setExpand(false);
80                 }
81                 x2 = x;
82                 y2 = y;
83         } else {
84                 for (int i = 0; i < nargs_; ++i) {
85                         args_[i].setExpand(true);
86                 }
87                 x2 = xo();
88                 y2 = yo();
89         }
90         MathParInset::draw(pain, x, y);
91         xo(x2);
92         yo(y2);
93
94         for (int i = 0; i < nargs_; ++i) {
95                 args_[i].setExpand(expnd);
96         }
97 }
98
99
100 void MathMacroTemplate::Metrics()
101 {
102         bool expnd = (nargs_ > 0) ? args_[0].getExpand(): false;
103     
104         if (flags_ & MMF_Edit) {
105                 for (int i = 0; i < nargs_; ++i) {
106                         args_[i].setExpand(false);
107                 }
108         } else {
109                 for (int i = 0; i < nargs_; ++i) {
110                         args_[i].setExpand(true);
111                 }
112         }
113         MathParInset::Metrics();
114     
115         for (int i = 0; i < nargs_; ++i) {
116                 args_[i].setExpand(expnd);
117         }
118 }
119
120
121 void MathMacroTemplate::update(MathMacro * macro)
122 {
123         int idx = (macro) ? macro->getArgumentIdx() : 0;
124         for (int i = 0; i < nargs_; ++i) {
125                 if (macro) {
126                         macro->setArgumentIdx(i);
127                         args_[i].setData(macro->GetData());
128                         MathedRowSt * row = macro->getRowSt();
129                         args_[i].setRowSt(row);
130                 }
131         }       
132         if (macro)
133                 macro->setArgumentIdx(idx);
134 }
135
136
137 void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
138 {
139         os << "\n\\newcommand{\\" << name << "}";
140
141         if (nargs_ > 0 ) 
142                 os << "[" << nargs_ << "]";
143
144         os << "{";
145
146         for (int i = 0; i < nargs_; ++i) {
147                 args_[i].setExpand(false);
148         }        
149         Write(os, fragile);
150         os << "}\n";
151 }
152
153
154 void MathMacroTemplate::setArgument(MathedArray * a, int i)
155 {
156         args_[i].setData(*a);
157 }
158
159
160 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
161 {
162         args_[i].GetXY(x, y);
163 }
164
165
166 MathParInset * MathMacroTemplate::getMacroPar(int i) const
167 {
168         if (i >= 0 && i < nargs_) 
169                 return const_cast<MathParInset *>
170                         (static_cast<MathParInset const *>(&args_[i]));
171         else 
172                 return 0;
173 }
174
175
176 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
177 {
178         for (int i = 0; i < nargs_; ++i) {
179                 if (args_[i].Inside(x, y)) {
180                         idx = i;
181                         break;
182                 }
183         }
184 }