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(Buffer const * buf, ostream & os) const
94 latex(buf, os, false, false);
98 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
100 par_->write(os, fragile);
105 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
107 par_->write(os, false);
112 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
114 return ascii(buf, os, 0);
118 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
120 return ascii(buf, os, 0);
124 void InsetFormula::read(Buffer const *, 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);
164 vector<string> const InsetFormula::getLabelList() const
166 return par_->getLabelList();
170 UpdatableInset::RESULT
171 InsetFormula::localDispatch(BufferView * bv, kb_action action,
174 RESULT result = DISPATCHED;
179 bv->lockedInsetStoreUndo(Undo::INSERT);
180 mathcursor->breakLine();
181 mathcursor->normalize();
182 updateLocal(bv, true);
185 case LFUN_MATH_NUMBER:
187 //lyxerr << "toggling all numbers\n";
189 bv->lockedInsetStoreUndo(Undo::INSERT);
190 bool old = par_->numberedType();
191 for (int row = 0; row < par_->nrows(); ++row)
192 par_->numbered(row, !old);
193 bv->owner()->message(old ? _("No number") : _("Number"));
194 updateLocal(bv, true);
199 case LFUN_MATH_NONUMBER:
201 //lyxerr << "toggling line number\n";
203 bv->lockedInsetStoreUndo(Undo::INSERT);
204 int row = mathcursor->row();
205 bool old = par_->numbered(row);
206 bv->owner()->message(old ? _("No number") : _("Number"));
207 par_->numbered(row, !old);
208 updateLocal(bv, true);
213 case LFUN_INSERT_LABEL:
215 bv->lockedInsetStoreUndo(Undo::INSERT);
217 int row = mathcursor->row();
218 string old_label = par_->label(row);
219 string new_label = arg;
221 if (new_label.empty()) {
222 string const default_label =
223 (lyxrc.label_init_length >= 0) ? "eq:" : "";
224 pair<bool, string> const res = old_label.empty()
225 ? askForText(_("Enter new label to insert:"), default_label)
226 : askForText(_("Enter label:"), old_label);
228 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
231 new_label = frontStrip(strip(res.second));
234 //if (new_label == old_label)
235 // break; // Nothing to do
237 if (!new_label.empty()) {
238 lyxerr << "setting label to '" << new_label << "'\n";
239 par_->numbered(row, true);
242 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
245 par_->label(row, new_label);
247 updateLocal(bv, true);
251 case LFUN_MATH_EXTERN:
252 bv->lockedInsetStoreUndo(Undo::EDIT);
253 handleExtern(arg, bv);
254 updateLocal(bv, true);
257 case LFUN_MATH_MUTATE:
259 bv->lockedInsetStoreUndo(Undo::EDIT);
262 mathcursor->getPos(x, y);
264 mathcursor->setPos(x, y);
265 mathcursor->normalize();
266 updateLocal(bv, true);
270 case LFUN_MATH_DISPLAY:
274 mathcursor->getPos(x, y);
275 if (par_->getType() == LM_OT_SIMPLE)
276 par_->mutate(LM_OT_EQUATION);
278 par_->mutate(LM_OT_SIMPLE);
279 mathcursor->setPos(x, y);
280 mathcursor->normalize();
281 updateLocal(bv, true);
285 case LFUN_PASTESELECTION:
287 string const clip = bv->getClipboard();
289 par(mathed_parse_normal(clip));
293 case LFUN_MATH_COLUMN_INSERT:
295 if (par_->getType() == LM_OT_ALIGN)
296 par_->mutate(LM_OT_ALIGNAT);
297 par_->addCol(par_->ncols());
298 mathcursor->normalize();
299 updateLocal(bv, true);
303 result = InsetFormulaBase::localDispatch(bv, action, arg);
310 void InsetFormula::handleExtern(const string & arg, BufferView *)
312 //string outfile = lyx::tempName("maple.out");
313 string outfile = "/tmp/lyx2" + arg + ".out";
315 par_->writeNormal(os);
316 string code = os.str().c_str();
317 string script = "lyx2" + arg + " '" + code + "' " + outfile;
318 lyxerr << "calling: " << script << endl;
319 Systemcalls cmd(Systemcalls::System, script, 0);
321 ifstream is(outfile.c_str());
322 par(mathed_parse_normal(is));
326 bool InsetFormula::display() const
328 return par_->getType() != LM_OT_SIMPLE;
332 MathInset const * InsetFormula::par() const
338 void InsetFormula::par(MathMatrixInset * p)
341 par_ = p ? static_cast<MathMatrixInset *>(p) : new MathMatrixInset;
345 Inset::Code InsetFormula::lyxCode() const
347 return Inset::MATH_CODE;
351 void InsetFormula::validate(LaTeXFeatures & features) const
353 par_->validate(features);
357 bool InsetFormula::insetAllowed(Inset::Code code) const
359 return code == Inset::LABEL_CODE && display();
363 int InsetFormula::ascent(BufferView *, LyXFont const &) const
365 return par_->ascent() + 1;
369 int InsetFormula::descent(BufferView *, LyXFont const &) const
371 return par_->descent() + 1;
375 int InsetFormula::width(BufferView *, LyXFont const &) const
378 return par_->width();
382 MathInsetTypes InsetFormula::getType() const
384 return par_->getType();