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/LAssert.h"
34 #include "support/lyxlib.h"
35 #include "support/syscall.h"
36 #include "support/lstrings.h"
40 #include "math_matrixinset.h"
41 #include "mathed/support.h"
51 InsetFormula::InsetFormula()
52 : par_(MathAtom(new MathMatrixInset))
56 InsetFormula::InsetFormula(MathInsetTypes t)
57 : par_(MathAtom(new MathMatrixInset(t)))
61 InsetFormula::InsetFormula(string const & s)
64 bool res = mathed_parse_normal(par_, s);
67 res = mathed_parse_normal(par_, "$" + s + "$");
70 lyxerr << "cannot interpret '" << s << "' as math\n";
71 par_ = MathAtom(new MathMatrixInset(LM_OT_SIMPLE));
78 Inset * InsetFormula::clone(Buffer const &, bool) const
80 return new InsetFormula(*this);
84 void InsetFormula::write(Buffer const * buf, ostream & os) const
87 latex(buf, os, false, false);
91 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
93 par_->write(os, fragile);
98 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
100 par_->write(os, false);
105 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
107 return ascii(buf, os, 0);
111 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
113 return ascii(buf, os, 0);
117 void InsetFormula::read(Buffer const *, LyXLex & lex)
119 mathed_parse_normal(par_, lex);
124 void InsetFormula::draw(BufferView * bv, LyXFont const &,
125 int y, float & xx, bool) const
130 MathInset::workwidth = bv->workWidth();
131 Painter & pain = bv->painter();
134 int w = par_->width();
135 int h = par_->height();
136 int a = par_->ascent();
137 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
139 if (mathcursor && mathcursor->formula() == this) {
140 mathcursor->drawSelection(pain);
141 pain.rectangle(x, y - a, w, h, LColor::mathframe);
144 par_->draw(pain, x, y);
147 setCursorVisible(false);
151 void InsetFormula::metrics() const
153 par_->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
157 vector<string> const InsetFormula::getLabelList() const
159 return mat()->getLabelList();
163 UpdatableInset::RESULT
164 InsetFormula::localDispatch(BufferView * bv, kb_action action,
167 RESULT result = DISPATCHED;
172 bv->lockedInsetStoreUndo(Undo::INSERT);
173 mathcursor->breakLine();
174 mathcursor->normalize();
175 updateLocal(bv, true);
178 case LFUN_MATH_NUMBER:
180 //lyxerr << "toggling all numbers\n";
182 bv->lockedInsetStoreUndo(Undo::INSERT);
183 bool old = mat()->numberedType();
184 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
185 mat()->numbered(row, !old);
186 bv->owner()->message(old ? _("No number") : _("Number"));
187 updateLocal(bv, true);
192 case LFUN_MATH_NONUMBER:
194 //lyxerr << "toggling line number\n";
196 bv->lockedInsetStoreUndo(Undo::INSERT);
197 MathCursor::row_type row = mathcursor->row();
198 bool old = mat()->numbered(row);
199 bv->owner()->message(old ? _("No number") : _("Number"));
200 mat()->numbered(row, !old);
201 updateLocal(bv, true);
206 case LFUN_INSERT_LABEL:
208 bv->lockedInsetStoreUndo(Undo::INSERT);
210 MathCursor::row_type row = mathcursor->row();
211 string old_label = mat()->label(row);
212 string new_label = arg;
214 if (new_label.empty()) {
215 string const default_label =
216 (lyxrc.label_init_length >= 0) ? "eq:" : "";
217 pair<bool, string> const res = old_label.empty()
218 ? askForText(_("Enter new label to insert:"), default_label)
219 : askForText(_("Enter label:"), old_label);
221 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
224 new_label = frontStrip(strip(res.second));
227 //if (new_label == old_label)
228 // break; // Nothing to do
230 if (!new_label.empty()) {
231 lyxerr << "setting label to '" << new_label << "'\n";
232 mat()->numbered(row, true);
235 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
238 mat()->label(row, new_label);
240 updateLocal(bv, true);
244 case LFUN_MATH_EXTERN:
245 bv->lockedInsetStoreUndo(Undo::EDIT);
246 handleExtern(arg, bv);
247 updateLocal(bv, true);
250 case LFUN_MATH_MUTATE:
252 bv->lockedInsetStoreUndo(Undo::EDIT);
255 mathcursor->getPos(x, y);
257 mathcursor->setPos(x, y);
258 mathcursor->normalize();
259 updateLocal(bv, true);
263 case LFUN_MATH_DISPLAY:
267 mathcursor->getPos(x, y);
268 if (mat()->getType() == LM_OT_SIMPLE)
269 mat()->mutate(LM_OT_EQUATION);
271 mat()->mutate(LM_OT_SIMPLE);
272 mathcursor->setPos(x, y);
273 mathcursor->normalize();
274 updateLocal(bv, true);
278 case LFUN_PASTESELECTION:
280 string const clip = bv->getClipboard();
282 mathed_parse_normal(par_, clip);
286 case LFUN_MATH_COLUMN_INSERT:
288 if (mat()->getType() == LM_OT_ALIGN)
289 mat()->mutate(LM_OT_ALIGNAT);
290 mat()->addCol(mat()->ncols());
291 mathcursor->normalize();
292 updateLocal(bv, true);
296 result = InsetFormulaBase::localDispatch(bv, action, arg);
303 void InsetFormula::handleExtern(const string & arg, BufferView *)
305 //string outfile = lyx::tempName("maple.out");
306 string outfile = "/tmp/lyx2" + arg + ".out";
308 mat()->writeNormal(os);
309 string code = os.str().c_str();
310 string script = "lyx2" + arg + " '" + code + "' " + outfile;
311 lyxerr << "calling: " << script << endl;
312 Systemcalls cmd(Systemcalls::System, script, 0);
314 ifstream is(outfile.c_str());
315 mathed_parse_normal(par_, is);
320 bool InsetFormula::display() const
322 return mat()->getType() != LM_OT_SIMPLE;
326 MathMatrixInset const * InsetFormula::mat() const
328 lyx::Assert(par_->asMatrixInset());
329 return par_->asMatrixInset();
333 MathMatrixInset * InsetFormula::mat()
335 lyx::Assert(par_->asMatrixInset());
336 return par_->asMatrixInset();
340 Inset::Code InsetFormula::lyxCode() const
342 return Inset::MATH_CODE;
346 void InsetFormula::validate(LaTeXFeatures & features) const
348 par_->validate(features);
352 bool InsetFormula::insetAllowed(Inset::Code code) const
354 return code == Inset::LABEL_CODE && display();
358 int InsetFormula::ascent(BufferView *, LyXFont const &) const
360 return par_->ascent() + 1;
364 int InsetFormula::descent(BufferView *, LyXFont const &) const
366 return par_->descent() + 1;
370 int InsetFormula::width(BufferView *, LyXFont const &) const
373 return par_->width();
377 MathInsetTypes InsetFormula::getType() const
379 return mat()->getType();