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