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