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