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