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