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 // quite a hack i know. Should be done with return values...
54 int number_of_newlines = 0;
57 InsetFormula::InsetFormula()
58 : InsetFormulaBase(new MathMatrixInset)
62 InsetFormula::InsetFormula(MathInsetTypes t)
63 : InsetFormulaBase(new MathMatrixInset(t))
67 InsetFormula::InsetFormula(string const & s)
68 : InsetFormulaBase(mathed_parse(s))
74 Inset * InsetFormula::clone(Buffer const &, bool) const
76 return new InsetFormula(*this);
80 void InsetFormula::write(ostream & os) const
83 latex(os, false, false);
87 int InsetFormula::latex(ostream & os, bool fragile, bool) const
89 par()->write(os, fragile);
94 int InsetFormula::ascii(ostream & os, int) const
96 par()->write(os, false);
101 int InsetFormula::linuxdoc(ostream & os) const
107 int InsetFormula::docBook(ostream & os) const
113 void InsetFormula::read(LyXLex & lex)
115 par(mathed_parse(lex));
120 void InsetFormula::draw(BufferView * bv, LyXFont const &,
121 int y, float & xx, bool) const
126 MathInset::workwidth = bv->workWidth();
127 Painter & pain = bv->painter();
130 int w = par()->width();
131 int h = par()->height();
132 int a = par()->ascent();
133 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
135 if (mathcursor && mathcursor->formula() == this) {
136 mathcursor->drawSelection(pain);
137 pain.rectangle(x, y - a, w, h, LColor::mathframe);
140 par()->draw(pain, x, y);
141 xx += par()->width();
143 setCursorVisible(false);
147 void InsetFormula::metrics() const
149 const_cast<MathInset *>(par_)->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
152 vector<string> const InsetFormula::getLabelList() const
154 return par()->getLabelList();
158 UpdatableInset::RESULT
159 InsetFormula::localDispatch(BufferView * bv, kb_action action,
162 RESULT result = DISPATCHED;
167 bv->lockedInsetStoreUndo(Undo::INSERT);
170 mathcursor->getPos(x, y);
171 mathcursor->breakLine();
172 mathcursor->normalize();
173 updateLocal(bv, true);
176 case LFUN_MATH_NUMBER:
178 //lyxerr << "toggling all numbers\n";
180 bv->lockedInsetStoreUndo(Undo::INSERT);
181 bool old = par()->numberedType();
182 for (int row = 0; row < par()->nrows(); ++row)
183 par()->numbered(row, !old);
184 bv->owner()->message(old ? _("No number") : _("Number"));
185 updateLocal(bv, true);
190 case LFUN_MATH_NONUMBER:
192 //lyxerr << "toggling line number\n";
194 bv->lockedInsetStoreUndo(Undo::INSERT);
195 int row = mathcursor->row();
196 bool old = par()->numbered(row);
197 bv->owner()->message(old ? _("No number") : _("Number"));
198 par()->numbered(row, !old);
199 updateLocal(bv, true);
204 case LFUN_INSERT_LABEL:
206 bv->lockedInsetStoreUndo(Undo::INSERT);
208 int row = mathcursor->row();
209 string old_label = par()->label(row);
210 string new_label = arg;
212 if (new_label.empty()) {
213 string const default_label =
214 (lyxrc.label_init_length >= 0) ? "eq:" : "";
215 pair<bool, string> const res = old_label.empty()
216 ? askForText(_("Enter new label to insert:"), default_label)
217 : askForText(_("Enter label:"), old_label);
219 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
222 new_label = frontStrip(strip(res.second));
225 //if (new_label == old_label)
226 // break; // Nothing to do
228 if (!new_label.empty()) {
229 lyxerr << "setting label to '" << new_label << "'\n";
230 par()->numbered(row, true);
233 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
236 par()->label(row, new_label);
238 updateLocal(bv, true);
242 case LFUN_MATH_EXTERN:
243 bv->lockedInsetStoreUndo(Undo::EDIT);
244 handleExtern(arg, bv);
245 updateLocal(bv, true);
248 case LFUN_MATH_MUTATE:
250 bv->lockedInsetStoreUndo(Undo::EDIT);
253 mathcursor->getPos(x, y);
255 mathcursor->setPos(x, y);
256 mathcursor->normalize();
257 updateLocal(bv, true);
261 case LFUN_MATH_DISPLAY:
265 mathcursor->getPos(x, y);
266 if (par()->getType() == LM_OT_SIMPLE)
267 par()->mutate(LM_OT_EQUATION);
269 par()->mutate(LM_OT_SIMPLE);
270 mathcursor->setPos(x, y);
271 mathcursor->normalize();
272 updateLocal(bv, true);
276 case LFUN_PASTESELECTION:
278 string const clip = bv->getClipboard();
280 par(mathed_parse(clip));
285 result = InsetFormulaBase::localDispatch(bv, action, arg);
292 void InsetFormula::handleExtern(const string & arg, BufferView *)
294 //string outfile = lyx::tempName("maple.out");
295 string outfile = "/tmp/lyx2" + arg + ".out";
297 par()->writeNormal(os);
298 string code = os.str().c_str();
299 string script = "lyx2" + arg + " '" + code + "' " + outfile;
300 lyxerr << "calling: " << script << endl;
301 Systemcalls cmd(Systemcalls::System, script, 0);
303 ifstream is(outfile.c_str());
304 par(mathed_parse(is));
308 bool InsetFormula::display() const
310 return par()->getType() != LM_OT_SIMPLE;
314 MathMatrixInset * InsetFormula::par() const
316 return static_cast<MathMatrixInset *>(par_);
319 void InsetFormula::par(MathInset * p)
322 par_ = p ? p : new MathMatrixInset;
326 Inset::Code InsetFormula::lyxCode() const
328 return Inset::MATH_CODE;
332 void InsetFormula::validate(LaTeXFeatures & features) const
334 par()->validate(features);
337 bool InsetFormula::insetAllowed(Inset::Code code) const
339 return code == Inset::LABEL_CODE;
343 int InsetFormula::ascent(BufferView *, LyXFont const &) const
345 return par()->ascent() + 1;
349 int InsetFormula::descent(BufferView *, LyXFont const &) const
351 return par()->descent() + 1;
355 int InsetFormula::width(BufferView *, LyXFont const &) const
358 return par()->width();
362 MathInsetTypes InsetFormula::getType() const
364 return par()->getType();;