]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
architectural changes to tex2lyx
[lyx.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 #include <config.h>
17
18 #include "formula.h"
19 #include "math_cursor.h"
20 #include "math_parser.h"
21 #include "math_charinset.h"
22 #include "math_arrayinset.h"
23 #include "metricsinfo.h"
24 #include "math_deliminset.h"
25 #include "math_hullinset.h"
26 #include "math_support.h"
27 #include "math_mathmlstream.h"
28 #include "textpainter.h"
29
30 #include "BufferView.h"
31 #include "gettext.h"
32 #include "debug.h"
33 #include "latexrunparams.h"
34 #include "lyxrc.h"
35 #include "funcrequest.h"
36 #include "Lsstream.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 using std::auto_ptr;
55 using std::endl;
56
57
58 class InsetFormula::PreviewImpl : public lyx::graphics::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 *)
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" << endl;
109 }
110
111
112
113 InsetFormula::~InsetFormula()
114 {}
115
116
117 auto_ptr<InsetBase> InsetFormula::clone() const
118 {
119         return auto_ptr<InsetBase>(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) const
133 {
134         WriteStream wi(os, runparams.moving_arg, true);
135         par_->write(wi);
136         return wi.line();
137 }
138
139
140 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
141 {
142         if (0 && display()) {
143                 Dimension dim;
144                 TextMetricsInfo mi;
145                 par()->metricsT(mi, dim);
146                 TextPainter tpain(dim.width(), dim.height());
147                 par()->drawT(tpain, 0, dim.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" << endl;
190                 if (par_->cell(0).size() == 1) {
191                         lyxerr << "this is size 1" << endl;
192                         if (par_->cell(0)[0]->asFontInset()) {
193                                 lyxerr << "this is a font inset "
194                                        << "replacing " << par_.nucleus()->cell(0) <<
195                                         " with " << par_->cell(0)[0]->cell(0) << endl;
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(PainterInfo & pi, int x, int y) const
211 {
212         cache(pi.base.bv);
213         // This initiates the loading of the preview, so should come
214         // before the metrics are computed.
215         bool const use_preview = preview_->previewReady();
216
217         int const w = dim_.wid;
218         int const d = dim_.des;
219         int const a = dim_.asc;
220         int const h = a + d;
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                 PainterInfo p(pi.base.bv);
227                 p.base.style = LM_ST_TEXT;
228                 p.base.font  = pi.base.font;
229                 p.base.font.setColor(LColor::math);
230                 if (lcolor.getX11Name(LColor::mathbg)
231                             != lcolor.getX11Name(LColor::background))
232                         p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
233
234                 if (mathcursor &&
235                                 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
236                 {
237                         mathcursor->drawSelection(pi);
238                         //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
239                 }
240
241                 par_->draw(p, x, y);
242         }
243
244         xo_ = x;
245         yo_ = y;
246 }
247
248
249 void InsetFormula::getLabelList(vector<string> & res) const
250 {
251         par()->getLabelList(res);
252 }
253
254
255 InsetOld::Code InsetFormula::lyxCode() const
256 {
257         return InsetOld::MATH_CODE;
258 }
259
260
261 void InsetFormula::validate(LaTeXFeatures & features) const
262 {
263         par_->validate(features);
264 }
265
266
267 bool InsetFormula::insetAllowed(InsetOld::Code code) const
268 {
269         return
270                    code == InsetOld::LABEL_CODE
271                 || code == InsetOld::REF_CODE
272                 || code == InsetOld::ERT_CODE;
273 }
274
275
276 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
277 {
278         view_ = m.base.bv;
279         if (preview_->previewReady()) {
280                 dim.asc = preview_->pimage()->ascent();
281                 dim.des = preview_->pimage()->descent();
282                 // insert a one pixel gap in front of the formula
283                 dim.wid = 1 + preview_->pimage()->width();
284                 if (display())
285                         dim.des += 12;
286         } else {
287                 MetricsInfo mi = m;
288                 mi.base.style = LM_ST_TEXT;
289                 mi.base.font.setColor(LColor::math);
290                 par()->metrics(mi, dim);
291                 dim.asc += 1;
292                 dim.des += 1;
293         }
294         dim_ = dim;
295 }
296
297
298 void InsetFormula::mutate(string const & type)
299 {
300         par_.nucleus()->mutate(type);
301 }
302
303
304 //
305 // preview stuff
306 //
307
308 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
309 {
310         preview_->addPreview(ploader);
311 }
312
313
314 void InsetFormula::generatePreview() const
315 {
316         preview_->generatePreview();
317 }
318
319
320 bool InsetFormula::PreviewImpl::previewWanted() const
321 {
322         return !parent().par_->asNestInset()->editing();
323 }
324
325
326 string const InsetFormula::PreviewImpl::latexString() const
327 {
328         ostringstream ls;
329         WriteStream wi(ls, false, false);
330         parent().par_->write(wi);
331         return STRCONV(ls.str());
332 }