]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
The "I want this in now" patch.
[lyx.git] / src / mathed / formulamacro.C
1 /*
2  *  File:        formulamacro.C
3  *  Purpose:     Implementation of the formula macro LyX inset
4  *  Author:      André Pönitz, based on ideas of Alejandro Aguilar Sierra
5  *  Created:     March 2001
6  *  Description: Allows the edition of math macros inside Lyx.
7  *
8  *  Copyright: 2001  The LyX Project
9  *
10  *   You are free to use and modify this code under the terms of
11  *   the GNU General Public Licence version 2 or later.
12  */
13
14 #include <config.h>
15
16
17 #include "formulamacro.h"
18 #include "lfuns.h"
19 #include "math_cursor.h"
20 #include "math_parser.h"
21 #include "math_macro.h"
22 #include "math_macrotable.h"
23 #include "math_macrotemplate.h"
24 #include "metricsinfo.h"
25 #include "math_support.h"
26 #include "math_mathmlstream.h"
27 #include "BufferView.h"
28 #include "gettext.h"
29 #include "latexrunparams.h"
30 #include "frontends/Painter.h"
31 #include "frontends/font_metrics.h"
32 #include "support/lyxlib.h"
33 #include "support/LOstream.h"
34 #include "debug.h"
35 #include "lyxlex.h"
36 #include "lyxtext.h"
37 #include "lyxfont.h"
38
39 #include "Lsstream.h"
40
41 #include "support/BoostFormat.h"
42
43 using std::ostream;
44
45 extern MathCursor * mathcursor;
46
47
48 InsetFormulaMacro::InsetFormulaMacro()
49 {
50         // inset name is inherited from Inset
51         setInsetName("unknown");
52 }
53
54
55 InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
56 {
57         setInsetName(name);
58         MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs)));
59 }
60
61
62 InsetFormulaMacro::InsetFormulaMacro(string const & s)
63 {
64         std::istringstream is(STRCONV(s));
65         read(is);
66 }
67
68
69 Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
70 {
71         return new InsetFormulaMacro(*this);
72 }
73
74
75 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
76 {
77         os << "FormulaMacro ";
78         WriteStream wi(os, false, false);
79         par()->write(wi);
80 }
81
82
83 int InsetFormulaMacro::latex(Buffer const *, ostream & os,
84                              LatexRunParams const & runparams,
85                              bool /*free_spacing*/) const
86 {
87         WriteStream wi(os, runparams.fragile, true);
88         par()->write(wi);
89         return 2;
90 }
91
92
93 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
94 {
95         WriteStream wi(os, false, true);
96         par()->write(wi);
97         return 0;
98 }
99
100
101 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
102 {
103         return ascii(buf, os, 0);
104 }
105
106
107 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
108 {
109         return ascii(buf, os, 0);
110 }
111
112
113 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
114 {
115         read(lex.getStream());
116 }
117
118
119 void InsetFormulaMacro::read(std::istream & is)
120 {
121         MathMacroTemplate * p = new MathMacroTemplate(is);
122         setInsetName(p->name());
123         MathMacroTable::create(MathAtom(p));
124         metrics();
125 }
126
127
128 string InsetFormulaMacro::prefix() const
129 {
130 #if USE_BOOST_FORMAT
131         return STRCONV(boost::io::str(boost::format(_(" Macro: %s: ")) %
132                 STRCONV(getInsetName())));
133 #else
134         return _(" Macro: ") + getInsetName() + ": ";
135 #endif
136 }
137
138
139 void InsetFormulaMacro::dimension(BufferView * bv, LyXFont const & font,
140         Dimension & dim) const
141 {
142         metrics(bv, font);
143         dim = par()->dimensions();
144         dim.a += 5;
145         dim.d += 5;
146         dim.w += 10 + font_metrics::width(prefix(), font);
147 }
148
149
150 MathAtom const & InsetFormulaMacro::par() const
151 {
152         return MathMacroTable::provide(getInsetName());
153 }
154
155
156 MathAtom & InsetFormulaMacro::par()
157 {
158         return MathMacroTable::provide(getInsetName());
159 }
160
161
162 Inset::Code InsetFormulaMacro::lyxCode() const
163 {
164         return Inset::MATHMACRO_CODE;
165 }
166
167
168 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
169                              int y, float & xx) const
170 {
171         // label
172         LyXFont font(f);
173         font.setColor(LColor::math);
174
175         PainterInfo pi = PainterInfo(bv->painter());
176         pi.base.style = LM_ST_TEXT;
177         pi.base.font  = font;
178
179         int const x = int(xx);
180         int const a = y - ascent(bv, font) + 1;
181         int const w = width(bv, font) - 2;
182         int const h = ascent(bv, font) + descent(bv, font) - 2;
183
184         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
185         pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
186         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
187
188         if (mathcursor &&
189                         const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
190                 mathcursor->drawSelection(pi);
191
192         pi.pain.text(x + 2, y, prefix(), font);
193
194         // formula
195         par()->draw(pi, x + font_metrics::width(prefix(), f) + 5, y);
196         xx += w + 2;
197         xo_ = x;
198         yo_ = y;
199 }