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