]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetFormulaMacro.cpp
adjust
[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 using std::string;
34 using std::ostream;
35 using std::endl;
36
37 namespace lyx {
38
39 using support::bformat;
40
41
42 InsetFormulaMacro::InsetFormulaMacro()
43         : InsetMathNest(2), name_("unknownA")
44 {}
45
46
47 InsetFormulaMacro::InsetFormulaMacro
48                 (docstring const & name, int nargs, docstring const & type)
49         : InsetMathNest(2), name_(name)
50 {
51         MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
52 }
53
54
55 InsetFormulaMacro::InsetFormulaMacro(string const & s)
56         : InsetMathNest(2), name_("unknownB")
57 {
58         std::istringstream is(s);
59         read(is);
60 }
61
62
63 Inset * InsetFormulaMacro::clone() const
64 {
65         return new InsetFormulaMacro(*this);
66 }
67
68
69 void InsetFormulaMacro::write(Buffer const &, ostream & os) const
70 {
71         os << "FormulaMacro\n";
72         WriteStream wi(os, false, false);
73         tmpl()->write(wi);
74 }
75
76
77 int InsetFormulaMacro::latex(Buffer const &, odocstream & os,
78                              OutputParams const & runparams) const
79 {
80         //lyxerr << "InsetFormulaMacro::latex" << endl;
81         WriteStream wi(os, runparams.moving_arg, true);
82         tmpl()->write(wi);
83         return 2;
84 }
85
86
87 int InsetFormulaMacro::plaintext(Buffer const &, odocstream & os,
88                                  OutputParams const &) const
89 {
90         odocstringstream oss;
91         WriteStream wi(oss, false, true);
92         tmpl()->write(wi);
93
94         docstring const str = oss.str();
95         os << str;
96         return str.size();
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 &, Lexer & 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         Font font = p.base.font;
142         font.setColor(Color::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         // Color::mathbg used to be "AntiqueWhite" but is "linen" now, too
153         pi.pain.fillRectangle(x, a, w, h, Color::mathmacrobg);
154         pi.pain.rectangle(x, a, w, h, Color::mathframe);
155
156         // FIXME
157 #if 0
158         Cursor & cur = p.base.bv->cursor();
159         if (cur.isInside(this))
160                 cur.drawSelection(pi);
161 #endif
162
163         pi.pain.text(x + 2, y, prefix(), font);
164
165         // body
166         tmpl()->draw(pi,
167                 x + theFontMetrics(p.base.font).width(prefix()) + 5,
168                 y);
169
170         setPosCache(pi, x, y);
171 }
172
173
174 MathAtom & InsetFormulaMacro::tmpl() const
175 {
176         return MathMacroTable::provide(name_);
177 }
178
179
180 } // namespace lyx