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