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