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(MathInsetTypes t)
59 : par_(new MathMatrixInset(t))
63 InsetFormula::InsetFormula(string const & s)
64 : par_(mathed_parse_normal(s))
70 InsetFormula::~InsetFormula()
76 Inset * InsetFormula::clone(Buffer const &, bool) const
78 return new InsetFormula(*this);
82 void InsetFormula::write(ostream & os) const
85 latex(os, false, false);
89 int InsetFormula::latex(ostream & os, bool fragile, bool) const
91 par_->write(os, fragile);
96 int InsetFormula::ascii(ostream & os, int) const
98 par_->write(os, false);
103 int InsetFormula::linuxdoc(ostream & os) const
109 int InsetFormula::docBook(ostream & os) const
115 void InsetFormula::read(LyXLex & lex)
117 par(mathed_parse_normal(lex));
122 void InsetFormula::draw(BufferView * bv, LyXFont const &,
123 int y, float & xx, bool) const
128 MathInset::workwidth = bv->workWidth();
129 Painter & pain = bv->painter();
132 int w = par_->width();
133 int h = par_->height();
134 int a = par_->ascent();
135 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
137 if (mathcursor && mathcursor->formula() == this) {
138 mathcursor->drawSelection(pain);
139 pain.rectangle(x, y - a, w, h, LColor::mathframe);
142 par_->draw(pain, x, y);
145 setCursorVisible(false);
149 void InsetFormula::metrics() const
151 const_cast<MathMatrixInset *>(par_)
152 -> metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
155 vector<string> const InsetFormula::getLabelList() const
157 return par_->getLabelList();
161 UpdatableInset::RESULT
162 InsetFormula::localDispatch(BufferView * bv, kb_action action,
165 RESULT result = DISPATCHED;
170 bv->lockedInsetStoreUndo(Undo::INSERT);
173 mathcursor->getPos(x, y);
174 mathcursor->breakLine();
175 mathcursor->normalize();
176 updateLocal(bv, true);
179 case LFUN_MATH_NUMBER:
181 //lyxerr << "toggling all numbers\n";
183 bv->lockedInsetStoreUndo(Undo::INSERT);
184 bool old = par_->numberedType();
185 for (int row = 0; row < par_->nrows(); ++row)
186 par_->numbered(row, !old);
187 bv->owner()->message(old ? _("No number") : _("Number"));
188 updateLocal(bv, true);
193 case LFUN_MATH_NONUMBER:
195 //lyxerr << "toggling line number\n";
197 bv->lockedInsetStoreUndo(Undo::INSERT);
198 int row = mathcursor->row();
199 bool old = par_->numbered(row);
200 bv->owner()->message(old ? _("No number") : _("Number"));
201 par_->numbered(row, !old);
202 updateLocal(bv, true);
207 case LFUN_INSERT_LABEL:
209 bv->lockedInsetStoreUndo(Undo::INSERT);
211 int row = mathcursor->row();
212 string old_label = par_->label(row);
213 string new_label = arg;
215 if (new_label.empty()) {
216 string const default_label =
217 (lyxrc.label_init_length >= 0) ? "eq:" : "";
218 pair<bool, string> const res = old_label.empty()
219 ? askForText(_("Enter new label to insert:"), default_label)
220 : askForText(_("Enter label:"), old_label);
222 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
225 new_label = frontStrip(strip(res.second));
228 //if (new_label == old_label)
229 // break; // Nothing to do
231 if (!new_label.empty()) {
232 lyxerr << "setting label to '" << new_label << "'\n";
233 par_->numbered(row, true);
236 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
239 par_->label(row, new_label);
241 updateLocal(bv, true);
245 case LFUN_MATH_EXTERN:
246 bv->lockedInsetStoreUndo(Undo::EDIT);
247 handleExtern(arg, bv);
248 updateLocal(bv, true);
251 case LFUN_MATH_MUTATE:
253 bv->lockedInsetStoreUndo(Undo::EDIT);
256 mathcursor->getPos(x, y);
258 mathcursor->setPos(x, y);
259 mathcursor->normalize();
260 updateLocal(bv, true);
264 case LFUN_MATH_DISPLAY:
268 mathcursor->getPos(x, y);
269 if (par_->getType() == LM_OT_SIMPLE)
270 par_->mutate(LM_OT_EQUATION);
272 par_->mutate(LM_OT_SIMPLE);
273 mathcursor->setPos(x, y);
274 mathcursor->normalize();
275 updateLocal(bv, true);
279 case LFUN_PASTESELECTION:
281 string const clip = bv->getClipboard();
283 par(mathed_parse_normal(clip));
288 result = InsetFormulaBase::localDispatch(bv, action, arg);
295 void InsetFormula::handleExtern(const string & arg, BufferView *)
297 //string outfile = lyx::tempName("maple.out");
298 string outfile = "/tmp/lyx2" + arg + ".out";
300 par_->writeNormal(os);
301 string code = os.str().c_str();
302 string script = "lyx2" + arg + " '" + code + "' " + outfile;
303 lyxerr << "calling: " << script << endl;
304 Systemcalls cmd(Systemcalls::System, script, 0);
306 ifstream is(outfile.c_str());
307 par(mathed_parse_normal(is));
311 bool InsetFormula::display() const
313 return par_->getType() != LM_OT_SIMPLE;
317 MathInset * InsetFormula::par() const
323 void InsetFormula::par(MathMatrixInset * p)
326 par_ = p ? static_cast<MathMatrixInset *>(p) : new MathMatrixInset;
330 Inset::Code InsetFormula::lyxCode() const
332 return Inset::MATH_CODE;
336 void InsetFormula::validate(LaTeXFeatures & features) const
338 par_->validate(features);
341 bool InsetFormula::insetAllowed(Inset::Code code) const
343 return code == Inset::LABEL_CODE;
347 int InsetFormula::ascent(BufferView *, LyXFont const &) const
349 return par_->ascent() + 1;
353 int InsetFormula::descent(BufferView *, LyXFont const &) const
355 return par_->descent() + 1;
359 int InsetFormula::width(BufferView *, LyXFont const &) const
362 return par_->width();
366 MathInsetTypes InsetFormula::getType() const
368 return par_->getType();;