]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotemplate.C
more mathed changes, read the changelog
[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                 args_.resize(nargs_);
24                 for (int i = 0; i < nargs_; ++i) {
25                         args_[i].setNumber(i + 1);
26                 }
27         } else {
28                 tcode_ = LM_TC_INSET;
29                 // Here is  nargs != args_.size()
30                 //args = 0;
31         }
32 }
33
34
35 void  MathMacroTemplate::setTCode(MathedTextCodes t)
36 {
37         tcode_ = t;
38 }
39
40
41 MathedTextCodes MathMacroTemplate::getTCode() const
42 {
43         return tcode_;
44 }
45
46
47 int MathMacroTemplate::getNoArgs() const
48 {
49         return nargs_;
50 }
51
52
53 void MathMacroTemplate::setEditMode(bool ed)
54 {
55         if (ed) {
56                 edit_ = true;
57                 for (int i = 0; i < nargs_; ++i) {
58                         args_[i].setExpand(false);
59                 }
60         } else {
61                 edit_ = false;
62                 for (int i = 0; i < nargs_; ++i) {
63                         args_[i].setExpand(true);
64                 }
65         }
66 }
67
68
69 void MathMacroTemplate::draw(Painter & pain, int x, int y)
70 {
71         int x2;
72         int y2;
73         bool expnd = (nargs_ > 0) ? args_[0].getExpand() : false;
74         if (edit_) {
75                 for (int i = 0; i < nargs_; ++i) {
76                         args_[i].setExpand(false);
77                 }
78                 x2 = x;
79                 y2 = y;
80         } else {
81                 for (int i = 0; i < nargs_; ++i) {
82                         args_[i].setExpand(true);
83                 }
84                 x2 = xo();
85                 y2 = yo();
86         }
87         MathParInset::draw(pain, x, y);
88         xo(x2);
89         yo(y2);
90
91         for (int i = 0; i < nargs_; ++i) {
92                 args_[i].setExpand(expnd);
93         }
94 }
95
96
97 void MathMacroTemplate::Metrics()
98 {
99         bool const expnd = (nargs_ > 0) ? args_[0].getExpand() : false;
100     
101         if (edit_) {
102                 for (int i = 0; i < nargs_; ++i) {
103                         args_[i].setExpand(false);
104                 }
105         } else {
106                 for (int i = 0; i < nargs_; ++i) {
107                         args_[i].setExpand(true);
108                 }
109         }
110         MathParInset::Metrics();
111     
112         for (int i = 0; i < nargs_; ++i) {
113                 args_[i].setExpand(expnd);
114         }
115 }
116
117
118 void MathMacroTemplate::update(MathMacro * macro)
119 {
120         Assert(macro);
121         int const idx = macro->getArgumentIdx();
122         for (int i = 0; i < nargs_; ++i) {
123                         macro->setArgumentIdx(i);
124                         args_[i].setData(macro->GetData());
125                         MathedRowSt * row = macro->getRowSt();
126                         args_[i].setRowSt(row);
127         }       
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::GetMacroXY(int i, int & x, int & y) const
150 {
151         args_[i].GetXY(x, y);
152 }
153
154
155 MathParInset * MathMacroTemplate::getMacroPar(int i) const
156 {
157         if (i >= 0 && i < nargs_) 
158                 return const_cast<MathParInset *>
159                         (static_cast<MathParInset const *>(&args_[i]));
160         else 
161                 return 0;
162 }
163
164
165 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
166 {
167         for (int i = 0; i < nargs_; ++i) {
168                 if (args_[i].Inside(x, y)) {
169                         idx = i;
170                         break;
171                 }
172         }
173 }