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);
167 setCursorVisible(false);
171 vector<string> const InsetFormula::getLabelList() const
173 return mat()->getLabelList();
177 UpdatableInset::RESULT
178 InsetFormula::localDispatch(BufferView * bv, kb_action action,
181 RESULT result = DISPATCHED;
186 bv->lockedInsetStoreUndo(Undo::INSERT);
187 mathcursor->breakLine();
188 mathcursor->normalize();
189 updateLocal(bv, true);
192 case LFUN_MATH_NUMBER:
194 //lyxerr << "toggling all numbers\n";
196 bv->lockedInsetStoreUndo(Undo::INSERT);
197 bool old = mat()->numberedType();
198 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
199 mat()->numbered(row, !old);
200 bv->owner()->message(old ? _("No number") : _("Number"));
201 updateLocal(bv, true);
206 case LFUN_MATH_NONUMBER:
208 //lyxerr << "toggling line number\n";
210 bv->lockedInsetStoreUndo(Undo::INSERT);
211 MathCursor::row_type row = mathcursor->row();
212 bool old = mat()->numbered(row);
213 bv->owner()->message(old ? _("No number") : _("Number"));
214 mat()->numbered(row, !old);
215 updateLocal(bv, true);
220 case LFUN_INSERT_LABEL:
222 bv->lockedInsetStoreUndo(Undo::INSERT);
224 MathCursor::row_type row = mathcursor->row();
225 string old_label = mat()->label(row);
226 string new_label = arg;
228 if (new_label.empty()) {
229 string const default_label =
230 (lyxrc.label_init_length >= 0) ? "eq:" : "";
231 pair<bool, string> const res = old_label.empty()
232 ? askForText(_("Enter new label to insert:"), default_label)
233 : askForText(_("Enter label:"), old_label);
235 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
238 new_label = frontStrip(strip(res.second));
241 //if (new_label == old_label)
242 // break; // Nothing to do
244 if (!new_label.empty()) {
245 lyxerr << "setting label to '" << new_label << "'\n";
246 mat()->numbered(row, true);
249 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
252 mat()->label(row, new_label);
254 updateLocal(bv, true);
258 case LFUN_MATH_EXTERN:
259 bv->lockedInsetStoreUndo(Undo::EDIT);
261 // re-compute inset dimension
263 updateLocal(bv, true);
266 case LFUN_MATH_MUTATE:
268 bv->lockedInsetStoreUndo(Undo::EDIT);
271 mathcursor->getPos(x, y);
273 mathcursor->setPos(x, y);
274 mathcursor->normalize();
275 updateLocal(bv, true);
279 case LFUN_MATH_DISPLAY:
283 mathcursor->getPos(x, y);
284 if (mat()->getType() == LM_OT_SIMPLE)
285 mat()->mutate(LM_OT_EQUATION);
287 mat()->mutate(LM_OT_SIMPLE);
288 mathcursor->setPos(x, y);
289 mathcursor->normalize();
290 updateLocal(bv, true);
294 case LFUN_PASTESELECTION:
296 string const clip = bv->getClipboard();
298 mathed_parse_normal(par_, clip);
302 case LFUN_MATH_COLUMN_INSERT:
304 if (mat()->getType() == LM_OT_ALIGN)
305 mat()->mutate(LM_OT_ALIGNAT);
306 mat()->addCol(mat()->ncols());
307 mathcursor->normalize();
308 updateLocal(bv, true);
312 result = InsetFormulaBase::localDispatch(bv, action, arg);
319 void InsetFormula::handleExtern(const string & arg)
325 MathArray & ar = mathcursor->cursor().cell();
330 istringstream iss(arg.c_str());
331 iss >> lang >> extra;
335 // strip last '=' and everything behind
336 stripFromLastEqualSign(ar);
338 // create normalized expression
339 //string outfile = lyx::tempName("maple.out");
340 string outfile = "/tmp/lyx2" + lang + ".out";
342 os << "[" << extra << ' ';
345 string code = os.str().c_str();
347 // run external sript
348 string script = "lyx2" + arg + " '" + code + "' " + outfile;
349 lyxerr << "calling: " << script << endl;
350 Systemcalls cmd(Systemcalls::System, script, 0);
353 ar.push_back(MathAtom(new MathCharInset('=')));
356 ifstream is(outfile.c_str());
357 mathed_parse_cell(ar, is);
362 bool InsetFormula::display() const
364 return mat()->getType() != LM_OT_SIMPLE;
368 MathMatrixInset const * InsetFormula::mat() const
370 lyx::Assert(par_->asMatrixInset());
371 return par_->asMatrixInset();
375 MathMatrixInset * InsetFormula::mat()
377 lyx::Assert(par_->asMatrixInset());
378 return par_->asMatrixInset();
382 Inset::Code InsetFormula::lyxCode() const
384 return Inset::MATH_CODE;
388 void InsetFormula::validate(LaTeXFeatures & features) const
390 par_->validate(features);
394 bool InsetFormula::insetAllowed(Inset::Code code) const
396 return code == Inset::LABEL_CODE && display();
400 int InsetFormula::ascent(BufferView *, LyXFont const &) const
402 return par_->ascent() + 2;
406 int InsetFormula::descent(BufferView *, LyXFont const &) const
408 return par_->descent() - 2;
412 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
415 return par_->width();
419 MathInsetTypes InsetFormula::getType() const
421 return mat()->getType();