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