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