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