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"
50 InsetFormula::InsetFormula()
51 : par_(new MathMatrixInset)
55 InsetFormula::InsetFormula(const InsetFormula & f)
56 : InsetFormulaBase(f), par_(static_cast<MathMatrixInset *>(f.par_->clone()))
60 InsetFormula::InsetFormula(MathInsetTypes t)
61 : par_(new MathMatrixInset(t))
65 InsetFormula::InsetFormula(string const & s)
66 : par_(mathed_parse_normal(s))
69 par_ = mathed_parse_normal("$" + s + "$");
72 lyxerr << "cannot interpret '" << s << "' as math\n";
73 par_ = new MathMatrixInset(LM_OT_SIMPLE);
79 InsetFormula::~InsetFormula()
85 Inset * InsetFormula::clone(Buffer const &, bool) const
87 return new InsetFormula(*this);
91 void InsetFormula::write(ostream & os) const
94 latex(os, false, false);
98 int InsetFormula::latex(ostream & os, bool fragile, bool) const
100 par_->write(os, fragile);
105 int InsetFormula::ascii(ostream & os, int) const
107 par_->write(os, false);
112 int InsetFormula::linuxdoc(ostream & os) const
118 int InsetFormula::docBook(ostream & os) const
124 void InsetFormula::read(LyXLex & lex)
126 par(mathed_parse_normal(lex));
131 void InsetFormula::draw(BufferView * bv, LyXFont const &,
132 int y, float & xx, bool) const
137 MathInset::workwidth = bv->workWidth();
138 Painter & pain = bv->painter();
141 int w = par_->width();
142 int h = par_->height();
143 int a = par_->ascent();
144 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
146 if (mathcursor && mathcursor->formula() == this) {
147 mathcursor->drawSelection(pain);
148 pain.rectangle(x, y - a, w, h, LColor::mathframe);
151 par_->draw(pain, x, y);
154 setCursorVisible(false);
158 void InsetFormula::metrics() const
160 par_->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
163 vector<string> const InsetFormula::getLabelList() const
165 return par_->getLabelList();
169 UpdatableInset::RESULT
170 InsetFormula::localDispatch(BufferView * bv, kb_action action,
173 RESULT result = DISPATCHED;
178 bv->lockedInsetStoreUndo(Undo::INSERT);
179 mathcursor->breakLine();
180 mathcursor->normalize();
181 updateLocal(bv, true);
184 case LFUN_MATH_NUMBER:
186 //lyxerr << "toggling all numbers\n";
188 bv->lockedInsetStoreUndo(Undo::INSERT);
189 bool old = par_->numberedType();
190 for (int row = 0; row < par_->nrows(); ++row)
191 par_->numbered(row, !old);
192 bv->owner()->message(old ? _("No number") : _("Number"));
193 updateLocal(bv, true);
198 case LFUN_MATH_NONUMBER:
200 //lyxerr << "toggling line number\n";
202 bv->lockedInsetStoreUndo(Undo::INSERT);
203 int row = mathcursor->row();
204 bool old = par_->numbered(row);
205 bv->owner()->message(old ? _("No number") : _("Number"));
206 par_->numbered(row, !old);
207 updateLocal(bv, true);
212 case LFUN_INSERT_LABEL:
214 bv->lockedInsetStoreUndo(Undo::INSERT);
216 int row = mathcursor->row();
217 string old_label = par_->label(row);
218 string new_label = arg;
220 if (new_label.empty()) {
221 string const default_label =
222 (lyxrc.label_init_length >= 0) ? "eq:" : "";
223 pair<bool, string> const res = old_label.empty()
224 ? askForText(_("Enter new label to insert:"), default_label)
225 : askForText(_("Enter label:"), old_label);
227 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
230 new_label = frontStrip(strip(res.second));
233 //if (new_label == old_label)
234 // break; // Nothing to do
236 if (!new_label.empty()) {
237 lyxerr << "setting label to '" << new_label << "'\n";
238 par_->numbered(row, true);
241 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
244 par_->label(row, new_label);
246 updateLocal(bv, true);
250 case LFUN_MATH_EXTERN:
251 bv->lockedInsetStoreUndo(Undo::EDIT);
252 handleExtern(arg, bv);
253 updateLocal(bv, true);
256 case LFUN_MATH_MUTATE:
258 bv->lockedInsetStoreUndo(Undo::EDIT);
261 mathcursor->getPos(x, y);
263 mathcursor->setPos(x, y);
264 mathcursor->normalize();
265 updateLocal(bv, true);
269 case LFUN_MATH_DISPLAY:
273 mathcursor->getPos(x, y);
274 if (par_->getType() == LM_OT_SIMPLE)
275 par_->mutate(LM_OT_EQUATION);
277 par_->mutate(LM_OT_SIMPLE);
278 mathcursor->setPos(x, y);
279 mathcursor->normalize();
280 updateLocal(bv, true);
284 case LFUN_PASTESELECTION:
286 string const clip = bv->getClipboard();
288 par(mathed_parse_normal(clip));
293 result = InsetFormulaBase::localDispatch(bv, action, arg);
300 void InsetFormula::handleExtern(const string & arg, BufferView *)
302 //string outfile = lyx::tempName("maple.out");
303 string outfile = "/tmp/lyx2" + arg + ".out";
305 par_->writeNormal(os);
306 string code = os.str().c_str();
307 string script = "lyx2" + arg + " '" + code + "' " + outfile;
308 lyxerr << "calling: " << script << endl;
309 Systemcalls cmd(Systemcalls::System, script, 0);
311 ifstream is(outfile.c_str());
312 par(mathed_parse_normal(is));
316 bool InsetFormula::display() const
318 return par_->getType() != LM_OT_SIMPLE;
322 MathInset const * InsetFormula::par() const
328 void InsetFormula::par(MathMatrixInset * p)
331 par_ = p ? static_cast<MathMatrixInset *>(p) : new MathMatrixInset;
335 Inset::Code InsetFormula::lyxCode() const
337 return Inset::MATH_CODE;
341 void InsetFormula::validate(LaTeXFeatures & features) const
343 par_->validate(features);
346 bool InsetFormula::insetAllowed(Inset::Code code) const
348 return code == Inset::LABEL_CODE;
352 int InsetFormula::ascent(BufferView *, LyXFont const &) const
354 return par_->ascent() + 1;
358 int InsetFormula::descent(BufferView *, LyXFont const &) const
360 return par_->descent() + 1;
364 int InsetFormula::width(BufferView *, LyXFont const &) const
367 return par_->width();
371 MathInsetTypes InsetFormula::getType() const
373 return par_->getType();;