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