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(string(), "lyx2" + 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_EXTERN:
295 bv->lockedInsetStoreUndo(Undo::EDIT);
297 // re-compute inset dimension
299 updateLocal(bv, true);
302 case LFUN_MATH_MUTATE:
304 bv->lockedInsetStoreUndo(Undo::EDIT);
307 mathcursor->getPos(x, y);
309 mathcursor->setPos(x, y);
310 mathcursor->normalize();
311 updateLocal(bv, true);
315 case LFUN_MATH_DISPLAY:
319 mathcursor->getPos(x, y);
320 if (mat()->getType() == LM_OT_SIMPLE)
321 mat()->mutate(LM_OT_EQUATION);
323 mat()->mutate(LM_OT_SIMPLE);
324 mathcursor->setPos(x, y);
325 mathcursor->normalize();
326 updateLocal(bv, true);
330 case LFUN_PASTESELECTION:
332 string const clip = bv->getClipboard();
334 mathed_parse_normal(par_, clip);
338 case LFUN_MATH_COLUMN_INSERT:
340 if (mat()->getType() == LM_OT_ALIGN)
341 mat()->mutate(LM_OT_ALIGNAT);
342 mat()->addCol(mat()->ncols());
343 mathcursor->normalize();
344 updateLocal(bv, true);
348 result = InsetFormulaBase::localDispatch(bv, action, arg);
355 void InsetFormula::handleExtern(const string & arg)
361 bool selected = mathcursor->selection();
365 mathcursor->selGet(ar);
366 lyxerr << "use selection: " << ar << "\n";
369 ar = mathcursor->cursor().cell();
370 stripFromLastEqualSign(ar);
371 mathcursor->insert(MathAtom(new MathCharInset('=', LM_TC_VAR)));
372 lyxerr << "use whole cell: " << ar << "\n";
375 mathcursor->insert(pipeThroughExtern(arg, ar));
379 bool InsetFormula::display() const
381 return mat()->getType() != LM_OT_SIMPLE;
385 MathMatrixInset const * InsetFormula::mat() const
387 lyx::Assert(par_->asMatrixInset());
388 return par_->asMatrixInset();
392 MathMatrixInset * InsetFormula::mat()
394 lyx::Assert(par_->asMatrixInset());
395 return par_->asMatrixInset();
399 Inset::Code InsetFormula::lyxCode() const
401 return Inset::MATH_CODE;
405 void InsetFormula::validate(LaTeXFeatures & features) const
407 par_->validate(features);
411 bool InsetFormula::insetAllowed(Inset::Code code) const
413 return code == Inset::LABEL_CODE && display();
417 int InsetFormula::ascent(BufferView *, LyXFont const &) const
419 return par_->ascent() + 2;
423 int InsetFormula::descent(BufferView *, LyXFont const &) const
425 return par_->descent() - 2;
429 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
432 return par_->width();
436 MathInsetTypes InsetFormula::getType() const
438 return mat()->getType();