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