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