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