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