]> git.lyx.org Git - features.git/blob - src/mathed/formula.C
The 'bformat' introduction
[features.git] / src / mathed / formula.C
1 /*
2 *  File:        formula.C
3 *  Purpose:     Implementation of formula inset
4 *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 *  Created:     January 1996
6 *  Description: Allows the edition of math paragraphs inside Lyx.
7 *
8 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
9 *
10 *  Version: 0.4, Lyx project.
11 *
12 *   You are free to use and modify this code under the terms of
13 *   the GNU General Public Licence version 2 or later.
14 */
15
16
17 #include <config.h>
18
19 #include "formula.h"
20 #include "math_cursor.h"
21 #include "math_parser.h"
22 #include "math_charinset.h"
23 #include "math_arrayinset.h"
24 #include "metricsinfo.h"
25 #include "math_deliminset.h"
26 #include "math_hullinset.h"
27 #include "math_support.h"
28 #include "math_mathmlstream.h"
29 #include "textpainter.h"
30 #include "Lsstream.h"
31
32 #include "BufferView.h"
33 #include "gettext.h"
34 #include "debug.h"
35 #include "lyxrc.h"
36 #include "funcrequest.h"
37
38 #include "support/LOstream.h"
39 #include "support/LAssert.h"
40 #include "support/lyxlib.h"
41 #include "support/systemcall.h"
42 #include "support/filetools.h"
43
44 #include "frontends/Alert.h"
45 #include "frontends/LyXView.h"
46 #include "frontends/Painter.h"
47
48 #include "graphics/PreviewedInset.h"
49 #include "graphics/PreviewImage.h"
50
51
52 using std::ostream;
53 using std::vector;
54
55
56 class InsetFormula::PreviewImpl : public grfx::PreviewedInset {
57 public:
58         ///
59         PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
60
61 private:
62         ///
63         bool previewWanted() const;
64         ///
65         string const latexString() const;
66         ///
67         InsetFormula & parent() const
68         {
69                 return *static_cast<InsetFormula*>(inset());
70         }
71 };
72
73
74
75 InsetFormula::InsetFormula(bool chemistry)
76         : par_(MathAtom(new MathHullInset)),
77           preview_(new PreviewImpl(*this))
78 {
79         if (chemistry)
80                 mutate("chemistry");
81 }
82
83
84 InsetFormula::InsetFormula(InsetFormula const & other)
85         : InsetFormulaBase(other),
86           par_(other.par_),
87           preview_(new PreviewImpl(*this))
88 {}
89
90
91 InsetFormula::InsetFormula(BufferView * bv)
92         : par_(MathAtom(new MathHullInset)),
93           preview_(new PreviewImpl(*this))
94 {
95         view_ = bv->owner()->view();
96 }
97
98
99 InsetFormula::InsetFormula(string const & data)
100         : par_(MathAtom(new MathHullInset)),
101           preview_(new PreviewImpl(*this))
102 {
103         if (!data.size())
104                 return;
105         if (!mathed_parse_normal(par_, data))
106                 lyxerr << "cannot interpret '" << data << "' as math\n";
107 }
108
109
110
111 InsetFormula::~InsetFormula()
112 {}
113
114
115 Inset * InsetFormula::clone(Buffer const &, bool) const
116 {
117         return new InsetFormula(*this);
118 }
119
120
121 void InsetFormula::write(Buffer const *, ostream & os) const
122 {
123         WriteStream wi(os, false, false);
124         os << par_->fileInsetLabel() << ' ';
125         par_->write(wi);
126 }
127
128
129 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
130 {
131         WriteStream wi(os, fragile, true);
132         par_->write(wi);
133         return wi.line();
134 }
135
136
137 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
138 {
139         if (0 && display()) {
140                 TextMetricsInfo mi;
141                 par()->metricsT(mi);
142                 TextPainter tpain(par()->width(), par()->height());
143                 par()->drawT(tpain, 0, par()->ascent());
144                 tpain.show(os, 3);
145                 // reset metrics cache to "real" values
146                 metrics();
147                 return tpain.textheight();
148         } else {
149                 WriteStream wi(os, false, true);
150                 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
151                 return wi.line();
152         }
153 }
154
155
156 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
157 {
158         return docbook(buf, os, false);
159 }
160
161
162 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
163 {
164         MathMLStream ms(os);
165         ms << MTag("equation");
166         ms <<   MTag("alt");
167         ms <<    "<[CDATA[";
168         int res = ascii(buf, ms.os(), 0);
169         ms <<    "]]>";
170         ms <<   ETag("alt");
171         ms <<   MTag("math");
172         ms <<    par_;
173         ms <<   ETag("math");
174         ms << ETag("equation");
175         return ms.line() + res;
176 }
177
178
179 void InsetFormula::read(Buffer const *, LyXLex & lex)
180 {
181         mathed_parse_normal(par_, lex);
182         // remove extra 'mathrm' for chemistry stuff.
183         // will be re-added on write
184         if (par_->asHullInset()->getType() =="chemistry")  {
185                 lyxerr << "this is chemistry\n";
186                 if (par_->cell(0).size() == 1) {
187                         lyxerr << "this is size 1\n";
188             if (par_->cell(0)[0]->asFontInset()) {
189                                 lyxerr << "this is a font inset \n";
190                                 lyxerr << "replacing " << par_.nucleus()->cell(0) << 
191                                         " with " << par_->cell(0)[0]->cell(0) << "\n";
192                         }
193                 }
194         }
195         metrics();
196 }
197
198
199 //ostream & operator<<(ostream & os, LyXCursor const & c)
200 //{
201 //      os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
202 //      return os;
203 //}
204
205
206 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
207                         int y, float & xx) const
208 {
209         cache(bv);
210         // This initiates the loading of the preview, so should come
211         // before the metrics are computed.
212         bool const use_preview = preview_->previewReady();
213
214         int const x = int(xx);
215         int const w = width(bv, font);
216         int const d = descent(bv, font);
217         int const a = ascent(bv, font);
218         int const h = a + d;
219
220         PainterInfo pi(bv->painter());
221
222         if (use_preview) {
223                 pi.pain.image(x + 1, y - a, w, h,   // one pixel gap in front
224                               *(preview_->pimage()->image()));
225         } else {
226                 pi.base.style = LM_ST_TEXT;
227                 pi.base.font  = font;
228                 pi.base.font.setColor(LColor::math);
229                 if (lcolor.getX11Name(LColor::mathbg)
230                             != lcolor.getX11Name(LColor::background))
231                         pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
232
233                 if (mathcursor &&
234                                 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
235                 {
236                         mathcursor->drawSelection(pi);
237                         //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
238                 }
239
240                 par_->draw(pi, x, y);
241         }
242
243         xx += w;
244         xo_ = x;
245         yo_ = y;
246 }
247
248
249 vector<string> const InsetFormula::getLabelList() const
250 {
251         vector<string> res;
252         par()->getLabelList(res);
253         return res;
254 }
255
256
257 Inset::Code InsetFormula::lyxCode() const
258 {
259         return Inset::MATH_CODE;
260 }
261
262
263 void InsetFormula::validate(LaTeXFeatures & features) const
264 {
265         par_->validate(features);
266 }
267
268
269 bool InsetFormula::insetAllowed(Inset::Code code) const
270 {
271         return
272                    code == Inset::LABEL_CODE
273                 || code == Inset::REF_CODE
274                 || code == Inset::ERT_CODE;
275 }
276
277
278 int InsetFormula::ascent(BufferView *, LyXFont const &) const
279 {
280         return preview_->previewReady() ?
281                 preview_->pimage()->ascent() : 1 + par_->ascent();
282 }
283
284
285 int InsetFormula::descent(BufferView *, LyXFont const &) const
286 {
287         if (!preview_->previewReady())
288                 return 1 + par_->descent();
289
290         int const descent = preview_->pimage()->descent();
291         return display() ? descent + 12 : descent;
292 }
293
294
295 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
296 {
297         metrics(bv, font);
298         return preview_->previewReady() ?
299                 1 + preview_->pimage()->width() : par_->width();
300                 // insert a one pixel gap in front of the formula
301 }
302
303
304 void InsetFormula::mutate(string const & type)
305 {
306         par_.nucleus()->mutate(type);
307 }
308
309
310 //
311 // preview stuff
312 //
313
314 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
315 {
316         preview_->addPreview(ploader);
317 }
318
319
320 void InsetFormula::generatePreview() const
321 {
322         preview_->generatePreview();
323 }
324
325
326 bool InsetFormula::PreviewImpl::previewWanted() const
327 {
328         return !parent().par_->asNestInset()->editing();
329 }
330
331
332 string const InsetFormula::PreviewImpl::latexString() const
333 {
334         ostringstream ls;
335         WriteStream wi(ls, false, false);
336         parent().par_->write(wi);
337         return STRCONV(ls.str());
338 }
339