]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
The deed is done.
[lyx.git] / src / mathed / formula.C
1 /**
2  * \file formula.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "formula.h"
15 #include "math_cursor.h"
16 #include "math_parser.h"
17 #include "math_hullinset.h"
18 #include "math_mathmlstream.h"
19 #include "textpainter.h"
20
21 #include "BufferView.h"
22 #include "debug.h"
23 #include "LColor.h"
24 #include "lyx_main.h"
25 #include "outputparams.h"
26
27 #include "frontends/Painter.h"
28
29 #include "graphics/PreviewLoader.h"
30
31 #include "insets/render_preview.h"
32
33 #include "support/std_sstream.h"
34
35 #include <boost/bind.hpp>
36
37 using std::string;
38 using std::ostream;
39 using std::ostringstream;
40 using std::vector;
41 using std::auto_ptr;
42 using std::endl;
43
44
45 InsetFormula::InsetFormula(bool chemistry)
46         : par_(MathAtom(new MathHullInset)),
47           preview_(new RenderPreview)
48 {
49         preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
50         if (chemistry)
51                 mutate("chemistry");
52 }
53
54
55 InsetFormula::InsetFormula(InsetFormula const & other)
56         : InsetFormulaBase(other),
57           par_(other.par_),
58           preview_(new RenderPreview)
59 {
60         preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
61 }
62
63
64 InsetFormula::InsetFormula(BufferView *)
65         : par_(MathAtom(new MathHullInset)),
66           preview_(new RenderPreview)
67 {
68         preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
69 }
70
71
72 InsetFormula::InsetFormula(string const & data)
73         : par_(MathAtom(new MathHullInset)),
74           preview_(new RenderPreview)
75 {
76         preview_->connect(boost::bind(&InsetFormula::statusChanged, this));
77         if (!data.size())
78                 return;
79         if (!mathed_parse_normal(par_, data))
80                 lyxerr << "cannot interpret '" << data << "' as math" << endl;
81 }
82
83
84 InsetFormula::~InsetFormula()
85 {}
86
87
88 auto_ptr<InsetBase> InsetFormula::clone() const
89 {
90         return auto_ptr<InsetBase>(new InsetFormula(*this));
91 }
92
93
94 void InsetFormula::write(Buffer const &, ostream & os) const
95 {
96         WriteStream wi(os, false, false);
97         os << par_->fileInsetLabel() << ' ';
98         par_->write(wi);
99 }
100
101
102 int InsetFormula::latex(Buffer const &, ostream & os,
103                         OutputParams const & runparams) const
104 {
105         WriteStream wi(os, runparams.moving_arg, true);
106         par_->write(wi);
107         return wi.line();
108 }
109
110
111 int InsetFormula::plaintext(Buffer const &, ostream & os,
112                         OutputParams const &) const
113 {
114         if (0 && display()) {
115                 Dimension dim;
116                 TextMetricsInfo mi;
117                 par()->metricsT(mi, dim);
118                 TextPainter tpain(dim.width(), dim.height());
119                 par()->drawT(tpain, 0, dim.ascent());
120                 tpain.show(os, 3);
121                 // reset metrics cache to "real" values
122                 //metrics();
123                 return tpain.textheight();
124         } else {
125                 WriteStream wi(os, false, true);
126                 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
127                 return wi.line();
128         }
129 }
130
131
132 int InsetFormula::linuxdoc(Buffer const & buf, ostream & os,
133                            OutputParams const & runparams) const
134 {
135         return docbook(buf, os, runparams);
136 }
137
138
139 int InsetFormula::docbook(Buffer const & buf, ostream & os,
140                           OutputParams const & runparams) const
141 {
142         MathMLStream ms(os);
143         ms << MTag("equation");
144         ms <<   MTag("alt");
145         ms <<    "<[CDATA[";
146         int res = plaintext(buf, ms.os(), runparams);
147         ms <<    "]]>";
148         ms <<   ETag("alt");
149         ms <<   MTag("math");
150         ms <<    par_;
151         ms <<   ETag("math");
152         ms << ETag("equation");
153         return ms.line() + res;
154 }
155
156
157 void InsetFormula::read(Buffer const &, LyXLex & lex)
158 {
159         mathed_parse_normal(par_, lex);
160         // remove extra 'mathrm' for chemistry stuff.
161         // will be re-added on write
162         if (par_->asHullInset()->getType() =="chemistry")  {
163                 lyxerr << "this is chemistry" << endl;
164                 if (par_->cell(0).size() == 1) {
165                         lyxerr << "this is size 1" << endl;
166                         if (par_->cell(0)[0]->asFontInset()) {
167                                 lyxerr << "this is a font inset "
168                                        << "replacing " << par_.nucleus()->cell(0) <<
169                                         " with " << par_->cell(0)[0]->cell(0) << endl;
170                         }
171                 }
172         }
173         //metrics();
174 }
175
176
177 //ostream & operator<<(ostream & os, LyXCursor const & c)
178 //{
179 //      os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
180 //      return os;
181 //}
182
183
184 namespace {
185
186 bool editing_inset(InsetFormula const * inset)
187 {
188         return mathcursor &&
189                 (const_cast<InsetFormulaBase const *>(mathcursor->formula()) ==
190                  inset);
191 }
192
193 } // namespace anon
194
195
196 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
197 {
198         // The previews are drawn only when we're not editing the inset.
199         bool const use_preview = !editing_inset(this)
200                 && RenderPreview::activated()
201                 && preview_->previewReady();
202
203         int const w = dim_.wid;
204         int const d = dim_.des;
205         int const a = dim_.asc;
206         int const h = a + d;
207
208         if (use_preview) {
209                 // one pixel gap in front
210                 preview_->draw(pi, x + 1, y);
211         } else {
212                 PainterInfo p(pi.base.bv);
213                 p.base.style = LM_ST_TEXT;
214                 p.base.font  = pi.base.font;
215                 p.base.font.setColor(LColor::math);
216                 if (lcolor.getX11Name(LColor::mathbg)
217                             != lcolor.getX11Name(LColor::background))
218                         p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
219
220                 if (editing_inset(this)) {
221                         mathcursor->drawSelection(pi);
222                         //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
223                 }
224
225                 par_->draw(p, x, y);
226         }
227
228         xo_ = x;
229         yo_ = y;
230
231         top_x = x;
232         top_baseline = y;
233
234 }
235
236
237 void InsetFormula::getLabelList(Buffer const & buffer,
238                                 vector<string> & res) const
239 {
240         par()->getLabelList(buffer, res);
241 }
242
243
244 InsetOld::Code InsetFormula::lyxCode() const
245 {
246         return InsetOld::MATH_CODE;
247 }
248
249
250 void InsetFormula::validate(LaTeXFeatures & features) const
251 {
252         par_->validate(features);
253 }
254
255
256 bool InsetFormula::insetAllowed(InsetOld::Code code) const
257 {
258         return
259                    code == InsetOld::LABEL_CODE
260                 || code == InsetOld::REF_CODE
261                 || code == InsetOld::ERT_CODE;
262 }
263
264
265 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
266 {
267         bool const use_preview = !editing_inset(this)
268                 && RenderPreview::activated()
269                 && preview_->previewReady();
270
271         if (use_preview) {
272                 preview_->metrics(m, dim);
273                 // insert a one pixel gap in front of the formula
274                 dim.wid += 1;
275                 if (display())
276                         dim.des += 12;
277         } else {
278                 MetricsInfo mi = m;
279                 mi.base.style = LM_ST_TEXT;
280                 mi.base.font.setColor(LColor::math);
281                 par()->metrics(mi, dim);
282                 dim.asc += 1;
283                 dim.des += 1;
284         }
285
286         dim_ = dim;
287 }
288
289
290 void InsetFormula::mutate(string const & type)
291 {
292         par_.nucleus()->mutate(type);
293 }
294
295
296 //
297 // preview stuff
298 //
299
300 void InsetFormula::statusChanged() const
301 {
302         LyX::cref().updateInset(this);
303 }
304
305
306 namespace {
307
308 string const latex_string(InsetFormula const & inset, Buffer const &)
309 {
310         ostringstream os;
311         WriteStream wi(os, false, false);
312         inset.par()->write(wi);
313         return os.str();
314 }
315
316 } // namespace anon
317
318
319 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
320 {
321         string const snippet = latex_string(*this, ploader.buffer());
322         preview_->addPreview(snippet, ploader);
323 }
324
325
326 void InsetFormula::generatePreview(Buffer const & buffer) const
327 {
328         string const snippet = latex_string(*this, buffer);
329         preview_->addPreview(snippet, buffer);
330         preview_->startLoading(buffer);
331 }