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
24 #include "commandtags.h"
25 #include "math_cursor.h"
26 #include "math_parser.h"
27 #include "math_charinset.h"
29 #include "BufferView.h"
32 #include "lyx_gui_misc.h"
33 #include "support/LOstream.h"
34 #include "support/LAssert.h"
35 #include "support/lyxlib.h"
36 #include "support/syscall.h"
37 #include "support/lstrings.h"
41 #include "math_matrixinset.h"
42 #include "mathed/support.h"
53 InsetFormula::InsetFormula()
54 : par_(MathAtom(new MathMatrixInset))
58 InsetFormula::InsetFormula(MathInsetTypes t)
59 : par_(MathAtom(new MathMatrixInset(t)))
63 InsetFormula::InsetFormula(string const & s)
66 bool res = mathed_parse_normal(par_, s);
69 res = mathed_parse_normal(par_, "$" + s + "$");
72 lyxerr << "cannot interpret '" << s << "' as math\n";
73 par_ = MathAtom(new MathMatrixInset(LM_OT_SIMPLE));
80 Inset * InsetFormula::clone(Buffer const &, bool) const
82 return new InsetFormula(*this);
86 void InsetFormula::write(Buffer const * buf, ostream & os) const
89 latex(buf, os, false, false);
93 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
95 par_->write(os, fragile);
100 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
102 par_->write(os, false);
107 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
109 return ascii(buf, os, 0);
113 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
115 return ascii(buf, os, 0);
119 void InsetFormula::read(Buffer const *, LyXLex & lex)
121 mathed_parse_normal(par_, lex);
126 void InsetFormula::draw(BufferView * bv, LyXFont const &,
127 int y, float & xx, bool) const
132 MathInset::workwidth = bv->workWidth();
133 Painter & pain = bv->painter();
136 int w = par_->width();
137 int h = par_->height();
138 int a = par_->ascent();
139 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
141 if (mathcursor && mathcursor->formula() == this) {
142 mathcursor->drawSelection(pain);
143 pain.rectangle(x, y - a, w, h, LColor::mathframe);
146 par_->draw(pain, x, y);
149 setCursorVisible(false);
153 void InsetFormula::metrics() const
155 par_->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
159 vector<string> const InsetFormula::getLabelList() const
161 return mat()->getLabelList();
165 UpdatableInset::RESULT
166 InsetFormula::localDispatch(BufferView * bv, kb_action action,
169 RESULT result = DISPATCHED;
174 bv->lockedInsetStoreUndo(Undo::INSERT);
175 mathcursor->breakLine();
176 mathcursor->normalize();
177 updateLocal(bv, true);
180 case LFUN_MATH_NUMBER:
182 //lyxerr << "toggling all numbers\n";
184 bv->lockedInsetStoreUndo(Undo::INSERT);
185 bool old = mat()->numberedType();
186 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
187 mat()->numbered(row, !old);
188 bv->owner()->message(old ? _("No number") : _("Number"));
189 updateLocal(bv, true);
194 case LFUN_MATH_NONUMBER:
196 //lyxerr << "toggling line number\n";
198 bv->lockedInsetStoreUndo(Undo::INSERT);
199 MathCursor::row_type row = mathcursor->row();
200 bool old = mat()->numbered(row);
201 bv->owner()->message(old ? _("No number") : _("Number"));
202 mat()->numbered(row, !old);
203 updateLocal(bv, true);
208 case LFUN_INSERT_LABEL:
210 bv->lockedInsetStoreUndo(Undo::INSERT);
212 MathCursor::row_type row = mathcursor->row();
213 string old_label = mat()->label(row);
214 string new_label = arg;
216 if (new_label.empty()) {
217 string const default_label =
218 (lyxrc.label_init_length >= 0) ? "eq:" : "";
219 pair<bool, string> const res = old_label.empty()
220 ? askForText(_("Enter new label to insert:"), default_label)
221 : askForText(_("Enter label:"), old_label);
223 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
226 new_label = frontStrip(strip(res.second));
229 //if (new_label == old_label)
230 // break; // Nothing to do
232 if (!new_label.empty()) {
233 lyxerr << "setting label to '" << new_label << "'\n";
234 mat()->numbered(row, true);
237 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
240 mat()->label(row, new_label);
242 updateLocal(bv, true);
246 case LFUN_MATH_EXTERN:
247 bv->lockedInsetStoreUndo(Undo::EDIT);
248 handleExtern(arg, bv);
249 updateLocal(bv, true);
252 case LFUN_MATH_MUTATE:
254 bv->lockedInsetStoreUndo(Undo::EDIT);
257 mathcursor->getPos(x, y);
259 mathcursor->setPos(x, y);
260 mathcursor->normalize();
261 updateLocal(bv, true);
265 case LFUN_MATH_DISPLAY:
269 mathcursor->getPos(x, y);
270 if (mat()->getType() == LM_OT_SIMPLE)
271 mat()->mutate(LM_OT_EQUATION);
273 mat()->mutate(LM_OT_SIMPLE);
274 mathcursor->setPos(x, y);
275 mathcursor->normalize();
276 updateLocal(bv, true);
280 case LFUN_PASTESELECTION:
282 string const clip = bv->getClipboard();
284 mathed_parse_normal(par_, clip);
288 case LFUN_MATH_COLUMN_INSERT:
290 if (mat()->getType() == LM_OT_ALIGN)
291 mat()->mutate(LM_OT_ALIGNAT);
292 mat()->addCol(mat()->ncols());
293 mathcursor->normalize();
294 updateLocal(bv, true);
298 result = InsetFormulaBase::localDispatch(bv, action, arg);
305 void InsetFormula::handleExtern(const string & arg, BufferView *)
308 MathArray & ar = mathcursor->cursor().cell();
310 // find position of last '=' in the array for handleExtern
311 MathArray::size_type pos = ar.size();
312 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
313 if ((*it)->getChar() == '=')
314 pos = it - ar.begin();
316 // delete everything behind this position
317 ar.erase(pos, ar.size());
319 // create normalized expression
320 //string outfile = lyx::tempName("maple.out");
321 string outfile = "/tmp/lyx2" + arg + ".out";
324 string code = os.str().c_str();
326 // run external sript
327 string script = "lyx2" + arg + " '" + code + "' " + outfile;
328 lyxerr << "calling: " << script << endl;
329 Systemcalls cmd(Systemcalls::System, script, 0);
332 ar.push_back(MathAtom(new MathCharInset('=')));
335 ifstream is(outfile.c_str());
336 mathed_parse_cell(ar, is);
339 // re-compute inset dimension
344 bool InsetFormula::display() const
346 return mat()->getType() != LM_OT_SIMPLE;
350 MathMatrixInset const * InsetFormula::mat() const
352 lyx::Assert(par_->asMatrixInset());
353 return par_->asMatrixInset();
357 MathMatrixInset * InsetFormula::mat()
359 lyx::Assert(par_->asMatrixInset());
360 return par_->asMatrixInset();
364 Inset::Code InsetFormula::lyxCode() const
366 return Inset::MATH_CODE;
370 void InsetFormula::validate(LaTeXFeatures & features) const
372 par_->validate(features);
376 bool InsetFormula::insetAllowed(Inset::Code code) const
378 return code == Inset::LABEL_CODE && display();
382 int InsetFormula::ascent(BufferView *, LyXFont const &) const
384 return par_->ascent() + 2;
388 int InsetFormula::descent(BufferView *, LyXFont const &) const
390 return par_->descent() - 2;
394 int InsetFormula::width(BufferView *, LyXFont const &) const
397 return par_->width();
401 MathInsetTypes InsetFormula::getType() const
403 return mat()->getType();