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 : par_(new MathMatrixInset)
58 InsetFormula::InsetFormula(const InsetFormula & f)
59 : InsetFormulaBase(f), par_(static_cast<MathMatrixInset *>(f.par_->clone()))
63 InsetFormula::InsetFormula(MathInsetTypes t)
64 : par_(new MathMatrixInset(t))
68 InsetFormula::InsetFormula(string const & s)
69 : par_(mathed_parse_normal(s))
75 InsetFormula::~InsetFormula()
81 Inset * InsetFormula::clone(Buffer const &, bool) const
83 return new InsetFormula(*this);
87 void InsetFormula::write(ostream & os) const
90 latex(os, false, false);
94 int InsetFormula::latex(ostream & os, bool fragile, bool) const
96 par_->write(os, fragile);
101 int InsetFormula::ascii(ostream & os, int) const
103 par_->write(os, false);
108 int InsetFormula::linuxdoc(ostream & os) const
114 int InsetFormula::docBook(ostream & os) const
120 void InsetFormula::read(LyXLex & lex)
122 par(mathed_parse_normal(lex));
127 void InsetFormula::draw(BufferView * bv, LyXFont const &,
128 int y, float & xx, bool) const
133 MathInset::workwidth = bv->workWidth();
134 Painter & pain = bv->painter();
137 int w = par_->width();
138 int h = par_->height();
139 int a = par_->ascent();
140 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
142 if (mathcursor && mathcursor->formula() == this) {
143 mathcursor->drawSelection(pain);
144 pain.rectangle(x, y - a, w, h, LColor::mathframe);
147 par_->draw(pain, x, y);
150 setCursorVisible(false);
154 void InsetFormula::metrics() const
156 par_->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
159 vector<string> const InsetFormula::getLabelList() const
161 return par_->getLabelList();
165 UpdatableInset::RESULT
166 InsetFormula::localDispatch(BufferView * bv, kb_action action,
169 RESULT result = DISPATCHED;
174 bv->lockedInsetStoreUndo(Undo::INSERT);
177 mathcursor->getPos(x, y);
178 mathcursor->breakLine();
179 mathcursor->normalize();
180 updateLocal(bv, true);
183 case LFUN_MATH_NUMBER:
185 //lyxerr << "toggling all numbers\n";
187 bv->lockedInsetStoreUndo(Undo::INSERT);
188 bool old = par_->numberedType();
189 for (int row = 0; row < par_->nrows(); ++row)
190 par_->numbered(row, !old);
191 bv->owner()->message(old ? _("No number") : _("Number"));
192 updateLocal(bv, true);
197 case LFUN_MATH_NONUMBER:
199 //lyxerr << "toggling line number\n";
201 bv->lockedInsetStoreUndo(Undo::INSERT);
202 int row = mathcursor->row();
203 bool old = par_->numbered(row);
204 bv->owner()->message(old ? _("No number") : _("Number"));
205 par_->numbered(row, !old);
206 updateLocal(bv, true);
211 case LFUN_INSERT_LABEL:
213 bv->lockedInsetStoreUndo(Undo::INSERT);
215 int row = mathcursor->row();
216 string old_label = par_->label(row);
217 string new_label = arg;
219 if (new_label.empty()) {
220 string const default_label =
221 (lyxrc.label_init_length >= 0) ? "eq:" : "";
222 pair<bool, string> const res = old_label.empty()
223 ? askForText(_("Enter new label to insert:"), default_label)
224 : askForText(_("Enter label:"), old_label);
226 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
229 new_label = frontStrip(strip(res.second));
232 //if (new_label == old_label)
233 // break; // Nothing to do
235 if (!new_label.empty()) {
236 lyxerr << "setting label to '" << new_label << "'\n";
237 par_->numbered(row, true);
240 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
243 par_->label(row, new_label);
245 updateLocal(bv, true);
249 case LFUN_MATH_EXTERN:
250 bv->lockedInsetStoreUndo(Undo::EDIT);
251 handleExtern(arg, bv);
252 updateLocal(bv, true);
255 case LFUN_MATH_MUTATE:
257 bv->lockedInsetStoreUndo(Undo::EDIT);
260 mathcursor->getPos(x, y);
262 mathcursor->setPos(x, y);
263 mathcursor->normalize();
264 updateLocal(bv, true);
268 case LFUN_MATH_DISPLAY:
272 mathcursor->getPos(x, y);
273 if (par_->getType() == LM_OT_SIMPLE)
274 par_->mutate(LM_OT_EQUATION);
276 par_->mutate(LM_OT_SIMPLE);
277 mathcursor->setPos(x, y);
278 mathcursor->normalize();
279 updateLocal(bv, true);
283 case LFUN_PASTESELECTION:
285 string const clip = bv->getClipboard();
287 par(mathed_parse_normal(clip));
292 result = InsetFormulaBase::localDispatch(bv, action, arg);
299 void InsetFormula::handleExtern(const string & arg, BufferView *)
301 //string outfile = lyx::tempName("maple.out");
302 string outfile = "/tmp/lyx2" + arg + ".out";
304 par_->writeNormal(os);
305 string code = os.str().c_str();
306 string script = "lyx2" + arg + " '" + code + "' " + outfile;
307 lyxerr << "calling: " << script << endl;
308 Systemcalls cmd(Systemcalls::System, script, 0);
310 ifstream is(outfile.c_str());
311 par(mathed_parse_normal(is));
315 bool InsetFormula::display() const
317 return par_->getType() != LM_OT_SIMPLE;
321 MathInset const * InsetFormula::par() const
327 void InsetFormula::par(MathMatrixInset * p)
330 par_ = p ? static_cast<MathMatrixInset *>(p) : new MathMatrixInset;
334 Inset::Code InsetFormula::lyxCode() const
336 return Inset::MATH_CODE;
340 void InsetFormula::validate(LaTeXFeatures & features) const
342 par_->validate(features);
345 bool InsetFormula::insetAllowed(Inset::Code code) const
347 return code == Inset::LABEL_CODE;
351 int InsetFormula::ascent(BufferView *, LyXFont const &) const
353 return par_->ascent() + 1;
357 int InsetFormula::descent(BufferView *, LyXFont const &) const
359 return par_->descent() + 1;
363 int InsetFormula::width(BufferView *, LyXFont const &) const
366 return par_->width();
370 MathInsetTypes InsetFormula::getType() const
372 return par_->getType();;