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"
28 #include "BufferView.h"
31 #include "lyx_gui_misc.h"
32 #include "support/LOstream.h"
33 #include "support/LAssert.h"
34 #include "support/lyxlib.h"
35 #include "support/syscall.h"
36 #include "support/lstrings.h"
37 #include "support/filetools.h" // LibFileSearch
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());
67 MathArray pipeThroughExtern(string const & arg, MathArray const & ar)
71 istringstream iss(arg.c_str());
76 // create normalized expression
77 string outfile = lyx::tempName(string(), "mathextern");
79 os << "[" << extra << ' ';
82 string code = os.str().c_str();
85 string file = LibFileSearch("mathed", "extern_" + lang);
87 lyxerr << "converter to '" << lang << "' not found\n";
90 string script = file + " '" + code + "' " + outfile;
91 lyxerr << "calling: " << script << endl;
92 Systemcalls cmd(Systemcalls::System, script, 0);
96 mathed_parse_cell(res, GetFileContents(outfile));
103 InsetFormula::InsetFormula()
104 : par_(MathAtom(new MathMatrixInset))
108 InsetFormula::InsetFormula(MathInsetTypes t)
109 : par_(MathAtom(new MathMatrixInset(t)))
113 InsetFormula::InsetFormula(string const & s)
116 bool res = mathed_parse_normal(par_, s);
119 res = mathed_parse_normal(par_, "$" + s + "$");
122 lyxerr << "cannot interpret '" << s << "' as math\n";
123 par_ = MathAtom(new MathMatrixInset(LM_OT_SIMPLE));
130 Inset * InsetFormula::clone(Buffer const &, bool) const
132 return new InsetFormula(*this);
136 void InsetFormula::write(Buffer const * buf, ostream & os) const
139 latex(buf, os, false, false);
143 int InsetFormula::latex(Buffer const * buf, ostream & os, bool fragil, bool)
146 MathWriteInfo wi(buf, os, fragil);
152 int InsetFormula::ascii(Buffer const * buf, ostream & os, int) const
154 MathWriteInfo wi(buf, os, false);
160 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
162 return ascii(buf, os, 0);
166 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
168 return ascii(buf, os, 0);
172 void InsetFormula::read(Buffer const *, LyXLex & lex)
174 mathed_parse_normal(par_, lex);
179 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
180 int y, float & xx, bool) const
185 Painter & pain = bv->painter();
188 int w = par_->width();
189 int h = par_->height();
190 int a = par_->ascent();
191 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
193 if (mathcursor && mathcursor->formula() == this) {
194 mathcursor->drawSelection(pain);
195 pain.rectangle(x, y - a, w, h, LColor::mathframe);
198 par_->draw(pain, x, y);
203 setCursorVisible(false);
207 vector<string> const InsetFormula::getLabelList() const
209 return mat()->getLabelList();
213 UpdatableInset::RESULT
214 InsetFormula::localDispatch(BufferView * bv, kb_action action,
217 RESULT result = DISPATCHED;
222 bv->lockedInsetStoreUndo(Undo::INSERT);
223 mathcursor->breakLine();
224 mathcursor->normalize();
225 updateLocal(bv, true);
228 case LFUN_MATH_NUMBER:
230 //lyxerr << "toggling all numbers\n";
232 bv->lockedInsetStoreUndo(Undo::INSERT);
233 bool old = mat()->numberedType();
234 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
235 mat()->numbered(row, !old);
236 bv->owner()->message(old ? _("No number") : _("Number"));
237 updateLocal(bv, true);
242 case LFUN_MATH_NONUMBER:
244 //lyxerr << "toggling line number\n";
246 bv->lockedInsetStoreUndo(Undo::INSERT);
247 MathCursor::row_type row = mathcursor->row();
248 bool old = mat()->numbered(row);
249 bv->owner()->message(old ? _("No number") : _("Number"));
250 mat()->numbered(row, !old);
251 updateLocal(bv, true);
256 case LFUN_INSERT_LABEL:
258 bv->lockedInsetStoreUndo(Undo::INSERT);
260 MathCursor::row_type row = mathcursor->row();
261 string old_label = mat()->label(row);
262 string new_label = arg;
264 if (new_label.empty()) {
265 string const default_label =
266 (lyxrc.label_init_length >= 0) ? "eq:" : "";
267 pair<bool, string> const res = old_label.empty()
268 ? askForText(_("Enter new label to insert:"), default_label)
269 : askForText(_("Enter label:"), old_label);
271 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
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 mat()->numbered(row, true);
285 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
288 mat()->label(row, new_label);
290 updateLocal(bv, true);
294 case LFUN_MATH_MUTATE:
296 bv->lockedInsetStoreUndo(Undo::EDIT);
299 mathcursor->getPos(x, y);
301 mathcursor->setPos(x, y);
302 mathcursor->normalize();
303 updateLocal(bv, true);
307 case LFUN_MATH_EXTERN:
309 bv->lockedInsetStoreUndo(Undo::EDIT);
311 // re-compute inset dimension
313 updateLocal(bv, true);
317 case LFUN_MATH_DISPLAY:
321 mathcursor->getPos(x, y);
322 if (mat()->getType() == LM_OT_SIMPLE)
323 mat()->mutate(LM_OT_EQUATION);
325 mat()->mutate(LM_OT_SIMPLE);
326 mathcursor->setPos(x, y);
327 mathcursor->normalize();
328 updateLocal(bv, true);
332 case LFUN_PASTESELECTION:
334 string const clip = bv->getClipboard();
336 mathed_parse_normal(par_, clip);
340 case LFUN_MATH_COLUMN_INSERT:
342 if (mat()->getType() == LM_OT_ALIGN)
343 mat()->mutate(LM_OT_ALIGNAT);
344 mat()->addCol(mat()->ncols());
345 mathcursor->normalize();
346 updateLocal(bv, true);
350 result = InsetFormulaBase::localDispatch(bv, action, arg);
357 void InsetFormula::handleExtern(const string & arg)
363 bool selected = mathcursor->selection();
367 mathcursor->selGet(ar);
368 lyxerr << "use selection: " << ar << "\n";
371 ar = mathcursor->cursor().cell();
372 stripFromLastEqualSign(ar);
373 mathcursor->insert(MathAtom(new MathCharInset('=', LM_TC_VAR)));
374 lyxerr << "use whole cell: " << ar << "\n";
377 mathcursor->insert(pipeThroughExtern(arg, ar));
381 bool InsetFormula::display() const
383 return mat()->getType() != LM_OT_SIMPLE;
387 MathMatrixInset const * InsetFormula::mat() const
389 lyx::Assert(par_->asMatrixInset());
390 return par_->asMatrixInset();
394 MathMatrixInset * InsetFormula::mat()
396 lyx::Assert(par_->asMatrixInset());
397 return par_->asMatrixInset();
401 Inset::Code InsetFormula::lyxCode() const
403 return Inset::MATH_CODE;
407 void InsetFormula::validate(LaTeXFeatures & features) const
409 par_->validate(features);
413 bool InsetFormula::insetAllowed(Inset::Code code) const
415 return code == Inset::LABEL_CODE && display();
419 int InsetFormula::ascent(BufferView *, LyXFont const &) const
421 return par_->ascent() + 2;
425 int InsetFormula::descent(BufferView *, LyXFont const &) const
427 return par_->descent() - 2;
431 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
434 return par_->width();
438 MathInsetTypes InsetFormula::getType() const
440 return mat()->getType();