]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
Use references instead of pointers where possible
[lyx.git] / src / mathed / math_macro.C
1 /*
2  *  File:        math_macro.C
3  *  Purpose:     Implementation of macro class for mathed 
4  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
5  *  Created:     November 1996
6  *  Description: WYSIWYG math macros
7  *
8  *  Dependencies: Math
9  *
10  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
11  *
12  *  Version: 0.2, Math & Lyx project.
13  *
14  *  This code is under the GNU General Public Licence version 2 or later.
15  */
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "math_macro.h"
22 #include "array.h"
23 #include "support/lstrings.h"
24 #include "support/LAssert.h"
25 #include "debug.h"
26 #include "mathed/support.h"
27 #include "mathed/math_cursor.h"
28 #include "math_macrotable.h"
29 #include "math_macrotemplate.h"
30 #include "Painter.h"
31 #include "LaTeXFeatures.h"
32
33 using std::endl;
34
35 MathMacro::MathMacro(string const & name)
36         : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
37                 tmplate_(MathMacroTable::provide(name))
38 {}
39
40
41 MathMacro::MathMacro(MathMacro const & m)
42         : MathNestInset(m),
43                 tmplate_(m.tmplate_) // don't copy 'expanded_'!
44 {}
45
46
47
48 MathInset * MathMacro::clone() const
49 {
50         return new MathMacro(*this);
51 }
52
53
54 const char * MathMacro::name() const
55 {
56         return tmplate_->asMacroTemplate()->name().c_str();
57 }
58
59
60 void MathMacro::metrics(MathStyles st) const
61 {
62         if (mathcursor && mathcursor->isInside(this)) {
63                 expanded_ = tmplate_->xcell(0);
64                 expanded_.metrics(st);
65                 size_    = st;
66                 width_   = expanded_.width()   + 4;
67                 ascent_  = expanded_.ascent()  + 2;
68                 descent_ = expanded_.descent() + 2;
69
70                 width_ +=  mathed_string_width(LM_TC_TEXTRM, size_, name()) + 10;
71
72                 int lasc;
73                 int ldes;
74                 int lwid;
75                 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
76
77                 for (idx_type i = 0; i < nargs(); ++i) {
78                         MathXArray const & c = xcell(i);
79                         c.metrics(st);
80                         width_    = std::max(width_, c.width() + lwid);
81                         descent_ += std::max(c.ascent(),  lasc) + 5;
82                         descent_ += std::max(c.descent(), ldes) + 5;
83                 }
84         } else {
85                 expanded_ = tmplate_->xcell(0);
86                 expanded_.data_.substitute(*this);
87                 expanded_.metrics(st);
88                 size_    = st;
89                 width_   = expanded_.width()   + 6;
90                 ascent_  = expanded_.ascent()  + 3;
91                 descent_ = expanded_.descent() + 3;
92         }
93 }
94
95
96 void MathMacro::draw(Painter & pain, int x, int y) const
97 {
98         xo(x);
99         yo(y);
100
101         metrics(size());
102
103         //LColor::color col;
104
105         if (mathcursor && mathcursor->isInside(this)) {
106
107                 int h = y - ascent() + 2 + expanded_.ascent();
108                 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name());
109
110                 int const w = mathed_string_width(LM_TC_TEXTRM, size(), name());
111                 expanded_.draw(pain, x + w + 12, h);
112                 h += expanded_.descent();
113
114                 int lasc;
115                 int ldes;
116                 int lwid;
117                 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
118
119                 for (idx_type i = 0; i < nargs(); ++i) {
120                         MathXArray const & c = xcell(i);
121                         h += std::max(c.ascent(), lasc) + 5;
122                         c.draw(pain, x + lwid, h);
123                         char str[] = "#1:";
124                         str[1] += static_cast<char>(i);
125                         drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
126                         h += std::max(c.descent(), ldes) + 5;
127                 }
128                 //col = LColor::red;
129         } else {
130                 expanded_.draw(pain, x + 3, y);
131                 //col = LColor::black;
132         }
133
134         //if (nargs() > 0)
135         //      pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
136 }
137
138
139 void MathMacro::dump() const
140 {
141         MathMacroTable::dump();
142         lyxerr << "\n macro: '" << this << "'\n";
143         lyxerr << " name: '" << name() << "'\n";
144         lyxerr << " template: '"; tmplate_->write(lyxerr, false); lyxerr << "'\n";
145         lyxerr << endl;
146 }
147
148
149 void MathMacro::write(std::ostream & os, bool fragile) const
150 {
151         os << '\\' << name();
152         for (idx_type i = 0; i < nargs(); ++i) {
153                 os << '{';
154                 cell(i).write(os, fragile);
155                 os << '}';
156         }
157         if (nargs() == 0) 
158                 os << ' ';
159 }
160
161
162 void MathMacro::writeNormal(std::ostream & os) const
163 {
164         os << "[macro " << name() << " ";
165         for (idx_type i = 0; i < nargs(); ++i) {
166                 cell(i).writeNormal(os);
167                 os << ' ';
168         }
169         os << "] ";
170 }
171
172
173 bool MathMacro::idxUp(idx_type & idx, pos_type & pos) const
174 {
175         return MathNestInset::idxLeft(idx, pos);
176 }
177
178
179 bool MathMacro::idxDown(idx_type & idx, pos_type & pos) const
180 {
181         return MathNestInset::idxRight(idx, pos);
182 }
183
184
185 bool MathMacro::idxLeft(idx_type &, pos_type &) const
186 {
187         return false;
188 }
189
190
191 bool MathMacro::idxRight(idx_type &, pos_type &) const
192 {
193         return false;
194 }
195
196
197 void MathMacro::validate(LaTeXFeatures & features) const
198 {
199         if (name() == "binom")
200                 features.binom = true;
201         //MathInset::validate(features);
202 }