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"
38 #include "math_matrixinset.h"
39 #include "mathed/support.h"
48 extern char const * latex_mathenv[];
49 extern MathCursor * mathcursor;
52 // quite a hack i know. Should be done with return values...
53 int number_of_newlines = 0;
56 InsetFormula::InsetFormula()
57 : InsetFormulaBase(new MathMatrixInset)
61 InsetFormula::InsetFormula(MathInsetTypes t)
62 : InsetFormulaBase(new MathMatrixInset(t))
66 InsetFormula::InsetFormula(string const & s)
67 : InsetFormulaBase(mathed_parse(s))
73 Inset * InsetFormula::clone(Buffer const &, bool) const
75 return new InsetFormula(*this);
79 void InsetFormula::write(ostream & os) const
82 latex(os, false, false);
86 int InsetFormula::latex(ostream & os, bool fragile, bool) const
88 par()->write(os, fragile);
93 int InsetFormula::ascii(ostream & os, int) const
95 par()->write(os, false);
100 int InsetFormula::linuxdoc(ostream & os) const
106 int InsetFormula::docBook(ostream & os) const
112 void InsetFormula::read(LyXLex & lex)
114 par(mathed_parse(lex));
119 void InsetFormula::draw(BufferView * bv, LyXFont const &,
120 int y, float & xx, bool) const
125 MathInset::workwidth = bv->workWidth();
126 Painter & pain = bv->painter();
129 int w = par()->width();
130 int h = par()->height();
131 int a = par()->ascent();
132 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
134 if (mathcursor && mathcursor->formula() == this) {
135 mathcursor->drawSelection(pain);
136 pain.rectangle(x, y - a, w, h, LColor::mathframe);
139 par()->draw(pain, x, y);
140 xx += par()->width();
142 setCursorVisible(false);
146 void InsetFormula::metrics() const
148 const_cast<MathInset *>(par_)->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
151 vector<string> const InsetFormula::getLabelList() const
153 return par()->getLabelList();
157 UpdatableInset::RESULT
158 InsetFormula::localDispatch(BufferView * bv, kb_action action,
161 RESULT result = DISPATCHED;
166 bv->lockedInsetStoreUndo(Undo::INSERT);
169 mathcursor->getPos(x, y);
170 mathcursor->breakLine();
171 mathcursor->normalize();
172 updateLocal(bv, true);
175 case LFUN_MATH_NUMBER:
177 //lyxerr << "toggling all numbers\n";
179 bv->lockedInsetStoreUndo(Undo::INSERT);
180 bool old = par()->numberedType();
181 for (int row = 0; row < par()->nrows(); ++row)
182 par()->numbered(row, !old);
183 bv->owner()->message(old ? _("No number") : _("Number"));
184 updateLocal(bv, true);
189 case LFUN_MATH_NONUMBER:
191 //lyxerr << "toggling line number\n";
193 bv->lockedInsetStoreUndo(Undo::INSERT);
194 int row = mathcursor->row();
195 bool old = par()->numbered(row);
196 bv->owner()->message(old ? _("No number") : _("Number"));
197 par()->numbered(row, !old);
198 updateLocal(bv, true);
203 case LFUN_INSERT_LABEL:
205 bv->lockedInsetStoreUndo(Undo::INSERT);
207 int row = mathcursor->row();
208 string old_label = par()->label(row);
209 string new_label = arg;
211 if (new_label.empty()) {
212 string const default_label =
213 (lyxrc.label_init_length >= 0) ? "eq:" : "";
214 pair<bool, string> const res = old_label.empty()
215 ? askForText(_("Enter new label to insert:"), default_label)
216 : askForText(_("Enter label:"), old_label);
218 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
221 new_label = frontStrip(strip(res.second));
224 //if (new_label == old_label)
225 // break; // Nothing to do
227 if (!new_label.empty()) {
228 lyxerr << "setting label to '" << new_label << "'\n";
229 par()->numbered(row, true);
232 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
235 par()->label(row, new_label);
237 updateLocal(bv, true);
241 case LFUN_MATH_EXTERN:
242 bv->lockedInsetStoreUndo(Undo::EDIT);
243 handleExtern(arg, bv);
244 updateLocal(bv, true);
247 case LFUN_MATH_MUTATE:
249 bv->lockedInsetStoreUndo(Undo::EDIT);
252 mathcursor->getPos(x, y);
254 mathcursor->setPos(x, y);
255 mathcursor->normalize();
256 updateLocal(bv, true);
260 case LFUN_MATH_DISPLAY:
264 mathcursor->getPos(x, y);
265 if (par()->getType() == LM_OT_SIMPLE)
266 par()->mutate(LM_OT_EQUATION);
268 par()->mutate(LM_OT_SIMPLE);
269 mathcursor->setPos(x, y);
270 mathcursor->normalize();
271 updateLocal(bv, true);
275 case LFUN_PASTESELECTION:
277 string const clip = bv->getClipboard();
279 par(mathed_parse(clip));
284 result = InsetFormulaBase::localDispatch(bv, action, arg);
291 void InsetFormula::handleExtern(const string & arg, BufferView *)
293 //string outfile = lyx::tempName("maple.out");
294 string outfile = "/tmp/lyx2" + arg + ".out";
296 par()->writeNormal(os);
297 string code = os.str().c_str();
298 string script = "lyx2" + arg + " '" + code + "' " + outfile;
299 lyxerr << "calling: " << script << endl;
300 Systemcalls cmd(Systemcalls::System, script, 0);
302 ifstream is(outfile.c_str());
303 par(mathed_parse(is));
307 bool InsetFormula::display() const
309 return par_->getType() != LM_OT_SIMPLE;
313 MathMatrixInset * InsetFormula::par() const
315 return static_cast<MathMatrixInset *>(par_);
318 void InsetFormula::par(MathInset * p)
321 par_ = p ? p : new MathMatrixInset;
325 Inset::Code InsetFormula::lyxCode() const
327 return Inset::MATH_CODE;
331 void InsetFormula::validate(LaTeXFeatures & features) const
333 par()->validate(features);
336 bool InsetFormula::insetAllowed(Inset::Code code) const
338 return code == Inset::LABEL_CODE;
342 int InsetFormula::ascent(BufferView *, LyXFont const &) const
344 return par()->ascent() + 1;
348 int InsetFormula::descent(BufferView *, LyXFont const &) const
350 return par()->descent() + 1;
354 int InsetFormula::width(BufferView *, LyXFont const &) const
357 return par()->width();