]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotemplate.C
most of the so far unapplied stuff from porto including proper support for
[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 "frontends/Painter.h"
8 #include "debug.h"
9
10
11 MathMacroTemplate::MathMacroTemplate()
12         : MathNestInset(2), numargs_(0), name_()
13 {}
14
15
16 MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs)
17         : MathNestInset(2), numargs_(numargs), name_(nm)
18 {
19         if (numargs_ > 9)
20                 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
21                         << numargs_ << std::endl;
22 }
23
24
25 MathInset * MathMacroTemplate::clone() const
26 {
27         //lyxerr << "cloning MacroTemplate!\n";
28         return new MathMacroTemplate(*this);
29 }
30
31
32 int MathMacroTemplate::numargs() const
33 {
34         return numargs_;
35 }
36
37
38 void MathMacroTemplate::numargs(int numargs)
39 {
40         numargs_ = numargs;
41 }
42
43
44 string const & MathMacroTemplate::name() const
45 {
46         return name_;
47 }
48
49
50 void MathMacroTemplate::metrics(MathMetricsInfo & mi) const
51 {
52         xcell(0).metrics(mi);
53         xcell(1).metrics(mi);
54         width_   = xcell(0).width() + xcell(1).width() + 10;
55         ascent_  = std::max(xcell(0).ascent(),  xcell(1).ascent())  + 2;
56         descent_ = std::max(xcell(0).descent(), xcell(1).descent()) + 2;
57 }
58
59
60 void MathMacroTemplate::draw(MathPainterInfo & pi, int x, int y) const
61 {
62         int const w0 = xcell(0).width();
63         int const w1 = xcell(1).width();
64         xcell(0).draw(pi, x + 2, y + 1);
65         pi.pain.rectangle(x, y - ascent() + 1, w0 + 4, height(),
66                         LColor::blue);
67         xcell(1).draw(pi, x + 8 + w0, y + 1);
68         pi.pain.rectangle(x + w0 + 6 , y - ascent() + 1, w1 + 4,
69                         height(), LColor::blue);
70 }
71
72
73 void MathMacroTemplate::write(WriteStream & os) const
74 {
75         if (os.latex()) {
76                 os << "\n\\newcommand{\\" << name_.c_str() << '}';
77                 if (numargs_ > 0)
78                         os << '[' << numargs_ << ']';
79                 os << '{' << cell(0) << "}\n";
80         } else {
81                 // writing .lyx
82                 os << "\n\\newcommand{\\" << name_.c_str() << '}';
83                 if (numargs_ > 0)
84                         os << '[' << numargs_ << ']';
85                 os << '{' << cell(0) << '}';
86                 // write special .tex export only if necessary
87                 if (!cell(1).empty())
88                         os << "\n{" << cell(1) << '}';
89         }
90 }