]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotemplate.C
mathed37.diff
[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):
17         MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
18         edit_(false),
19         nargs_(na)
20 {
21         if (nargs_ > 0) {
22                 tcode_ = LM_TC_ACTIVE_INSET;
23                 for (int i = 0; i < nargs_; ++i) {
24                         args_.push_back(MathMacroArgument(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                 edit_ = true;
56                 for (int i = 0; i < nargs_; ++i) {
57                         args_[i].setExpand(false);
58                 }
59         } else {
60                 edit_ = false;
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 (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 const expnd = (nargs_ > 0) ? args_[0].getExpand() : false;
99     
100         if (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 const 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::GetMacroXY(int i, int & x, int & y) const
149 {
150         args_[i].GetXY(x, y);
151 }
152
153
154 MathParInset * MathMacroTemplate::getMacroPar(int i) const
155 {
156         if (i >= 0 && i < nargs_) 
157                 return const_cast<MathParInset *>
158                         (static_cast<MathParInset const *>(&args_[i]));
159         else 
160                 return 0;
161 }
162
163
164 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
165 {
166         for (int i = 0; i < nargs_; ++i) {
167                 if (args_[i].Inside(x, y)) {
168                         idx = i;
169                         break;
170                 }
171         }
172 }