]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotemplate.C
Ensure all #warning statements are wrapped by #ifdef WITH_WARNINGS.
[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 "math_support.h"
17
18 #include "cursor.h"
19 #include "debug.h"
20 #include "gettext.h"
21 #include "lyxlex.h"
22 #include "LColor.h"
23
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
26
27 #include "support/lstrings.h"
28
29 using lyx::support::bformat;
30
31 using std::string;
32 using std::auto_ptr;
33 using std::ostream;
34 using std::endl;
35
36
37 MathMacroTemplate::MathMacroTemplate()
38         : MathNestInset(2), numargs_(0), name_(), type_("newcommand")
39 {}
40
41
42 MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
43                 string const & type, MathArray const & ar1, MathArray const & ar2)
44         : MathNestInset(2), numargs_(numargs), name_(nm), type_(type)
45 {
46         if (numargs_ > 9)
47                 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
48                         << numargs_ << std::endl;
49         cell(0) = ar1;
50         cell(1) = ar2;
51 }
52
53
54 MathMacroTemplate::MathMacroTemplate(std::istream & is)
55         : MathNestInset(2), numargs_(0), name_()
56 {
57         MathArray ar;
58         mathed_parse_cell(ar, is);
59         if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
60                 lyxerr << "cannot read macro from '" << ar << "'" << endl;
61                 return;
62         }
63         operator=( *(ar[0]->asMacroTemplate()) );
64 }
65
66
67 auto_ptr<InsetBase> MathMacroTemplate::clone() const
68 {
69         return auto_ptr<InsetBase>(new MathMacroTemplate(*this));
70 }
71
72
73 void MathMacroTemplate::edit(LCursor & cur, bool)
74 {
75         lyxerr << "MathMacroTemplate: edit left/right" << endl;
76         cur.push(*this);
77 }
78
79
80 int MathMacroTemplate::numargs() const
81 {
82         return numargs_;
83 }
84
85
86 void MathMacroTemplate::numargs(int numargs)
87 {
88         numargs_ = numargs;
89 }
90
91
92 string MathMacroTemplate::name() const
93 {
94         return name_;
95 }
96
97
98 string MathMacroTemplate::prefix() const
99 {
100         return bformat(_(" Macro: %1$s: "), name_);
101 }
102
103
104 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
105 {
106         cell(0).metrics(mi);
107         cell(1).metrics(mi);
108         dim.wid = cell(0).width() + cell(1).width() + 20
109                 + font_metrics::width(prefix(), mi.base.font);
110         dim.asc = std::max(cell(0).ascent(),  cell(1).ascent())  + 7;
111         dim.des = std::max(cell(0).descent(), cell(1).descent()) + 7;
112         dim_ = dim;
113 }
114
115
116 void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
117 {
118         setPosCache(p, x, y);
119
120         // label
121         LyXFont font = p.base.font;
122         font.setColor(LColor::math);
123
124         PainterInfo pi(p.base.bv, p.pain);
125         pi.base.style = LM_ST_TEXT;
126         pi.base.font  = font;
127
128         int const a = y - dim_.asc + 1;
129         int const w = dim_.wid - 2;
130         int const h = dim_.height() - 2;
131
132         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
133         // the next line would overwrite the selection!
134         //pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
135         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
136
137 #ifdef WITH_WARNINGS
138 #warning FIXME
139 #endif
140 #if 0
141         LCursor & cur = p.base.bv->cursor();
142         if (cur.isInside(this))
143                 cur.drawSelection(pi);
144 #endif
145
146         pi.pain.text(x + 2, y, prefix(), font);
147         x += font_metrics::width(prefix(), pi.base.font) + 6;
148
149         int const w0 = cell(0).width();
150         int const w1 = cell(1).width();
151         cell(0).draw(pi, x + 2, y + 1);
152         pi.pain.rectangle(x, y - dim_.ascent() + 3,
153                 w0 + 4, dim_.height() - 6, LColor::mathline);
154         cell(1).draw(pi, x + 8 + w0, y + 1);
155         pi.pain.rectangle(x + w0 + 6, y - dim_.ascent() + 3,
156                 w1 + 4, dim_.height() - 6, LColor::mathline);
157 }
158
159
160 void MathMacroTemplate::read(Buffer const &, LyXLex & lex)
161 {
162         MathArray ar;
163         mathed_parse_cell(ar, lex.getStream());
164         if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
165                 lyxerr << "cannot read macro from '" << ar << "'" << endl;
166                 return;
167         }
168         operator=( *(ar[0]->asMacroTemplate()) );
169 }
170
171
172 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
173 {
174         WriteStream wi(os, false, false);
175         os << "FormulaMacro ";
176         write(wi);
177 }
178
179
180 void MathMacroTemplate::write(WriteStream & os) const
181 {
182         if (type_ == "def") {
183                 os << "\n\\def\\" << name_.c_str();
184                 for (int i = 1; i <= numargs_; ++i)
185                         os << '#' << i;
186         } else {
187                 // newcommand or renewcommand
188                 os << "\n\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
189                 if (numargs_ > 0)
190                         os << '[' << numargs_ << ']';
191         }
192
193         os << '{' << cell(0) << "}";
194
195         if (os.latex()) {
196                 // writing .tex. done.
197                 os << "\n";
198         } else {
199                 // writing .lyx, write special .tex export only if necessary
200                 if (!cell(1).empty())
201                         os << "\n{" << cell(1) << '}';
202         }
203 }
204
205
206 MacroData MathMacroTemplate::asMacroData() const
207 {
208         return MacroData(asString(cell(0)), numargs(), asString(cell(1)));
209 }