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"
54 void stripFromLastEqualSign(MathArray & ar)
56 // find position of last '=' in the array
57 MathArray::size_type pos = ar.size();
58 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
59 if ((*it)->getChar() == '=')
60 pos = it - ar.begin();
62 // delete everything behind this position
63 ar.erase(pos, ar.size());
69 InsetFormula::InsetFormula()
70 : par_(MathAtom(new MathMatrixInset))
74 InsetFormula::InsetFormula(MathInsetTypes t)
75 : par_(MathAtom(new MathMatrixInset(t)))
79 InsetFormula::InsetFormula(string const & s)
82 bool res = mathed_parse_normal(par_, s);
85 res = mathed_parse_normal(par_, "$" + s + "$");
88 lyxerr << "cannot interpret '" << s << "' as math\n";
89 par_ = MathAtom(new MathMatrixInset(LM_OT_SIMPLE));
96 Inset * InsetFormula::clone(Buffer const &, bool) const
98 return new InsetFormula(*this);
102 void InsetFormula::write(Buffer const * buf, ostream & os) const
105 latex(buf, os, false, false);
109 int InsetFormula::latex(Buffer const * buf, ostream & os, bool fragil, bool)
112 MathWriteInfo wi(buf, os, fragil);
118 int InsetFormula::ascii(Buffer const * buf, ostream & os, int) const
120 MathWriteInfo wi(buf, os, false);
126 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
128 return ascii(buf, os, 0);
132 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
134 return ascii(buf, os, 0);
138 void InsetFormula::read(Buffer const *, LyXLex & lex)
140 mathed_parse_normal(par_, lex);
145 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
146 int y, float & xx, bool) const
151 Painter & pain = bv->painter();
154 int w = par_->width();
155 int h = par_->height();
156 int a = par_->ascent();
157 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
159 if (mathcursor && mathcursor->formula() == this) {
160 mathcursor->drawSelection(pain);
161 pain.rectangle(x, y - a, w, h, LColor::mathframe);
164 par_->draw(pain, x, y);
169 setCursorVisible(false);
173 vector<string> const InsetFormula::getLabelList() const
175 return mat()->getLabelList();
179 UpdatableInset::RESULT
180 InsetFormula::localDispatch(BufferView * bv, kb_action action,
183 RESULT result = DISPATCHED;
188 bv->lockedInsetStoreUndo(Undo::INSERT);
189 mathcursor->breakLine();
190 mathcursor->normalize();
191 updateLocal(bv, true);
194 case LFUN_MATH_NUMBER:
196 //lyxerr << "toggling all numbers\n";
198 bv->lockedInsetStoreUndo(Undo::INSERT);
199 bool old = mat()->numberedType();
200 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
201 mat()->numbered(row, !old);
202 bv->owner()->message(old ? _("No number") : _("Number"));
203 updateLocal(bv, true);
208 case LFUN_MATH_NONUMBER:
210 //lyxerr << "toggling line number\n";
212 bv->lockedInsetStoreUndo(Undo::INSERT);
213 MathCursor::row_type row = mathcursor->row();
214 bool old = mat()->numbered(row);
215 bv->owner()->message(old ? _("No number") : _("Number"));
216 mat()->numbered(row, !old);
217 updateLocal(bv, true);
222 case LFUN_INSERT_LABEL:
224 bv->lockedInsetStoreUndo(Undo::INSERT);
226 MathCursor::row_type row = mathcursor->row();
227 string old_label = mat()->label(row);
228 string new_label = arg;
230 if (new_label.empty()) {
231 string const default_label =
232 (lyxrc.label_init_length >= 0) ? "eq:" : "";
233 pair<bool, string> const res = old_label.empty()
234 ? askForText(_("Enter new label to insert:"), default_label)
235 : askForText(_("Enter label:"), old_label);
237 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
240 new_label = frontStrip(strip(res.second));
243 //if (new_label == old_label)
244 // break; // Nothing to do
246 if (!new_label.empty()) {
247 lyxerr << "setting label to '" << new_label << "'\n";
248 mat()->numbered(row, true);
251 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
254 mat()->label(row, new_label);
256 updateLocal(bv, true);
260 case LFUN_MATH_EXTERN:
261 bv->lockedInsetStoreUndo(Undo::EDIT);
263 // re-compute inset dimension
265 updateLocal(bv, true);
268 case LFUN_MATH_MUTATE:
270 bv->lockedInsetStoreUndo(Undo::EDIT);
273 mathcursor->getPos(x, y);
275 mathcursor->setPos(x, y);
276 mathcursor->normalize();
277 updateLocal(bv, true);
281 case LFUN_MATH_DISPLAY:
285 mathcursor->getPos(x, y);
286 if (mat()->getType() == LM_OT_SIMPLE)
287 mat()->mutate(LM_OT_EQUATION);
289 mat()->mutate(LM_OT_SIMPLE);
290 mathcursor->setPos(x, y);
291 mathcursor->normalize();
292 updateLocal(bv, true);
296 case LFUN_PASTESELECTION:
298 string const clip = bv->getClipboard();
300 mathed_parse_normal(par_, clip);
304 case LFUN_MATH_COLUMN_INSERT:
306 if (mat()->getType() == LM_OT_ALIGN)
307 mat()->mutate(LM_OT_ALIGNAT);
308 mat()->addCol(mat()->ncols());
309 mathcursor->normalize();
310 updateLocal(bv, true);
314 result = InsetFormulaBase::localDispatch(bv, action, arg);
321 void InsetFormula::handleExtern(const string & arg)
327 MathArray & ar = mathcursor->cursor().cell();
332 istringstream iss(arg.c_str());
333 iss >> lang >> extra;
337 // strip last '=' and everything behind
338 stripFromLastEqualSign(ar);
340 // create normalized expression
341 //string outfile = lyx::tempName("maple.out");
342 string outfile = "/tmp/lyx2" + lang + ".out";
344 os << "[" << extra << ' ';
347 string code = os.str().c_str();
349 // run external sript
350 string script = "lyx2" + arg + " '" + code + "' " + outfile;
351 lyxerr << "calling: " << script << endl;
352 Systemcalls cmd(Systemcalls::System, script, 0);
355 ar.push_back(MathAtom(new MathCharInset('=')));
358 ifstream is(outfile.c_str());
359 mathed_parse_cell(ar, is);
364 void InsetFormula::handleExtern(const string & arg)
370 bool selected = mathcursor->selection();
374 mathcursor->selGet(ar);
375 lyxerr << "use selection: " << ar << "\n";
377 ar = mathcursor->cursor().cell();
378 lyxerr << "use whole cell: " << ar << "\n";
385 istringstream iss(arg.c_str());
386 iss >> lang >> extra;
390 // strip last '=' and everything behind
391 stripFromLastEqualSign(ar);
393 // create normalized expression
394 //string outfile = lyx::tempName("maple.out");
395 string outfile = "/tmp/lyx2" + lang + ".out";
397 os << "[" << extra << ' ';
400 string code = os.str().c_str();
402 // run external sript
403 string script = "lyx2" + lang + " '" + code + "' " + outfile;
404 lyxerr << "calling: " << script << endl;
405 Systemcalls cmd(Systemcalls::System, script, 0);
410 br.push_back(MathAtom(new MathCharInset('=', LM_TC_VAR)));
411 ifstream is(outfile.c_str());
412 mathed_parse_cell(br, is);
413 mathcursor->insert(br);
416 bool InsetFormula::display() const
418 return mat()->getType() != LM_OT_SIMPLE;
422 MathMatrixInset const * InsetFormula::mat() const
424 lyx::Assert(par_->asMatrixInset());
425 return par_->asMatrixInset();
429 MathMatrixInset * InsetFormula::mat()
431 lyx::Assert(par_->asMatrixInset());
432 return par_->asMatrixInset();
436 Inset::Code InsetFormula::lyxCode() const
438 return Inset::MATH_CODE;
442 void InsetFormula::validate(LaTeXFeatures & features) const
444 par_->validate(features);
448 bool InsetFormula::insetAllowed(Inset::Code code) const
450 return code == Inset::LABEL_CODE && display();
454 int InsetFormula::ascent(BufferView *, LyXFont const &) const
456 return par_->ascent() + 2;
460 int InsetFormula::descent(BufferView *, LyXFont const &) const
462 return par_->descent() - 2;
466 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
469 return par_->width();
473 MathInsetTypes InsetFormula::getType() const
475 return mat()->getType();