]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
small up/down tweaking
[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 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "formulamacro.h"
21 #include "commandtags.h"
22 #include "math_cursor.h"
23 #include "math_parser.h"
24 #include "math_macro.h"
25 #include "math_macrotable.h"
26 #include "math_macrotemplate.h"
27 #include "math_metricsinfo.h"
28 #include "math_support.h"
29 #include "math_mathmlstream.h"
30 #include "BufferView.h"
31 #include "gettext.h"
32 #include "frontends/Painter.h"
33 #include "frontends/font_metrics.h"
34 #include "support/lyxlib.h"
35 #include "support/LOstream.h"
36 #include "debug.h"
37 #include "lyxlex.h"
38 #include "lyxtext.h"
39 #include "lyxfont.h"
40
41 #include "Lsstream.h"
42
43 #include "BoostFormat.h"
44
45 using std::ostream;
46
47 extern MathCursor * mathcursor;
48
49
50 InsetFormulaMacro::InsetFormulaMacro()
51 {
52         // inset name is inherited from Inset
53         setInsetName("unknown");
54 }
55
56
57 InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
58 {
59         setInsetName(name);
60         MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs)));
61 }
62
63
64 InsetFormulaMacro::InsetFormulaMacro(string const & s)
65 {
66         std::istringstream is(STRCONV(s));
67         read(is);
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, bool fragile,
86                              bool /*free_spacing*/) const
87 {
88         WriteStream wi(os, fragile, 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 #if USE_BOOST_FORMAT
132         return boost::io::str(boost::format(_(" Macro: %s: ")) % getInsetName());
133 #else
134         return _(" Macro: ") + getInsetName() + ": ";
135 #endif
136 }
137
138
139 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
140 {
141         return par()->ascent() + 5;
142 }
143
144
145 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
146 {
147         return par()->descent() + 5;
148 }
149
150
151 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
152 {
153         metrics(bv, f);
154         return 10 + font_metrics::width(prefix(), f) + par()->width();
155 }
156
157
158 MathAtom const & InsetFormulaMacro::par() const
159 {
160         return MathMacroTable::provide(getInsetName());
161 }
162
163
164 MathAtom & InsetFormulaMacro::par()
165 {
166         return MathMacroTable::provide(getInsetName());
167 }
168
169
170 Inset::Code InsetFormulaMacro::lyxCode() const
171 {
172         return Inset::MATHMACRO_CODE;
173 }
174
175
176 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
177                              int y, float & xx, bool /*cleared*/) const
178 {
179         // label
180         LyXFont font(f);
181         font.setColor(LColor::math);
182
183         MathPainterInfo pi = MathPainterInfo(bv->painter());
184         pi.base.style = LM_ST_TEXT;
185         pi.base.font  = font;
186
187         int const x = int(xx);
188         int const a = y - ascent(bv, font) + 1;
189         int const w = width(bv, font) - 2;
190         int const h = ascent(bv, font) + descent(bv, font) - 2;
191
192         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
193         pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
194         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
195
196         if (mathcursor &&
197                         const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
198                 mathcursor->drawSelection(pi);
199
200         pi.pain.text(x + 2, y, prefix(), font);
201
202         // formula
203         par()->draw(pi, x + font_metrics::width(prefix(), f) + 5, y);
204         xx += w + 2;
205         xo_ = x;
206         yo_ = y;
207
208         setCursorVisible(false);
209 }