]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotemplate.C
mathed33.diff
[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
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         }
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         int idx = (macro) ? macro->getArgumentIdx() : 0;
120         for (int i = 0; i < nargs_; ++i) {
121                 if (macro) {
122                         macro->setArgumentIdx(i);
123                         args_[i].setData(macro->GetData());
124                         MathedRowSt * row = macro->getRowSt();
125                         args_[i].setRowSt(row);
126                 }
127         }       
128         if (macro)
129                 macro->setArgumentIdx(idx);
130 }
131
132
133 void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
134 {
135         os << "\n\\newcommand{\\" << name << "}";
136
137         if (nargs_ > 0 ) 
138                 os << "[" << nargs_ << "]";
139
140         os << "{";
141
142         for (int i = 0; i < nargs_; ++i) {
143                 args_[i].setExpand(false);
144         }        
145         Write(os, fragile);
146         os << "}\n";
147 }
148
149
150 void MathMacroTemplate::setArgument(MathedArray * a, int i)
151 {
152         args_[i].setData(*a);
153 }
154
155
156 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
157 {
158         args_[i].GetXY(x, y);
159 }
160
161
162 MathParInset * MathMacroTemplate::getMacroPar(int i) const
163 {
164         if (i >= 0 && i < nargs_) 
165                 return const_cast<MathParInset *>
166                         (static_cast<MathParInset const *>(&args_[i]));
167         else 
168                 return 0;
169 }
170
171
172 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
173 {
174         for (int i = 0; i < nargs_; ++i) {
175                 if (args_[i].Inside(x, y)) {
176                         idx = i;
177                         break;
178                 }
179         }
180 }