]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotemplate.C
small up/down tweaking
[lyx.git] / src / mathed / math_macrotemplate.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_macrotemplate.h"
6 #include "math_mathmlstream.h"
7 #include "math_parser.h"
8 #include "frontends/Painter.h"
9 #include "debug.h"
10
11
12 MathMacroTemplate::MathMacroTemplate()
13         : MathNestInset(2), numargs_(0), name_()
14 {}
15
16
17 MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
18                 MathArray const & ar1, MathArray const & ar2)
19         : MathNestInset(2), numargs_(numargs), name_(nm)
20 {
21         if (numargs_ > 9)
22                 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
23                         << numargs_ << std::endl;
24         cell(0) = ar1;
25         cell(1) = ar2;
26 }
27
28
29
30 MathMacroTemplate::MathMacroTemplate(std::istream & is)
31         : MathNestInset(2), numargs_(0), name_()
32 {
33         MathArray ar;
34         mathed_parse_cell(ar, is);
35         if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
36                 lyxerr << "cannot read macro from '" << ar << "'\n";
37                 return;
38         }
39         operator=( *(ar[0]->asMacroTemplate()) );
40 }
41
42
43 MathInset * MathMacroTemplate::clone() const
44 {
45         //lyxerr << "cloning MacroTemplate!\n";
46         return new MathMacroTemplate(*this);
47 }
48
49
50 int MathMacroTemplate::numargs() const
51 {
52         return numargs_;
53 }
54
55
56 void MathMacroTemplate::numargs(int numargs)
57 {
58         numargs_ = numargs;
59 }
60
61
62 string MathMacroTemplate::name() const
63 {
64         return name_;
65 }
66
67
68 void MathMacroTemplate::metrics(MathMetricsInfo & mi) const
69 {
70         cell(0).metrics(mi);
71         cell(1).metrics(mi);
72         dim_.w = cell(0).width() + cell(1).width() + 10;
73         dim_.a = std::max(cell(0).ascent(),  cell(1).ascent())  + 2;
74         dim_.d = std::max(cell(0).descent(), cell(1).descent()) + 2;
75 }
76
77
78 void MathMacroTemplate::draw(MathPainterInfo & pi, int x, int y) const
79 {
80         int const w0 = cell(0).width();
81         int const w1 = cell(1).width();
82         cell(0).draw(pi, x + 2, y + 1);
83         pi.pain.rectangle(x, y - ascent() + 1, w0 + 4, height(),
84                         LColor::blue);
85         cell(1).draw(pi, x + 8 + w0, y + 1);
86         pi.pain.rectangle(x + w0 + 6 , y - ascent() + 1, w1 + 4,
87                         height(), LColor::blue);
88 }
89
90
91
92 void MathMacroTemplate::write(WriteStream & os) const
93 {
94         if (os.latex()) {
95                 os << "\n\\newcommand{\\" << name_.c_str() << '}';
96                 if (numargs_ > 0)
97                         os << '[' << numargs_ << ']';
98                 os << '{' << cell(0) << "}\n";
99         } else {
100                 // writing .lyx
101                 os << "\n\\newcommand{\\" << name_.c_str() << '}';
102                 if (numargs_ > 0)
103                         os << '[' << numargs_ << ']';
104                 os << '{' << cell(0) << '}';
105                 // write special .tex export only if necessary
106                 if (!cell(1).empty())
107                         os << "\n{" << cell(1) << '}';
108         }
109 }