A bit more re-organization.
[lyx.git] / src / mathed / InsetFormulaMacro.cpp
blobeaa89d70e3087715204e36eef74f1541b4686012
1 /**
2 * \file InsetFormulaMacro.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
7 * \author André Pönitz
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "InsetFormulaMacro.h"
15 #include "MacroTable.h"
16 #include "MathMacroTemplate.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "support/debug.h"
21 #include "support/gettext.h"
22 #include "Lexer.h"
23 #include "OutputParams.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "support/lstrings.h"
30 #include <sstream>
32 using namespace std;
33 using namespace lyx::support;
35 namespace lyx {
38 InsetFormulaMacro::InsetFormulaMacro()
39 : InsetMathNest(2), name_(from_ascii("unknownA"))
43 InsetFormulaMacro::InsetFormulaMacro
44 (docstring const & name, int nargs, docstring const & type)
45 : InsetMathNest(2), name_(name)
47 MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
51 InsetFormulaMacro::InsetFormulaMacro(string const & s)
52 : InsetMathNest(2), name_("unknownB")
54 istringstream is(s);
55 read(is);
59 Inset * InsetFormulaMacro::clone() const
61 return new InsetFormulaMacro(*this);
65 void InsetFormulaMacro::write(ostream & os) const
67 os << "FormulaMacro\n";
68 WriteStream wi(os, false, false, WriteStream::wsDefault);
69 tmpl()->write(wi);
73 int InsetFormulaMacro::latex(odocstream & os,
74 OutputParams const & runparams) const
76 //lyxerr << "InsetFormulaMacro::latex" << endl;
77 WriteStream wi(os, runparams.moving_arg, true,
78 runparams.dryrun ? WriteStream::wsDryrun: WriteStream::wsDefault,
79 runparams.encoding);
80 tmpl()->write(wi);
81 return 2;
85 int InsetFormulaMacro::plaintext(odocstream & os, OutputParams const & runparams) const
87 odocstringstream oss;
88 WriteStream wi(oss, false, true, WriteStream::wsDefault, runparams.encoding);
89 tmpl()->write(wi);
91 docstring const str = oss.str();
92 os << str;
93 return str.size();
97 int InsetFormulaMacro::docbook(ostream & os,
98 OutputParams const & runparams) const
100 return plaintext(os, runparams);
104 void InsetFormulaMacro::read(Lexer & lex)
106 read(lex.getStream());
110 void InsetFormulaMacro::read(istream & is)
112 auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
113 name_ = p->name();
114 MathMacroTable::create(MathAtom(p.release()));
118 string InsetFormulaMacro::prefix() const
120 return to_utf8(bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_)));
124 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
126 //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
127 tmpl()->metrics(mi, dim);
128 dim.asc += 5;
129 dim.des += 5;
130 dim.wid += 10 + theFontMetrics(mi.base.font).width(prefix());
131 dim_ = dim;
135 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
137 // label
138 Font font = p.base.font;
139 font.setColor(Color_math);
141 PainterInfo pi(p.base.bv, p.pain);
142 pi.base.style = LM_ST_TEXT;
143 pi.base.font = font;
145 int const a = y - dim_.asc + 1;
146 int const w = dim_.wid - 2;
147 int const h = dim_.height() - 2;
149 // Color_mathbg used to be "AntiqueWhite" but is "linen" now, too
150 pi.pain.fillRectangle(x, a, w, h, Color_mathmacrobg);
151 pi.pain.rectangle(x, a, w, h, Color_mathframe);
153 // FIXME
154 #if 0
155 Cursor & cur = p.base.bv->cursor();
156 if (cur.isInside(this))
157 cur.drawSelection(pi);
158 #endif
160 pi.pain.text(x + 2, y, prefix(), font);
162 // body
163 tmpl()->draw(pi,
164 x + theFontMetrics(p.base.font).width(prefix()) + 5,
167 setPosCache(pi, x, y);
171 MathAtom & InsetFormulaMacro::tmpl() const
173 return MathMacroTable::provide(name_);
177 } // namespace lyx