]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotemplate.C
mathed compilation fixes
[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 #include "support/LOstream.h"
7
8 using std::ostream;
9
10
11 void  MathMacroTemplate::setTCode(MathedTextCodes t)
12 {
13         tcode = t;
14 }
15
16
17 MathedTextCodes MathMacroTemplate::getTCode() const
18 {
19         return tcode;
20 }
21
22
23 int MathMacroTemplate::getNoArgs() const
24 {
25         return nargs;
26 }
27
28
29 MathMacroTemplate::MathMacroTemplate(string const & nm, int na, int flg):
30         MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
31         flags(flg), nargs(na)
32 {
33         if (nargs > 0) {
34                 tcode = LM_TC_ACTIVE_INSET;
35                 args_.resize(nargs);
36                 for (int i = 0; i < nargs; ++i) {
37                         args_[i].setNumber(i + 1);
38                 }
39         } else {
40                 tcode = LM_TC_INSET;
41                 // Here is  nargs != args_.size()
42                 //args = 0;
43         }
44 }
45
46
47 MathMacroTemplate::~MathMacroTemplate()
48 {
49         // prevent to delete already deleted objects
50         for (int i = 0; i < nargs; ++i) {
51                 args_[i].SetData(0);
52         }
53 }
54
55
56 void MathMacroTemplate::setEditMode(bool ed)
57 {
58         if (ed) {
59                 flags |= MMF_Edit;
60                 for (int i = 0; i < nargs; ++i) {
61                         args_[i].setExpand(false);
62                 }
63         }
64         else {
65                 flags &= ~MMF_Edit;
66                 for (int i = 0; i < nargs; ++i) {
67                         args_[i].setExpand(true);
68                 }
69         }
70 }
71
72
73 void MathMacroTemplate::draw(Painter & pain, int x, int y)
74 {
75         int x2, y2;
76         bool expnd = (nargs > 0) ? args_[0].getExpand(): false;
77         if (flags & MMF_Edit) {
78                 for (int i = 0; i < nargs; ++i) {
79                         args_[i].setExpand(false);
80                 }
81                 x2 = x; y2 = y;
82         } else {
83                 for (int i = 0; i < nargs; ++i) {
84                         args_[i].setExpand(true);
85                 }
86                 x2 = xo; y2 = yo;
87         }
88         MathParInset::draw(pain, x, y);
89         xo = x2; 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 expnd = (nargs > 0) ? args_[0].getExpand(): false;
100     
101         if (flags & MMF_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         int idx = (macro) ? macro->getArgumentIdx() : 0;
121         for (int i = 0; i < nargs; ++i) {
122                 if (macro) {
123                         macro->setArgumentIdx(i);
124                         args_[i].SetData(macro->GetData());
125                         MathedRowSt * row = macro->getRowSt();
126                         args_[i].setRowSt(row);
127                 }
128         }       
129         if (macro)
130                 macro->setArgumentIdx(idx);
131 }
132
133
134 void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
135 {
136         os << "\n\\newcommand{\\" << name << "}";
137
138         if (nargs > 0 ) 
139                 os << "[" << nargs << "]";
140
141         os << "{";
142
143         for (int i = 0; i < nargs; ++i) {
144                 args_[i].setExpand(false);
145         }        
146         Write(os, fragile); 
147         os << "}\n";
148 }
149
150
151 void MathMacroTemplate::setArgument(MathedArray * a, int i)
152 {
153         args_[i].SetData(a);
154 }
155
156
157 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
158 {
159         args_[i].GetXY(x, y);
160 }
161
162
163 MathParInset * MathMacroTemplate::getMacroPar(int i) const
164 {
165         if (i >= 0 && i < nargs) 
166                 return const_cast<MathParInset *>
167                         (static_cast<MathParInset const *>(&args_[i]));
168         else 
169                 return 0;
170 }
171
172
173 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
174 {
175         for (int i = 0; i < nargs; ++i) {
176                 if (args_[i].Inside(x, y)) {
177                         idx = i;
178                         break;
179                 }
180         }
181 }