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.
8 * Copyright: 1996-1998 Alejandro Aguilar Sierra
10 * Version: 0.4, Lyx project.
12 * You are free to use and modify this code under the terms of
13 * the GNU General Public Licence version 2 or later.
17 #pragma implementation
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_deliminset.h"
30 #include "BufferView.h"
33 #include "frontends/Alert.h"
34 #include "support/LOstream.h"
35 #include "support/LAssert.h"
36 #include "support/filetools.h" // LibFileSearch
37 #include "frontends/LyXView.h"
38 #include "frontends/Painter.h"
40 #include "math_hullinset.h"
41 #include "math_support.h"
42 #include "math_mathmlstream.h"
43 #include "textpainter.h"
46 #include "graphics/GraphicsCacheItem.h"
58 InsetFormula::InsetFormula()
59 : par_(MathAtom(new MathHullInset))
63 InsetFormula::InsetFormula(MathInsetTypes t)
64 : par_(MathAtom(new MathHullInset(t)))
68 InsetFormula::InsetFormula(string const & s)
71 bool res = mathed_parse_normal(par_, s);
74 res = mathed_parse_normal(par_, "$" + s + "$");
77 lyxerr << "cannot interpret '" << s << "' as math\n";
78 par_ = MathAtom(new MathHullInset(LM_OT_SIMPLE));
85 Inset * InsetFormula::clone(Buffer const &, bool) const
87 return new InsetFormula(*this);
91 void InsetFormula::write(Buffer const *, ostream & os) const
94 WriteStream wi(os, false, false);
99 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
101 WriteStream wi(os, fragile, true);
107 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
112 TextPainter tpain(par()->width(), par()->height());
113 par()->drawT(tpain, 0, par()->ascent());
115 // reset metrics cache to "real" values
117 return tpain.textheight();
119 WriteStream wi(os, false, true);
126 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
128 return docbook(buf, os, false);
132 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
135 ms << MTag("equation");
138 int res = ascii(buf, ms.os(), 0);
142 ms << par_.nucleus();
144 ms << ETag("equation");
145 return ms.line() + res;
149 void InsetFormula::read(Buffer const *, LyXLex & lex)
151 mathed_parse_normal(par_, lex);
156 //ostream & operator<<(ostream & os, LyXCursor const & c)
158 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
163 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
164 int y, float & xx, bool) const
169 int w = par_->width();
170 int h = par_->height();
171 int a = par_->ascent();
173 MathPainterInfo pi(bv->painter());
175 pi.base.font.setColor(LColor::math);
176 pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
178 if (lcolor.getX11Name(LColor::mathbg)!=lcolor.getX11Name(LColor::background))
179 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
182 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
184 mathcursor->drawSelection(pi);
185 pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
188 par_->draw(pi, x, y);
193 WriteStream wi(os, false, false);
195 if (preview(os.str(), preview_)) {
196 cerr << "image could be drawn\n";
197 pi.pain.image(x + 40, y, 50, 50, *(preview_->image()));
205 setCursorVisible(false);
209 vector<string> const InsetFormula::getLabelList() const
211 return hull()->getLabelList();
215 UpdatableInset::RESULT
216 InsetFormula::localDispatch(BufferView * bv, kb_action action,
219 RESULT result = DISPATCHED;
224 bv->lockedInsetStoreUndo(Undo::INSERT);
225 mathcursor->breakLine();
226 mathcursor->normalize();
227 updateLocal(bv, true);
230 case LFUN_MATH_NUMBER:
232 //lyxerr << "toggling all numbers\n";
234 bv->lockedInsetStoreUndo(Undo::INSERT);
235 bool old = hull()->numberedType();
236 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
237 hull()->numbered(row, !old);
238 bv->owner()->message(old ? _("No number") : _("Number"));
239 updateLocal(bv, true);
244 case LFUN_MATH_NONUMBER:
246 //lyxerr << "toggling line number\n";
248 bv->lockedInsetStoreUndo(Undo::INSERT);
249 MathCursor::row_type row = mathcursor->hullRow();
250 bool old = hull()->numbered(row);
251 bv->owner()->message(old ? _("No number") : _("Number"));
252 hull()->numbered(row, !old);
253 updateLocal(bv, true);
258 case LFUN_INSERT_LABEL:
260 bv->lockedInsetStoreUndo(Undo::INSERT);
262 MathCursor::row_type row = mathcursor->hullRow();
263 string old_label = hull()->label(row);
264 string new_label = arg;
266 if (new_label.empty()) {
267 string const default_label =
268 (lyxrc.label_init_length >= 0) ? "eq:" : "";
269 pair<bool, string> const res = old_label.empty()
270 ? Alert::askForText(_("Enter new label to insert:"), default_label)
271 : Alert::askForText(_("Enter label:"), old_label);
274 new_label = frontStrip(strip(res.second));
277 //if (new_label == old_label)
278 // break; // Nothing to do
280 if (!new_label.empty()) {
281 lyxerr << "setting label to '" << new_label << "'\n";
282 hull()->numbered(row, true);
285 #warning FIXME: please check you really mean repaint() ... is it needed,
286 #warning and if so, should it be update() instead ?
287 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
290 hull()->label(row, new_label);
292 updateLocal(bv, true);
296 case LFUN_MATH_MUTATE:
298 bv->lockedInsetStoreUndo(Undo::EDIT);
301 mathcursor->getPos(x, y);
303 mathcursor->setPos(x, y);
304 mathcursor->normalize();
305 updateLocal(bv, true);
309 case LFUN_MATH_EXTERN:
311 bv->lockedInsetStoreUndo(Undo::EDIT);
313 mathcursor->handleExtern(arg);
314 // re-compute inset dimension
316 updateLocal(bv, true);
320 case LFUN_MATH_DISPLAY:
324 mathcursor->getPos(x, y);
325 if (getType() == LM_OT_SIMPLE)
326 hull()->mutate(LM_OT_EQUATION);
328 hull()->mutate(LM_OT_SIMPLE);
329 mathcursor->setPos(x, y);
330 mathcursor->normalize();
331 updateLocal(bv, true);
335 case LFUN_PASTESELECTION:
337 string const clip = bv->getClipboard();
339 mathed_parse_normal(par_, clip);
344 result = InsetFormulaBase::localDispatch(bv, action, arg);
351 bool InsetFormula::display() const
353 return getType() != LM_OT_SIMPLE;
357 MathHullInset const * InsetFormula::hull() const
359 lyx::Assert(par_->asHullInset());
360 return par_->asHullInset();
364 MathHullInset * InsetFormula::hull()
366 lyx::Assert(par_->asHullInset());
367 return par_->asHullInset();
371 Inset::Code InsetFormula::lyxCode() const
373 return Inset::MATH_CODE;
377 void InsetFormula::validate(LaTeXFeatures & features) const
379 par_->validate(features);
383 bool InsetFormula::insetAllowed(Inset::Code code) const
386 (code == Inset::LABEL_CODE && display())
387 || code == Inset::REF_CODE
388 || code == Inset::ERT_CODE;
392 int InsetFormula::ascent(BufferView *, LyXFont const &) const
394 return par_->ascent() + 1;
398 int InsetFormula::descent(BufferView *, LyXFont const &) const
400 return par_->descent() + 1;
404 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
407 return par_->width();
411 MathInsetTypes InsetFormula::getType() const
413 return hull()->getType();