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