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