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