]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
some (yet unfinished) up/down work
[lyx.git] / src / mathed / math_macro.C
1 /**
2  * \file math_macro.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "math_macro.h"
15 #include "math_support.h"
16 #include "math_extern.h"
17 #include "math_mathmlstream.h"
18
19 #include "buffer.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "BufferView.h"
23 #include "LaTeXFeatures.h"
24 #include "frontends/Painter.h"
25
26 using std::string;
27 using std::max;
28 using std::auto_ptr;
29 using std::endl;
30
31
32 MathMacro::MathMacro(string const & name, int numargs)
33         : MathNestInset(numargs), name_(name)
34 {}
35
36
37 auto_ptr<InsetBase> MathMacro::doClone() const
38 {
39         return auto_ptr<InsetBase>(new MathMacro(*this));
40 }
41
42
43 string MathMacro::name() const
44 {
45         return name_;
46 }
47
48
49 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
50 {
51         if (!MacroTable::globalMacros().has(name())) {
52                 mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
53         } else if (editing(mi.base.bv)) {
54                 asArray(MacroTable::globalMacros().get(name()).def(), tmpl_);
55                 LyXFont font = mi.base.font;
56                 augmentFont(font, "lyxtex");
57                 tmpl_.metrics(mi, dim);
58                 dim.wid += mathed_string_width(font, name()) + 10;
59                 int ww = mathed_string_width(font, "#1: ");
60                 for (idx_type i = 0; i < nargs(); ++i) {
61                         MathArray const & c = cell(i);
62                         c.metrics(mi);
63                         dim.wid  = max(dim.wid, c.width() + ww);
64                         dim.des += c.height() + 10;
65                 }
66         } else {
67                 MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
68                 expanded_.metrics(mi, dim);
69         }
70         metricsMarkers2(dim);
71         dim_ = dim;
72 }
73
74
75 void MathMacro::draw(PainterInfo & pi, int x, int y) const
76 {
77         if (!MacroTable::globalMacros().has(name())) {
78                 drawStrRed(pi, x, y, "Unknown: " + name());
79         } else if (editing(pi.base.bv)) {
80                 LyXFont font = pi.base.font;
81                 augmentFont(font, "lyxtex");
82                 int h = y - dim_.ascent() + 2 + tmpl_.ascent();
83                 pi.pain.text(x + 3, h, name(), font);
84                 int const w = mathed_string_width(font, name());
85                 tmpl_.draw(pi, x + w + 12, h);
86                 h += tmpl_.descent();
87                 Dimension ldim;
88                 mathed_string_dim(font, "#1: ", ldim);
89                 for (idx_type i = 0; i < nargs(); ++i) {
90                         MathArray const & c = cell(i);
91                         h += max(c.ascent(), ldim.asc) + 5;
92                         c.draw(pi, x + ldim.wid, h);
93                         char str[] = "#1:";
94                         str[1] += static_cast<char>(i);
95                         pi.pain.text(x + 3, h, str, font);
96                         h += max(c.descent(), ldim.des) + 5;
97                 }
98         } else {
99                 expanded_.draw(pi, x, y);
100         }
101         drawMarkers2(pi, x, y);
102 }
103
104
105 void MathMacro::validate(LaTeXFeatures & features) const
106 {
107         if (name() == "binom" || name() == "mathcircumflex")
108                 features.require(name());
109 }
110
111
112 void MathMacro::maple(MapleStream & os) const
113 {
114         updateExpansion();
115         ::maple(expanded_, os);
116 }
117
118
119 void MathMacro::mathmlize(MathMLStream & os) const
120 {
121         updateExpansion();
122         ::mathmlize(expanded_, os);
123 }
124
125
126 void MathMacro::octave(OctaveStream & os) const
127 {
128         updateExpansion();
129         ::octave(expanded_, os);
130 }
131
132
133 void MathMacro::updateExpansion() const
134 {
135         //expanded_.substitute(*this);
136 }
137
138
139 void MathMacro::infoize(std::ostream & os) const
140 {
141         os << "Macro: " << name();
142 }
143
144
145 void MathMacro::infoize2(std::ostream & os) const
146 {
147         os << "Macro: " << name();
148
149 }