]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
Part of IU.
[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_cursor.h"
16 #include "math_macrotable.h"
17 #include "math_macrotemplate.h"
18 #include "math_mathmlstream.h"
19
20 #include "gettext.h"
21 #include "LColor.h"
22 #include "lyxlex.h"
23 #include "outputparams.h"
24
25 #include "frontends/Painter.h"
26 #include "frontends/font_metrics.h"
27
28 #include "support/lstrings.h"
29 #include "support/std_sstream.h"
30
31 using lyx::support::bformat;
32
33 using std::string;
34 using std::auto_ptr;
35 using std::ostream;
36
37
38 InsetFormulaMacro::InsetFormulaMacro()
39 {
40         // inset name is inherited from Inset
41         setInsetName("unknown");
42 }
43
44
45 InsetFormulaMacro::InsetFormulaMacro
46         (string const & name, int nargs, string const & type)
47 {
48         setInsetName(name);
49         MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
50 }
51
52
53 InsetFormulaMacro::InsetFormulaMacro(string const & s)
54 {
55         std::istringstream is(s);
56         read(is);
57 }
58
59
60 auto_ptr<InsetBase> InsetFormulaMacro::clone() const
61 {
62         return auto_ptr<InsetBase>(new InsetFormulaMacro(*this));
63 }
64
65
66 void InsetFormulaMacro::write(Buffer const &, ostream & os) const
67 {
68         os << "FormulaMacro ";
69         WriteStream wi(os, false, false);
70         par()->write(wi);
71 }
72
73
74 int InsetFormulaMacro::latex(Buffer const &, ostream & os,
75                              OutputParams const & runparams) const
76 {
77         WriteStream wi(os, runparams.moving_arg, true);
78         par()->write(wi);
79         return 2;
80 }
81
82
83 int InsetFormulaMacro::plaintext(Buffer const &, ostream & os,
84                              OutputParams const &) const
85 {
86         WriteStream wi(os, false, true);
87         par()->write(wi);
88         return 0;
89 }
90
91
92 int InsetFormulaMacro::linuxdoc(Buffer const & buf, ostream & os,
93                                 OutputParams const & runparams) const
94 {
95         return plaintext(buf, os, runparams);
96 }
97
98
99 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
100                                OutputParams const & runparams) const
101 {
102         return plaintext(buf, os, runparams);
103 }
104
105
106 void InsetFormulaMacro::read(Buffer const &, LyXLex & lex)
107 {
108         read(lex.getStream());
109 }
110
111
112 void InsetFormulaMacro::read(std::istream & is)
113 {
114         auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
115         setInsetName(p->name());
116         MathMacroTable::create(MathAtom(p.release()));
117         //metrics();
118 }
119
120
121 string InsetFormulaMacro::prefix() const
122 {
123         return bformat(_(" Macro: %1$s: "), getInsetName());
124 }
125
126
127 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
128 {
129         par()->metrics(mi, dim_);
130         dim_.asc += 5;
131         dim_.des += 5;
132         dim_.wid += 10 + font_metrics::width(prefix(), mi.base.font);
133         dim = dim_;
134 }
135
136
137 MathAtom const & InsetFormulaMacro::par() const
138 {
139         return MathMacroTable::provide(getInsetName());
140 }
141
142
143 MathAtom & InsetFormulaMacro::par()
144 {
145         return MathMacroTable::provide(getInsetName());
146 }
147
148
149 InsetOld::Code InsetFormulaMacro::lyxCode() const
150 {
151         return InsetOld::MATHMACRO_CODE;
152 }
153
154
155 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
156 {
157         // label
158         LyXFont font = p.base.font;
159         font.setColor(LColor::math);
160
161         PainterInfo pi(p.base.bv);
162         pi.base.style = LM_ST_TEXT;
163         pi.base.font  = font;
164
165         int const a = y - dim_.asc + 1;
166         int const w = dim_.wid - 2;
167         int const h = dim_.height() - 2;
168
169         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
170         pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
171         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
172
173         if (inMathed() &&
174                         const_cast<InsetFormulaBase const *>(mathcursor::formula()) == this)
175                 mathcursor::drawSelection(pi);
176
177         pi.pain.text(x + 2, y, prefix(), font);
178
179         // formula
180         par()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);
181         xo_ = x;
182         yo_ = y;
183 }