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