]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
iterators for MathArray; cosmetics;
[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(MathMacroTemplate const & t)
36         : MathNestInset(t.numargs()), tmplate_(&t)
37 {}
38
39
40 MathMacro::MathMacro(MathMacro const & t)
41         : MathNestInset(t), tmplate_(t.tmplate_) // don't copy 'expanded_'!
42 {}
43
44
45
46 MathInset * MathMacro::clone() const
47 {
48         return new MathMacro(*this);
49 }
50
51 string const & MathMacro::name() const
52 {
53         return tmplate_->name();
54 }
55
56 void MathMacro::metrics(MathStyles st) const
57 {
58         if (mathcursor && mathcursor->isInside(this)) {
59                 expanded_ = tmplate_->xcell(0);
60                 expanded_.metrics(st);
61                 size_    = st;
62                 width_   = expanded_.width()   + 4;
63                 ascent_  = expanded_.ascent()  + 2;
64                 descent_ = expanded_.descent() + 2;
65
66                 width_ +=  mathed_string_width(LM_TC_TEXTRM, size_, name()) + 10;
67
68                 int lasc;
69                 int ldes;
70                 int lwid;
71                 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
72
73                 for (int i = 0; i < nargs(); ++i) {
74                         MathXArray const & c = xcell(i);
75                         c.metrics(st);
76                         width_    = std::max(width_, c.width() + lwid);
77                         descent_ += std::max(c.ascent(),  lasc) + 5;
78                         descent_ += std::max(c.descent(), ldes) + 5;
79                 }
80         } else {
81                 expanded_ = tmplate_->xcell(0);
82                 expanded_.data_.substitute(*this);
83                 expanded_.metrics(st);
84                 size_    = st;
85                 width_   = expanded_.width()   + 6;
86                 ascent_  = expanded_.ascent()  + 3;
87                 descent_ = expanded_.descent() + 3;
88         }
89 }
90
91
92 void MathMacro::draw(Painter & pain, int x, int y) const
93 {
94         xo(x);
95         yo(y);
96
97         metrics(size());
98
99         LColor::color col;
100
101         if (mathcursor && mathcursor->isInside(this)) {
102
103                 int h = y - ascent() + 2 + expanded_.ascent();
104                 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name());
105
106                 int const w = mathed_string_width(LM_TC_TEXTRM, size(), name());
107                 expanded_.draw(pain, x + w + 12, h);
108                 h += expanded_.descent();
109
110                 int lasc;
111                 int ldes;
112                 int lwid;
113                 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
114
115                 for (int i = 0; i < nargs(); ++i) {
116                         MathXArray const & c = xcell(i);
117                         h += std::max(c.ascent(), lasc) + 5;
118                         c.draw(pain, x + lwid, h);
119                         char str[] = "#1:";
120                         str[1] += i;
121                         drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
122                         h += std::max(c.descent(), ldes) + 5;
123                 }
124                 col = LColor::red;
125         } else {
126                 expanded_.draw(pain, x + 3, y);
127                 col = LColor::black;
128         }
129
130         if (nargs() > 0)
131                 pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
132 }
133
134
135 void MathMacro::dump(std::ostream & os) const
136 {
137         MathMacroTable::dump();
138         os << "\n macro: '" << this << "'\n";
139         os << " name: '" << name() << "'\n";
140         os << " template: '" << tmplate_ << "'\n";
141         os << " template: '" << *tmplate_ << "'\n";
142         os << endl;
143 }
144
145 void MathMacro::write(std::ostream & os, bool fragile) const
146 {
147         os << '\\' << name();
148         for (int i = 0; i < nargs(); ++i) {
149                 os << '{';
150                 cell(i).write(os, fragile);
151                 os << '}';
152         }
153         if (nargs() == 0) 
154                 os << ' ';
155 }
156
157
158 void MathMacro::writeNormal(std::ostream & os) const
159 {
160         os << "[macro " << name() << " ";
161         for (int i = 0; i < nargs(); ++i) {
162                 cell(i).writeNormal(os);
163                 os << ' ';
164         }
165         os << "] ";
166 }
167
168
169 bool MathMacro::idxUp(int & idx, int & pos) const
170 {
171         return MathNestInset::idxLeft(idx, pos);
172 }
173
174
175 bool MathMacro::idxDown(int & idx, int & pos) const
176 {
177         return MathNestInset::idxRight(idx, pos);
178 }
179
180
181 bool MathMacro::idxLeft(int &, int &) const
182 {
183         return false;
184 }
185
186
187 bool MathMacro::idxRight(int &, int &) const
188 {
189         return false;
190 }
191
192
193 void MathMacro::validate(LaTeXFeatures & features) const
194 {
195         if (name() == "binom")
196                 features.binom = true;
197         //MathInset::validate(features);
198 }