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"
28 #include "BufferView.h"
31 #include "lyx_gui_misc.h"
32 #include "support/LOstream.h"
33 #include "support/lyxlib.h"
34 #include "support/syscall.h"
35 #include "support/lstrings.h"
39 #include "math_matrixinset.h"
40 #include "mathed/support.h"
49 extern char const * latex_mathenv[];
50 extern MathCursor * mathcursor;
53 InsetFormula::InsetFormula()
54 : InsetFormulaBase(new MathMatrixInset)
58 InsetFormula::InsetFormula(MathInsetTypes t)
59 : InsetFormulaBase(new MathMatrixInset(t))
63 InsetFormula::InsetFormula(string const & s)
64 : InsetFormulaBase(mathed_parse(s))
70 Inset * InsetFormula::clone(Buffer const &, bool) const
72 return new InsetFormula(*this);
76 void InsetFormula::write(ostream & os) const
79 latex(os, false, false);
83 int InsetFormula::latex(ostream & os, bool fragile, bool) const
85 par()->write(os, fragile);
90 int InsetFormula::ascii(ostream & os, int) const
92 par()->write(os, false);
97 int InsetFormula::linuxdoc(ostream & os) const
103 int InsetFormula::docBook(ostream & os) const
109 void InsetFormula::read(LyXLex & lex)
111 par(mathed_parse(lex));
116 void InsetFormula::draw(BufferView * bv, LyXFont const &,
117 int y, float & xx, bool) const
122 MathInset::workwidth = bv->workWidth();
123 Painter & pain = bv->painter();
126 int w = par()->width();
127 int h = par()->height();
128 int a = par()->ascent();
129 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
131 if (mathcursor && mathcursor->formula() == this) {
132 mathcursor->drawSelection(pain);
133 pain.rectangle(x, y - a, w, h, LColor::mathframe);
136 par()->draw(pain, x, y);
137 xx += par()->width();
139 setCursorVisible(false);
143 void InsetFormula::metrics() const
145 const_cast<MathInset *>(par_)->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
148 vector<string> const InsetFormula::getLabelList() const
150 return par()->getLabelList();
154 UpdatableInset::RESULT
155 InsetFormula::localDispatch(BufferView * bv, kb_action action,
158 RESULT result = DISPATCHED;
163 bv->lockedInsetStoreUndo(Undo::INSERT);
166 mathcursor->getPos(x, y);
167 mathcursor->breakLine();
168 mathcursor->normalize();
169 updateLocal(bv, true);
172 case LFUN_MATH_NUMBER:
174 //lyxerr << "toggling all numbers\n";
176 bv->lockedInsetStoreUndo(Undo::INSERT);
177 bool old = par()->numberedType();
178 for (int row = 0; row < par()->nrows(); ++row)
179 par()->numbered(row, !old);
180 bv->owner()->message(old ? _("No number") : _("Number"));
181 updateLocal(bv, true);
186 case LFUN_MATH_NONUMBER:
188 //lyxerr << "toggling line number\n";
190 bv->lockedInsetStoreUndo(Undo::INSERT);
191 int row = mathcursor->row();
192 bool old = par()->numbered(row);
193 bv->owner()->message(old ? _("No number") : _("Number"));
194 par()->numbered(row, !old);
195 updateLocal(bv, true);
200 case LFUN_INSERT_LABEL:
202 bv->lockedInsetStoreUndo(Undo::INSERT);
204 int row = mathcursor->row();
205 string old_label = par()->label(row);
206 string new_label = arg;
208 if (new_label.empty()) {
209 string const default_label =
210 (lyxrc.label_init_length >= 0) ? "eq:" : "";
211 pair<bool, string> const res = old_label.empty()
212 ? askForText(_("Enter new label to insert:"), default_label)
213 : askForText(_("Enter label:"), old_label);
215 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
218 new_label = frontStrip(strip(res.second));
221 //if (new_label == old_label)
222 // break; // Nothing to do
224 if (!new_label.empty()) {
225 lyxerr << "setting label to '" << new_label << "'\n";
226 par()->numbered(row, true);
229 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
232 par()->label(row, new_label);
234 updateLocal(bv, true);
238 case LFUN_MATH_EXTERN:
239 bv->lockedInsetStoreUndo(Undo::EDIT);
240 handleExtern(arg, bv);
241 updateLocal(bv, true);
244 case LFUN_MATH_MUTATE:
246 bv->lockedInsetStoreUndo(Undo::EDIT);
249 mathcursor->getPos(x, y);
251 mathcursor->setPos(x, y);
252 mathcursor->normalize();
253 updateLocal(bv, true);
257 case LFUN_MATH_DISPLAY:
261 mathcursor->getPos(x, y);
262 if (par()->getType() == LM_OT_SIMPLE)
263 par()->mutate(LM_OT_EQUATION);
265 par()->mutate(LM_OT_SIMPLE);
266 mathcursor->setPos(x, y);
267 mathcursor->normalize();
268 updateLocal(bv, true);
272 case LFUN_PASTESELECTION:
274 string const clip = bv->getClipboard();
276 par(mathed_parse(clip));
281 result = InsetFormulaBase::localDispatch(bv, action, arg);
288 void InsetFormula::handleExtern(const string & arg, BufferView *)
290 //string outfile = lyx::tempName("maple.out");
291 string outfile = "/tmp/lyx2" + arg + ".out";
293 par()->writeNormal(os);
294 string code = os.str().c_str();
295 string script = "lyx2" + arg + " '" + code + "' " + outfile;
296 lyxerr << "calling: " << script << endl;
297 Systemcalls cmd(Systemcalls::System, script, 0);
299 ifstream is(outfile.c_str());
300 par(mathed_parse(is));
304 bool InsetFormula::display() const
306 return par()->getType() != LM_OT_SIMPLE;
310 MathMatrixInset * InsetFormula::par() const
312 return static_cast<MathMatrixInset *>(par_);
316 void InsetFormula::par(MathInset * p)
319 par_ = p ? p : new MathMatrixInset;
323 Inset::Code InsetFormula::lyxCode() const
325 return Inset::MATH_CODE;
329 void InsetFormula::validate(LaTeXFeatures & features) const
331 par()->validate(features);
334 bool InsetFormula::insetAllowed(Inset::Code code) const
336 return code == Inset::LABEL_CODE;
340 int InsetFormula::ascent(BufferView *, LyXFont const &) const
342 return par()->ascent() + 1;
346 int InsetFormula::descent(BufferView *, LyXFont const &) const
348 return par()->descent() + 1;
352 int InsetFormula::width(BufferView *, LyXFont const &) const
355 return par()->width();
359 MathInsetTypes InsetFormula::getType() const
361 return par()->getType();;