]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
3a96e87ebf221d3ae7d31e42217029a80cd87990
[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) const
85 {
86         WriteStream wi(os, runparams.moving_arg, true);
87         par()->write(wi);
88         return 2;
89 }
90
91
92 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
93 {
94         WriteStream wi(os, false, true);
95         par()->write(wi);
96         return 0;
97 }
98
99
100 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
101 {
102         return ascii(buf, os, 0);
103 }
104
105
106 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
107 {
108         return ascii(buf, os, 0);
109 }
110
111
112 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
113 {
114         read(lex.getStream());
115 }
116
117
118 void InsetFormulaMacro::read(std::istream & is)
119 {
120         MathMacroTemplate * p = new MathMacroTemplate(is);
121         setInsetName(p->name());
122         MathMacroTable::create(MathAtom(p));
123         metrics();
124 }
125
126
127 string InsetFormulaMacro::prefix() const
128 {
129 #if USE_BOOST_FORMAT
130         return STRCONV(boost::io::str(boost::format(_(" Macro: %s: ")) %
131                 STRCONV(getInsetName())));
132 #else
133         return _(" Macro: ") + getInsetName() + ": ";
134 #endif
135 }
136
137
138 void InsetFormulaMacro::dimension(BufferView * bv, LyXFont const & font,
139         Dimension & dim) const
140 {
141         metrics(bv, font);
142         dim = par()->dimensions();
143         dim.a += 5;
144         dim.d += 5;
145         dim.w += 10 + font_metrics::width(prefix(), font);
146 }
147
148
149 MathAtom const & InsetFormulaMacro::par() const
150 {
151         return MathMacroTable::provide(getInsetName());
152 }
153
154
155 MathAtom & InsetFormulaMacro::par()
156 {
157         return MathMacroTable::provide(getInsetName());
158 }
159
160
161 Inset::Code InsetFormulaMacro::lyxCode() const
162 {
163         return Inset::MATHMACRO_CODE;
164 }
165
166
167 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
168                              int y, float & xx) const
169 {
170         // label
171         LyXFont font(f);
172         font.setColor(LColor::math);
173
174         PainterInfo pi = PainterInfo(bv->painter());
175         pi.base.style = LM_ST_TEXT;
176         pi.base.font  = font;
177
178         int const x = int(xx);
179         int const a = y - ascent(bv, font) + 1;
180         int const w = width(bv, font) - 2;
181         int const h = ascent(bv, font) + descent(bv, font) - 2;
182
183         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
184         pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
185         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
186
187         if (mathcursor &&
188                         const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
189                 mathcursor->drawSelection(pi);
190
191         pi.pain.text(x + 2, y, prefix(), font);
192
193         // formula
194         par()->draw(pi, x + font_metrics::width(prefix(), f) + 5, y);
195         xx += w + 2;
196         xo_ = x;
197         yo_ = y;
198 }