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