]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetFormulaMacro.cpp
rename LyXFont to Font except in tex2lyx
[lyx.git] / src / mathed / InsetFormulaMacro.cpp
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.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetFormulaMacro.h"
15 #include "MacroTable.h"
16 #include "MathMacroTemplate.h"
17
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "debug.h"
21 #include "gettext.h"
22 #include "Color.h"
23 #include "Lexer.h"
24 #include "OutputParams.h"
25
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/lstrings.h"
30
31 #include <sstream>
32
33
34 namespace lyx {
35
36 using odocstream;
37 using support::bformat;
38
39 using std::string;
40 using std::auto_ptr;
41 using std::ostream;
42 using std::endl;
43
44
45
46 InsetFormulaMacro::InsetFormulaMacro()
47         : InsetMathNest(2), name_("unknownA")
48 {}
49
50
51 InsetFormulaMacro::InsetFormulaMacro
52                 (string const & name, int nargs, string const & type)
53         : InsetMathNest(2), name_(name)
54 {
55         MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
56 }
57
58
59 InsetFormulaMacro::InsetFormulaMacro(string const & s)
60         : InsetMathNest(2), name_("unknownB")
61 {
62         std::istringstream is(s);
63         read(is);
64 }
65
66
67 auto_ptr<Inset> InsetFormulaMacro::clone() const
68 {
69         return auto_ptr<Inset>(new InsetFormulaMacro(*this));
70 }
71
72
73 void InsetFormulaMacro::write(Buffer const &, ostream & os) const
74 {
75         os << "FormulaMacro\n";
76         WriteStream wi(os, false, false);
77         tmpl()->write(wi);
78 }
79
80
81 int InsetFormulaMacro::latex(Buffer const &, odocstream & os,
82                              OutputParams const & runparams) const
83 {
84         //lyxerr << "InsetFormulaMacro::latex" << endl;
85         WriteStream wi(os, runparams.moving_arg, true);
86         tmpl()->write(wi);
87         return 2;
88 }
89
90
91 int InsetFormulaMacro::plaintext(Buffer const &, odocstream & os,
92                                  OutputParams const &) const
93 {
94         odocstringstream oss;
95         WriteStream wi(oss, false, true);
96         tmpl()->write(wi);
97
98         docstring const str = oss.str();
99         os << str;
100         return str.size();
101 }
102
103
104 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
105                                OutputParams const & runparams) const
106 {
107         return plaintext(buf, os, runparams);
108 }
109
110
111 void InsetFormulaMacro::read(Buffer const &, Lexer & lex)
112 {
113         read(lex.getStream());
114 }
115
116
117 void InsetFormulaMacro::read(std::istream & is)
118 {
119         auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
120         name_ = p->name();
121         MathMacroTable::create(MathAtom(p.release()));
122 }
123
124
125 string InsetFormulaMacro::prefix() const
126 {
127         return to_utf8(bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_)));
128 }
129
130
131 bool InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
132 {
133         //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
134         tmpl()->metrics(mi, dim);
135         dim.asc += 5;
136         dim.des += 5;
137         dim.wid += 10 + theFontMetrics(mi.base.font).width(prefix());
138         bool const changed = dim_ != dim;
139         dim_ = dim;
140         return changed;
141 }
142
143
144 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
145 {
146         // label
147         Font font = p.base.font;
148         font.setColor(Color::math);
149
150         PainterInfo pi(p.base.bv, p.pain);
151         pi.base.style = LM_ST_TEXT;
152         pi.base.font  = font;
153
154         int const a = y - dim_.asc + 1;
155         int const w = dim_.wid - 2;
156         int const h = dim_.height() - 2;
157
158         // Color::mathbg used to be "AntiqueWhite" but is "linen" now, too
159         pi.pain.fillRectangle(x, a, w, h, Color::mathmacrobg);
160         pi.pain.rectangle(x, a, w, h, Color::mathframe);
161
162 #ifdef WITH_WARNINGS
163 #warning FIXME
164 #endif
165 #if 0
166         Cursor & cur = p.base.bv->cursor();
167         if (cur.isInside(this))
168                 cur.drawSelection(pi);
169 #endif
170
171         pi.pain.text(x + 2, y, prefix(), font);
172
173         // body
174         tmpl()->draw(pi,
175                 x + theFontMetrics(p.base.font).width(prefix()) + 5,
176                 y);
177
178         setPosCache(pi, x, y);
179 }
180
181
182 MathAtom & InsetFormulaMacro::tmpl() const
183 {
184         return MathMacroTable::provide(name_);
185 }
186
187
188 } // namespace lyx