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