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