]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
Andreas' patch to prevent crash on click on previewd inset
[lyx.git] / src / mathed / formulamacro.C
1 /**
2  * \file formulamacro.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 "formulamacro.h"
15 #include "math_macrotable.h"
16 #include "math_macrotemplate.h"
17 #include "math_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/Painter.h"
28 #include "frontends/font_metrics.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         : MathNestInset(2), name_("unknownA")
45 {}
46
47
48 InsetFormulaMacro::InsetFormulaMacro
49                 (string const & name, int nargs, string const & type)
50         : MathNestInset(2), name_(name)
51 {
52         MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
53 }
54
55
56 InsetFormulaMacro::InsetFormulaMacro(string const & s)
57         : MathNestInset(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::linuxdoc(Buffer const & buf, ostream & os,
98                                 OutputParams const & runparams) const
99 {
100         return plaintext(buf, os, runparams);
101 }
102
103
104 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
105                                OutputParams const & runparams) const
106 {
107         return plaintext(buf, os, runparams);
108 }
109
110
111 void InsetFormulaMacro::read(Buffer const &, LyXLex & lex)
112 {
113         read(lex.getStream());
114 }
115
116
117 void InsetFormulaMacro::read(std::istream & is)
118 {
119         auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
120         name_ = p->name();
121         MathMacroTable::create(MathAtom(p.release()));
122 }
123
124
125 string InsetFormulaMacro::prefix() const
126 {
127         return bformat(_(" Macro: %1$s: "), name_);
128 }
129
130
131 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
132 {
133         //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
134         tmpl()->metrics(mi, dim);
135         dim.asc += 5;
136         dim.des += 5;
137         dim.wid += 10 + font_metrics::width(prefix(), mi.base.font);
138         dim_ = dim;
139 }
140
141
142 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
143 {
144         // label
145         LyXFont font = p.base.font;
146         font.setColor(LColor::math);
147
148         PainterInfo pi(p.base.bv, p.pain);
149         pi.base.style = LM_ST_TEXT;
150         pi.base.font  = font;
151
152         int const a = y - dim_.asc + 1;
153         int const w = dim_.wid - 2;
154         int const h = dim_.height() - 2;
155
156         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
157         pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
158         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
159
160 #ifdef WITH_WARNINGS
161 #warning FIXME
162 #endif
163 #if 0
164         LCursor & cur = p.base.bv->cursor();
165         if (cur.isInside(this))
166                 cur.drawSelection(pi);
167 #endif
168
169         pi.pain.text(x + 2, y, prefix(), font);
170
171         // body
172         tmpl()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);
173
174         setPosCache(pi, x, y);
175 }
176
177
178 MathAtom & InsetFormulaMacro::tmpl() const
179 {
180         return MathMacroTable::provide(name_);
181 }