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