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