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