]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetFormulaMacro.cpp
* src/frontends/controllers/Dialog.{cpp,h}:
[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::auto_ptr;
35 using std::ostream;
36 using std::endl;
37
38 namespace lyx {
39
40 using support::bformat;
41
42
43 InsetFormulaMacro::InsetFormulaMacro()
44         : InsetMathNest(2), name_("unknownA")
45 {}
46
47
48 InsetFormulaMacro::InsetFormulaMacro
49                 (docstring const & name, int nargs, docstring 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<Inset> InsetFormulaMacro::clone() const
65 {
66         return auto_ptr<Inset>(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 &, odocstream & 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 &, odocstream & os,
89                                  OutputParams const &) const
90 {
91         odocstringstream oss;
92         WriteStream wi(oss, false, true);
93         tmpl()->write(wi);
94
95         docstring const str = oss.str();
96         os << str;
97         return str.size();
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 &, Lexer & 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 bool 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         bool const changed = dim_ != dim;
136         dim_ = dim;
137         return changed;
138 }
139
140
141 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
142 {
143         // label
144         Font font = p.base.font;
145         font.setColor(Color::math);
146
147         PainterInfo pi(p.base.bv, p.pain);
148         pi.base.style = LM_ST_TEXT;
149         pi.base.font  = font;
150
151         int const a = y - dim_.asc + 1;
152         int const w = dim_.wid - 2;
153         int const h = dim_.height() - 2;
154
155         // Color::mathbg used to be "AntiqueWhite" but is "linen" now, too
156         pi.pain.fillRectangle(x, a, w, h, Color::mathmacrobg);
157         pi.pain.rectangle(x, a, w, h, Color::mathframe);
158
159 #ifdef WITH_WARNINGS
160 #warning FIXME
161 #endif
162 #if 0
163         Cursor & cur = p.base.bv->cursor();
164         if (cur.isInside(this))
165                 cur.drawSelection(pi);
166 #endif
167
168         pi.pain.text(x + 2, y, prefix(), font);
169
170         // body
171         tmpl()->draw(pi,
172                 x + theFontMetrics(p.base.font).width(prefix()) + 5,
173                 y);
174
175         setPosCache(pi, x, y);
176 }
177
178
179 MathAtom & InsetFormulaMacro::tmpl() const
180 {
181         return MathMacroTable::provide(name_);
182 }
183
184
185 } // namespace lyx