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 * buf, ostream & os, bool fragil, bool)
96 MathWriteInfo wi(buf, os, fragil);
102 int InsetFormula::ascii(Buffer const * buf, ostream & os, int) const
104 MathWriteInfo wi(buf, os, false);
110 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
112 return ascii(buf, os, 0);
116 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
118 return ascii(buf, os, 0);
122 void InsetFormula::read(Buffer const *, LyXLex & lex)
124 mathed_parse_normal(par_, lex);
129 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
130 int y, float & xx, bool) const
135 MathInset::workwidth = bv->workWidth();
136 Painter & pain = bv->painter();
139 int w = par_->width();
140 int h = par_->height();
141 int a = par_->ascent();
142 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
144 if (mathcursor && mathcursor->formula() == this) {
145 mathcursor->drawSelection(pain);
146 pain.rectangle(x, y - a, w, h, LColor::mathframe);
149 par_->draw(pain, x, y);
152 setCursorVisible(false);
156 vector<string> const InsetFormula::getLabelList() const
158 return mat()->getLabelList();
162 UpdatableInset::RESULT
163 InsetFormula::localDispatch(BufferView * bv, kb_action action,
166 RESULT result = DISPATCHED;
171 bv->lockedInsetStoreUndo(Undo::INSERT);
172 mathcursor->breakLine();
173 mathcursor->normalize();
174 updateLocal(bv, true);
177 case LFUN_MATH_NUMBER:
179 //lyxerr << "toggling all numbers\n";
181 bv->lockedInsetStoreUndo(Undo::INSERT);
182 bool old = mat()->numberedType();
183 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
184 mat()->numbered(row, !old);
185 bv->owner()->message(old ? _("No number") : _("Number"));
186 updateLocal(bv, true);
191 case LFUN_MATH_NONUMBER:
193 //lyxerr << "toggling line number\n";
195 bv->lockedInsetStoreUndo(Undo::INSERT);
196 MathCursor::row_type row = mathcursor->row();
197 bool old = mat()->numbered(row);
198 bv->owner()->message(old ? _("No number") : _("Number"));
199 mat()->numbered(row, !old);
200 updateLocal(bv, true);
205 case LFUN_INSERT_LABEL:
207 bv->lockedInsetStoreUndo(Undo::INSERT);
209 MathCursor::row_type row = mathcursor->row();
210 string old_label = mat()->label(row);
211 string new_label = arg;
213 if (new_label.empty()) {
214 string const default_label =
215 (lyxrc.label_init_length >= 0) ? "eq:" : "";
216 pair<bool, string> const res = old_label.empty()
217 ? askForText(_("Enter new label to insert:"), default_label)
218 : askForText(_("Enter label:"), old_label);
220 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
223 new_label = frontStrip(strip(res.second));
226 //if (new_label == old_label)
227 // break; // Nothing to do
229 if (!new_label.empty()) {
230 lyxerr << "setting label to '" << new_label << "'\n";
231 mat()->numbered(row, true);
234 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
237 mat()->label(row, new_label);
239 updateLocal(bv, true);
243 case LFUN_MATH_EXTERN:
244 bv->lockedInsetStoreUndo(Undo::EDIT);
245 handleExtern(arg, bv);
246 updateLocal(bv, true);
249 case LFUN_MATH_MUTATE:
251 bv->lockedInsetStoreUndo(Undo::EDIT);
254 mathcursor->getPos(x, y);
256 mathcursor->setPos(x, y);
257 mathcursor->normalize();
258 updateLocal(bv, true);
262 case LFUN_MATH_DISPLAY:
266 mathcursor->getPos(x, y);
267 if (mat()->getType() == LM_OT_SIMPLE)
268 mat()->mutate(LM_OT_EQUATION);
270 mat()->mutate(LM_OT_SIMPLE);
271 mathcursor->setPos(x, y);
272 mathcursor->normalize();
273 updateLocal(bv, true);
277 case LFUN_PASTESELECTION:
279 string const clip = bv->getClipboard();
281 mathed_parse_normal(par_, clip);
285 case LFUN_MATH_COLUMN_INSERT:
287 if (mat()->getType() == LM_OT_ALIGN)
288 mat()->mutate(LM_OT_ALIGNAT);
289 mat()->addCol(mat()->ncols());
290 mathcursor->normalize();
291 updateLocal(bv, true);
295 result = InsetFormulaBase::localDispatch(bv, action, arg);
302 void InsetFormula::handleExtern(const string & arg, BufferView * bv)
305 MathArray & ar = mathcursor->cursor().cell();
307 // find position of last '=' in the array for handleExtern
308 MathArray::size_type pos = ar.size();
309 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
310 if ((*it)->getChar() == '=')
311 pos = it - ar.begin();
313 // delete everything behind this position
314 ar.erase(pos, ar.size());
316 // create normalized expression
317 //string outfile = lyx::tempName("maple.out");
318 string outfile = "/tmp/lyx2" + arg + ".out";
321 string code = os.str().c_str();
323 // run external sript
324 string script = "lyx2" + arg + " '" + code + "' " + outfile;
325 lyxerr << "calling: " << script << endl;
326 Systemcalls cmd(Systemcalls::System, script, 0);
329 ar.push_back(MathAtom(new MathCharInset('=')));
332 ifstream is(outfile.c_str());
333 mathed_parse_cell(ar, is);
336 // re-compute inset dimension
341 bool InsetFormula::display() const
343 return mat()->getType() != LM_OT_SIMPLE;
347 MathMatrixInset const * InsetFormula::mat() const
349 lyx::Assert(par_->asMatrixInset());
350 return par_->asMatrixInset();
354 MathMatrixInset * InsetFormula::mat()
356 lyx::Assert(par_->asMatrixInset());
357 return par_->asMatrixInset();
361 Inset::Code InsetFormula::lyxCode() const
363 return Inset::MATH_CODE;
367 void InsetFormula::validate(LaTeXFeatures & features) const
369 par_->validate(features);
373 bool InsetFormula::insetAllowed(Inset::Code code) const
375 return code == Inset::LABEL_CODE && display();
379 int InsetFormula::ascent(BufferView *, LyXFont const &) const
381 return par_->ascent() + 2;
385 int InsetFormula::descent(BufferView *, LyXFont const &) const
387 return par_->descent() - 2;
391 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
394 return par_->width();
398 MathInsetTypes InsetFormula::getType() const
400 return mat()->getType();