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