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