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