]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
ws changes only
[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 "latexrunparams.h"
24 #include "LColor.h"
25
26 #include "frontends/Painter.h"
27
28 #include "graphics/PreviewImage.h"
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                         LatexRunParams 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::ascii(Buffer const &, ostream & os, int) const
112 {
113         if (0 && display()) {
114                 Dimension dim;
115                 TextMetricsInfo mi;
116                 par()->metricsT(mi, dim);
117                 TextPainter tpain(dim.width(), dim.height());
118                 par()->drawT(tpain, 0, dim.ascent());
119                 tpain.show(os, 3);
120                 // reset metrics cache to "real" values
121                 //metrics();
122                 return tpain.textheight();
123         } else {
124                 WriteStream wi(os, false, true);
125                 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
126                 return wi.line();
127         }
128 }
129
130
131 int InsetFormula::linuxdoc(Buffer const & buf, ostream & os) const
132 {
133         return docbook(buf, os, false);
134 }
135
136
137 int InsetFormula::docbook(Buffer const & buf, ostream & os, bool) const
138 {
139         MathMLStream ms(os);
140         ms << MTag("equation");
141         ms <<   MTag("alt");
142         ms <<    "<[CDATA[";
143         int res = ascii(buf, ms.os(), 0);
144         ms <<    "]]>";
145         ms <<   ETag("alt");
146         ms <<   MTag("math");
147         ms <<    par_;
148         ms <<   ETag("math");
149         ms << ETag("equation");
150         return ms.line() + res;
151 }
152
153
154 void InsetFormula::read(Buffer const &, LyXLex & lex)
155 {
156         mathed_parse_normal(par_, lex);
157         // remove extra 'mathrm' for chemistry stuff.
158         // will be re-added on write
159         if (par_->asHullInset()->getType() =="chemistry")  {
160                 lyxerr << "this is chemistry" << endl;
161                 if (par_->cell(0).size() == 1) {
162                         lyxerr << "this is size 1" << endl;
163                         if (par_->cell(0)[0]->asFontInset()) {
164                                 lyxerr << "this is a font inset "
165                                        << "replacing " << par_.nucleus()->cell(0) <<
166                                         " with " << par_->cell(0)[0]->cell(0) << endl;
167                         }
168                 }
169         }
170         //metrics();
171 }
172
173
174 //ostream & operator<<(ostream & os, LyXCursor const & c)
175 //{
176 //      os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
177 //      return os;
178 //}
179
180
181 void InsetFormula::draw(PainterInfo & pi, int x, int y) const
182 {
183         cache(pi.base.bv);
184
185         // The previews are drawn only when we're not editing the inset.
186         bool const editing_inset = mathcursor && mathcursor->formula() == this;
187         bool const use_preview = !editing_inset && preview_->previewReady();
188
189         int const w = dim_.wid;
190         int const d = dim_.des;
191         int const a = dim_.asc;
192         int const h = a + d;
193
194         if (use_preview) {
195                 // one pixel gap in front
196                 preview_->draw(pi, x + 1, y);
197         } else {
198                 PainterInfo p(pi.base.bv);
199                 p.base.style = LM_ST_TEXT;
200                 p.base.font  = pi.base.font;
201                 p.base.font.setColor(LColor::math);
202                 if (lcolor.getX11Name(LColor::mathbg)
203                             != lcolor.getX11Name(LColor::background))
204                         p.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
205
206                 if (editing_inset) {
207                         mathcursor->drawSelection(pi);
208                         //p.pain.rectangle(x, y - a, w, h, LColor::mathframe);
209                 }
210
211                 par_->draw(p, x + offset_, y);
212         }
213
214         xo_ = x;
215         yo_ = y;
216 }
217
218
219 void InsetFormula::getLabelList(Buffer const & buffer,
220                                 vector<string> & res) const
221 {
222         par()->getLabelList(buffer, res);
223 }
224
225
226 InsetOld::Code InsetFormula::lyxCode() const
227 {
228         return InsetOld::MATH_CODE;
229 }
230
231
232 void InsetFormula::validate(LaTeXFeatures & features) const
233 {
234         par_->validate(features);
235 }
236
237
238 bool InsetFormula::insetAllowed(InsetOld::Code code) const
239 {
240         return
241                    code == InsetOld::LABEL_CODE
242                 || code == InsetOld::REF_CODE
243                 || code == InsetOld::ERT_CODE;
244 }
245
246
247 void InsetFormula::metrics(MetricsInfo & m, Dimension & dim) const
248 {
249         bool const editing_inset = mathcursor && mathcursor->formula() == this;
250         bool const use_preview = !editing_inset && preview_->previewReady();
251
252         if (use_preview) {
253                 preview_->metrics(m, dim);
254                 // insert a one pixel gap in front of the formula
255                 dim.wid += 1;
256                 if (display())
257                         dim.des += 12;
258         } else {
259                 MetricsInfo mi = m;
260                 mi.base.style = LM_ST_TEXT;
261                 mi.base.font.setColor(LColor::math);
262                 par()->metrics(mi, dim);
263                 dim.asc += 1;
264                 dim.des += 1;
265         }
266
267         if (display()) {
268                 offset_ = (m.base.textwidth - dim.wid) / 2;
269                 dim.wid = m.base.textwidth;
270         } else {
271                 offset_ = 0;
272         }
273
274         dim_ = dim;
275 }
276
277
278 void InsetFormula::mutate(string const & type)
279 {
280         par_.nucleus()->mutate(type);
281 }
282
283
284 //
285 // preview stuff
286 //
287
288 void InsetFormula::statusChanged() const
289 {
290         if (view())
291                 view()->updateInset(this);
292 }
293
294
295 namespace {
296
297 bool preview_wanted(InsetFormula const & inset, Buffer const &)
298 {
299         // Don't want a preview when we're editing the inset
300         return !(mathcursor && mathcursor->formula() == &inset);
301 }
302
303
304 string const latex_string(InsetFormula const & inset, Buffer const &)
305 {
306         ostringstream ls;
307         WriteStream wi(ls, false, false);
308         inset.par()->write(wi);
309         return ls.str();
310 }
311
312 } // namespace anon
313
314
315 void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const
316 {
317         if (preview_wanted(*this, ploader.buffer())) {
318                 string const snippet = latex_string(*this, ploader.buffer());
319                 preview_->addPreview(snippet, ploader);
320         }
321 }
322
323
324 void InsetFormula::generatePreview(Buffer const & buffer) const
325 {
326         if (preview_wanted(*this, buffer)) {
327                 string const snippet = latex_string(*this, buffer);
328                 preview_->generatePreview(snippet, buffer);
329         }
330 }