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