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