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