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