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