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"
56 InsetFormula::InsetFormula()
57 : par_(MathAtom(new MathHullInset))
61 InsetFormula::InsetFormula(MathInsetTypes t)
62 : par_(MathAtom(new MathHullInset(t)))
66 InsetFormula::InsetFormula(string const & s)
69 bool res = mathed_parse_normal(par_, s);
72 res = mathed_parse_normal(par_, "$" + s + "$");
75 lyxerr << "cannot interpret '" << s << "' as math\n";
76 par_ = MathAtom(new MathHullInset(LM_OT_SIMPLE));
83 Inset * InsetFormula::clone(Buffer const &, bool) const
85 return new InsetFormula(*this);
89 void InsetFormula::write(Buffer const *, ostream & os) const
92 WriteStream wi(os, false, false);
97 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
99 WriteStream wi(os, fragile, true);
105 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
110 TextPainter tpain(par()->width(), par()->height());
111 par()->drawT(tpain, 0, par()->ascent());
113 // reset metrics cache to "real" values
115 return tpain.textheight();
117 WriteStream wi(os, false, true);
124 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
126 return docbook(buf, os, false);
130 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
133 ms << MTag("equation");
136 int res = ascii(buf, ms.os(), 0);
140 ms << par_.nucleus();
142 ms << ETag("equation");
143 return ms.line() + res;
147 void InsetFormula::read(Buffer const *, LyXLex & lex)
149 mathed_parse_normal(par_, lex);
154 //ostream & operator<<(ostream & os, LyXCursor const & c)
156 // os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
161 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
162 int y, float & xx, bool) const
167 int w = par_->width();
168 int h = par_->height();
169 int a = par_->ascent();
171 MathPainterInfo pi(bv->painter());
173 pi.base.font.setColor(LColor::math);
174 pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
176 if (lcolor.getX11Name(LColor::mathbg)!=lcolor.getX11Name(LColor::background))
177 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
180 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
182 mathcursor->drawSelection(pi);
183 pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
186 par_->draw(pi, x, y);
191 WriteStream wi(os, false, false);
193 if (grfx::ImagePtr image = preview(os.str()))
194 pain.image(x, y, w, h, *image);
201 setCursorVisible(false);
205 vector<string> const InsetFormula::getLabelList() const
207 return hull()->getLabelList();
211 UpdatableInset::RESULT
212 InsetFormula::localDispatch(BufferView * bv, kb_action action,
215 RESULT result = DISPATCHED;
220 bv->lockedInsetStoreUndo(Undo::INSERT);
221 mathcursor->breakLine();
222 mathcursor->normalize();
223 updateLocal(bv, true);
226 case LFUN_MATH_NUMBER:
228 //lyxerr << "toggling all numbers\n";
230 bv->lockedInsetStoreUndo(Undo::INSERT);
231 bool old = hull()->numberedType();
232 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
233 hull()->numbered(row, !old);
234 bv->owner()->message(old ? _("No number") : _("Number"));
235 updateLocal(bv, true);
240 case LFUN_MATH_NONUMBER:
242 //lyxerr << "toggling line number\n";
244 bv->lockedInsetStoreUndo(Undo::INSERT);
245 MathCursor::row_type row = mathcursor->hullRow();
246 bool old = hull()->numbered(row);
247 bv->owner()->message(old ? _("No number") : _("Number"));
248 hull()->numbered(row, !old);
249 updateLocal(bv, true);
254 case LFUN_INSERT_LABEL:
256 bv->lockedInsetStoreUndo(Undo::INSERT);
258 MathCursor::row_type row = mathcursor->hullRow();
259 string old_label = hull()->label(row);
260 string new_label = arg;
262 if (new_label.empty()) {
263 string const default_label =
264 (lyxrc.label_init_length >= 0) ? "eq:" : "";
265 pair<bool, string> const res = old_label.empty()
266 ? Alert::askForText(_("Enter new label to insert:"), default_label)
267 : Alert::askForText(_("Enter label:"), old_label);
270 new_label = frontStrip(strip(res.second));
273 //if (new_label == old_label)
274 // break; // Nothing to do
276 if (!new_label.empty()) {
277 lyxerr << "setting label to '" << new_label << "'\n";
278 hull()->numbered(row, true);
281 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
284 hull()->label(row, new_label);
286 updateLocal(bv, true);
290 case LFUN_MATH_MUTATE:
292 bv->lockedInsetStoreUndo(Undo::EDIT);
295 mathcursor->getPos(x, y);
297 mathcursor->setPos(x, y);
298 mathcursor->normalize();
299 updateLocal(bv, true);
303 case LFUN_MATH_EXTERN:
305 bv->lockedInsetStoreUndo(Undo::EDIT);
307 mathcursor->handleExtern(arg);
308 // re-compute inset dimension
310 updateLocal(bv, true);
314 case LFUN_MATH_DISPLAY:
318 mathcursor->getPos(x, y);
319 if (getType() == LM_OT_SIMPLE)
320 hull()->mutate(LM_OT_EQUATION);
322 hull()->mutate(LM_OT_SIMPLE);
323 mathcursor->setPos(x, y);
324 mathcursor->normalize();
325 updateLocal(bv, true);
329 case LFUN_PASTESELECTION:
331 string const clip = bv->getClipboard();
333 mathed_parse_normal(par_, clip);
338 result = InsetFormulaBase::localDispatch(bv, action, arg);
345 bool InsetFormula::display() const
347 return getType() != LM_OT_SIMPLE;
351 MathHullInset const * InsetFormula::hull() const
353 lyx::Assert(par_->asHullInset());
354 return par_->asHullInset();
358 MathHullInset * InsetFormula::hull()
360 lyx::Assert(par_->asHullInset());
361 return par_->asHullInset();
365 Inset::Code InsetFormula::lyxCode() const
367 return Inset::MATH_CODE;
371 void InsetFormula::validate(LaTeXFeatures & features) const
373 par_->validate(features);
377 bool InsetFormula::insetAllowed(Inset::Code code) const
380 (code == Inset::LABEL_CODE && display())
381 || code == Inset::ERT_CODE;
385 int InsetFormula::ascent(BufferView *, LyXFont const &) const
387 return par_->ascent() + 1;
391 int InsetFormula::descent(BufferView *, LyXFont const &) const
393 return par_->descent() + 1;
397 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
400 return par_->width();
404 MathInsetTypes InsetFormula::getType() const
406 return hull()->getType();