]> git.lyx.org Git - features.git/blob - src/mathed/formula.C
remove a few unneeded 'include'
[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 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include <config.h>
21
22 #include "formula.h"
23 #include "commandtags.h"
24 #include "math_cursor.h"
25 #include "math_parser.h"
26 #include "math_charinset.h"
27 #include "math_arrayinset.h"
28 #include "math_metricsinfo.h"
29 #include "math_deliminset.h"
30 #include "math_hullinset.h"
31 #include "math_support.h"
32 #include "math_mathmlstream.h"
33 #include "textpainter.h"
34
35 #include "lyx_main.h"
36 #include "BufferView.h"
37 #include "gettext.h"
38 #include "debug.h"
39 #include "lyxrc.h"
40
41 #include "support/LOstream.h"
42 #include "support/LAssert.h"
43 #include "support/lyxlib.h"
44 #include "support/systemcall.h"
45 #include "support/filetools.h"
46
47 #include "frontends/Alert.h"
48 #include "frontends/LyXView.h"
49 #include "frontends/Painter.h"
50
51 #include "graphics/PreviewedInset.h"
52 #include "graphics/PreviewImage.h"
53
54 #include <fstream>
55
56
57 using std::ostream;
58 using std::ifstream;
59 using std::istream;
60 using std::pair;
61 using std::endl;
62 using std::vector;
63 using std::getline;
64
65
66 class InsetFormula::PreviewImpl : public grfx::PreviewedInset {
67 public:
68         ///
69         PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
70
71 private:
72         ///
73         bool previewWanted() const;
74         ///
75         string const latexString() const;
76         ///
77         BufferView * view() const
78         {
79                 return parent().view();
80         }
81         ///
82         InsetFormula & parent() const
83         {
84                 return *static_cast<InsetFormula*>(inset());
85         }
86 };
87
88
89
90 InsetFormula::InsetFormula()
91         : par_(MathAtom(new MathHullInset)),
92           preview_(new PreviewImpl(*this))
93 {}
94
95
96 InsetFormula::InsetFormula(InsetFormula const & other)
97         : InsetFormulaBase(other),
98           par_(other.par_),
99           preview_(new PreviewImpl(*this))
100 {}
101
102
103 InsetFormula::InsetFormula(BufferView * bv)
104         : par_(MathAtom(new MathHullInset)),
105           preview_(new PreviewImpl(*this))
106 {
107         view_ = bv->owner()->view();
108 }
109
110
111 InsetFormula::InsetFormula(string const & data)
112         : par_(MathAtom(new MathHullInset)),
113           preview_(new PreviewImpl(*this))
114 {
115         if (!data.size())
116                 return;
117         if (!mathed_parse_normal(par_, data))
118                 lyxerr << "cannot interpret '" << data << "' as math\n";
119 }
120
121
122
123 InsetFormula::~InsetFormula()
124 {}
125
126
127 Inset * InsetFormula::clone(Buffer const &, bool) const
128 {
129         return new InsetFormula(*this);
130 }
131
132
133 void InsetFormula::write(Buffer const *, ostream & os) const
134 {
135         WriteStream wi(os, false, false);
136         os << par_->fileInsetLabel() << " ";
137         par_->write(wi);
138 }
139
140
141 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
142 {
143         WriteStream wi(os, fragile, true);
144         par_->write(wi);
145         return wi.line();
146 }
147
148
149 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
150 {
151         if (display()) {
152                 TextMetricsInfo mi;
153                 par()->metricsT(mi);
154                 TextPainter tpain(par()->width(), par()->height());
155                 par()->drawT(tpain, 0, par()->ascent());
156                 tpain.show(os, 3);
157                 // reset metrics cache to "real" values
158                 metrics();
159                 return tpain.textheight();
160         } else {
161                 WriteStream wi(os, false, true);
162                 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
163                 return wi.line();
164         }
165 }
166
167
168 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
169 {
170         return docbook(buf, os, false);
171 }
172
173
174 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
175 {
176         MathMLStream ms(os);
177         ms << MTag("equation");
178         ms <<   MTag("alt");
179         ms <<    "<[CDATA[";
180         int res = ascii(buf, ms.os(), 0);
181         ms <<    "]]>";
182         ms <<   ETag("alt");
183         ms <<   MTag("math");
184         ms <<    par_.nucleus();
185         ms <<   ETag("math");
186         ms << ETag("equation");
187         return ms.line() + res;
188 }
189
190
191 void InsetFormula::read(Buffer const *, LyXLex & lex)
192 {
193         mathed_parse_normal(par_, lex);
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, bool) const
207 {
208         // This initiates the loading of the preview, so should come
209         // before the metrics are computed.
210         view_ = bv->owner()->view();
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         MathPainterInfo pi(bv->painter());
220
221         if (use_preview) {
222                 pi.pain.image(x, y - a, w, h,
223                               *(preview_->pimage()->image(*this, *bv)));
224         } else {
225                 //pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
226                 pi.base.style = LM_ST_TEXT;
227                 pi.base.font  = font;
228                 pi.base.font.setColor(LColor::math);
229                 if (lcolor.getX11Name(LColor::mathbg)
230                             != lcolor.getX11Name(LColor::background))
231                         pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
232
233                 if (mathcursor &&
234                                 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
235                 {
236                         mathcursor->drawSelection(pi);
237                         //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
238                 }
239
240                 par_->draw(pi, x + 1, y);
241         }
242
243         xx += w;
244         xo_ = x;
245         yo_ = y;
246
247         setCursorVisible(false);
248 }
249
250
251 vector<string> const InsetFormula::getLabelList() const
252 {
253         vector<string> res;
254         par()->getLabelList(res);
255         return res;
256 }
257
258
259 UpdatableInset::RESULT
260 InsetFormula::localDispatch(BufferView * bv, kb_action action,
261          string const & arg)
262 {
263         RESULT result = DISPATCHED;
264
265         switch (action) {
266
267                 case LFUN_BREAKLINE:
268                         bv->lockedInsetStoreUndo(Undo::INSERT);
269                         mathcursor->breakLine();
270                         mathcursor->normalize();
271                         updateLocal(bv, true);
272                         break;
273
274                 case LFUN_MATH_NUMBER:
275                 {
276                         if (!hull())
277                                 break;
278                         //lyxerr << "toggling all numbers\n";
279                         if (display()) {
280                                 bv->lockedInsetStoreUndo(Undo::INSERT);
281                                 bool old = par()->numberedType();
282                                 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
283                                         hull()->numbered(row, !old);
284                                 bv->owner()->message(old ? _("No number") : _("Number"));
285                                 updateLocal(bv, true);
286                         }
287                         break;
288                 }
289
290                 case LFUN_MATH_NONUMBER:
291                 {
292                         //lyxerr << "toggling line number\n";
293                         if (display()) {
294                                 bv->lockedInsetStoreUndo(Undo::INSERT);
295                                 MathCursor::row_type row = mathcursor->hullRow();
296                                 bool old = hull()->numbered(row);
297                                 bv->owner()->message(old ? _("No number") : _("Number"));
298                                 hull()->numbered(row, !old);
299                                 updateLocal(bv, true);
300                         }
301                         break;
302                 }
303
304                 case LFUN_INSERT_LABEL:
305                 {
306                         if (!hull())
307                                 break;
308
309                         bv->lockedInsetStoreUndo(Undo::INSERT);
310
311                         MathCursor::row_type row = mathcursor->hullRow();
312                         string old_label = hull()->label(row);
313                         string new_label = arg;
314
315                         if (new_label.empty()) {
316                                 string const default_label =
317                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
318                                 pair<bool, string> const res = old_label.empty()
319                                         ? Alert::askForText(_("Enter new label to insert:"), default_label)
320                                         : Alert::askForText(_("Enter label:"), old_label);
321                                 if (!res.first)
322                                         break;
323                                 new_label = trim(res.second);
324                         }
325
326                         //if (new_label == old_label)
327                         //      break;  // Nothing to do
328
329                         if (!new_label.empty()) {
330                                 lyxerr << "setting label to '" << new_label << "'\n";
331                                 hull()->numbered(row, true);
332                         }
333
334 #warning FIXME: please check you really mean repaint() ... is it needed,
335 #warning and if so, should it be update() instead ?
336                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
337                                 bv->repaint();
338
339                         hull()->label(row, new_label);
340
341                         updateLocal(bv, true);
342                         break;
343                 }
344
345                 case LFUN_MATH_MUTATE:
346                 {
347                         bv->lockedInsetStoreUndo(Undo::EDIT);
348                         int x;
349                         int y;
350                         mathcursor->getPos(x, y);
351                         mutate(arg);
352                         mathcursor->setPos(x, y);
353                         mathcursor->normalize();
354                         updateLocal(bv, true);
355                         break;
356                 }
357
358                 case LFUN_MATH_EXTERN:
359                 {
360                         bv->lockedInsetStoreUndo(Undo::EDIT);
361                         if (mathcursor)
362                                 mathcursor->handleExtern(arg);
363                         // re-compute inset dimension
364                         metrics(bv);
365                         updateLocal(bv, true);
366                         break;
367                 }
368
369                 case LFUN_MATH_DISPLAY:
370                 {
371                         int x = 0;
372                         int y = 0;
373                         mathcursor->getPos(x, y);
374                         if (hullType() == "simple")
375                                 mutate("equation");
376                         else
377                                 mutate("simple");
378                         mathcursor->setPos(x, y);
379                         mathcursor->normalize();
380                         updateLocal(bv, true);
381                         break;
382                 }
383
384                 case LFUN_PASTESELECTION:
385                 {
386                         string const clip = bv->getClipboard();
387                 if (!clip.empty())
388                                 mathed_parse_normal(par_, clip);
389                         break;
390                 }
391
392                 default:
393                         result = InsetFormulaBase::localDispatch(bv, action, arg);
394         }
395
396         return result;
397 }
398
399
400 bool InsetFormula::display() const
401 {
402         return hullType() != "simple" && hullType() != "none";
403 }
404
405
406 MathHullInset * InsetFormula::hull() const
407 {
408         lyx::Assert(par_->asHullInset());
409         return par_->asHullInset();
410 }
411
412
413 Inset::Code InsetFormula::lyxCode() const
414 {
415         return Inset::MATH_CODE;
416 }
417
418
419 void InsetFormula::validate(LaTeXFeatures & features) const
420 {
421         par_->validate(features);
422 }
423
424
425 bool InsetFormula::insetAllowed(Inset::Code code) const
426 {
427         return
428                 (code == Inset::LABEL_CODE && display())
429                 || code == Inset::REF_CODE
430                 || code == Inset::ERT_CODE;
431 }
432
433
434 int InsetFormula::ascent(BufferView *, LyXFont const &) const
435 {
436         return preview_->previewReady() ?
437                 preview_->pimage()->ascent() : 1 + par_->ascent();
438 }
439
440
441 int InsetFormula::descent(BufferView *, LyXFont const &) const
442 {
443         if (!preview_->previewReady())
444                 return 1 + par_->descent();
445
446         int const descent = preview_->pimage()->descent();
447         return display() ? descent + 12 : descent;
448 }
449
450
451 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
452 {
453         metrics(bv, font);
454         return preview_->previewReady() ?
455                 preview_->pimage()->width() : par_->width();
456 }
457
458
459 string InsetFormula::hullType() const
460 {
461         return par()->getType();
462 }
463
464
465 void InsetFormula::mutate(string const & type)
466 {
467         par()->mutate(type);
468 }
469
470
471 //
472 // preview stuff
473 //
474
475 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
476 {
477         preview_->addPreview(ploader);
478 }
479
480
481 void InsetFormula::generatePreview() const
482 {
483         preview_->generatePreview();
484 }
485
486
487 bool InsetFormula::PreviewImpl::previewWanted() const
488 {
489         return !parent().par_->asNestInset()->editing();
490 }
491
492
493 string const InsetFormula::PreviewImpl::latexString() const
494 {
495         ostringstream ls;
496         WriteStream wi(ls, false, false);
497         parent().par_->write(wi);
498         return ls.str().c_str();
499 }