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