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