]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetFormulaMacro.C
Fix comment according to Enricos explanation
[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
18 #include "BufferView.h"
19 #include "cursor.h"
20 #include "debug.h"
21 #include "gettext.h"
22 #include "LColor.h"
23 #include "lyxlex.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<InsetBase> InsetFormulaMacro::clone() const
68 {
69         return auto_ptr<InsetBase>(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         WriteStream wi(os, false, true);
95         tmpl()->write(wi);
96         return 0;
97 }
98
99
100 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
101                                OutputParams const & runparams) const
102 {
103         return plaintext(buf, os, runparams);
104 }
105
106
107 void InsetFormulaMacro::read(Buffer const &, LyXLex & lex)
108 {
109         read(lex.getStream());
110 }
111
112
113 void InsetFormulaMacro::read(std::istream & is)
114 {
115         auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
116         name_ = p->name();
117         MathMacroTable::create(MathAtom(p.release()));
118 }
119
120
121 string InsetFormulaMacro::prefix() const
122 {
123         return to_utf8(bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_)));
124 }
125
126
127 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
128 {
129         //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
130         tmpl()->metrics(mi, dim);
131         dim.asc += 5;
132         dim.des += 5;
133         dim.wid += 10 + theFontMetrics(mi.base.font).width(prefix());
134         dim_ = dim;
135 }
136
137
138 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
139 {
140         // label
141         LyXFont font = p.base.font;
142         font.setColor(LColor::math);
143
144         PainterInfo pi(p.base.bv, p.pain);
145         pi.base.style = LM_ST_TEXT;
146         pi.base.font  = font;
147
148         int const a = y - dim_.asc + 1;
149         int const w = dim_.wid - 2;
150         int const h = dim_.height() - 2;
151
152         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
153         pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
154         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
155
156 #ifdef WITH_WARNINGS
157 #warning FIXME
158 #endif
159 #if 0
160         LCursor & cur = p.base.bv->cursor();
161         if (cur.isInside(this))
162                 cur.drawSelection(pi);
163 #endif
164
165         pi.pain.text(x + 2, y, prefix(), font);
166
167         // body
168         tmpl()->draw(pi,
169                 x + theFontMetrics(p.base.font).width(prefix()) + 5,
170                 y);
171
172         setPosCache(pi, x, y);
173 }
174
175
176 MathAtom & InsetFormulaMacro::tmpl() const
177 {
178         return MathMacroTable::provide(name_);
179 }
180
181
182 } // namespace lyx