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