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