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)
69 istringstream is(s.c_str());
70 par(mathed_parse(is));
75 Inset * InsetFormula::clone(Buffer const &, bool) const
77 return new InsetFormula(*this);
81 void InsetFormula::write(ostream & os) const
84 latex(os, false, false);
88 int InsetFormula::latex(ostream & os, bool fragile, bool) const
90 par()->Write(os, fragile);
95 int InsetFormula::ascii(ostream & os, int) const
97 par()->Write(os, false);
102 int InsetFormula::linuxdoc(ostream & os) const
108 int InsetFormula::docBook(ostream & os) const
114 void InsetFormula::read(LyXLex & lex)
116 par(mathed_parse(lex));
121 void InsetFormula::draw(BufferView * bv, LyXFont const &,
122 int y, float & xx, bool) const
127 MathInset::workwidth = bv->workWidth();
128 Painter & pain = bv->painter();
131 int w = par()->width();
132 int h = par()->height();
133 int a = par()->ascent();
134 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
136 if (mathcursor && mathcursor->formula() == this) {
137 if (mathcursor->Selection()) {
141 mathcursor->SelGetArea(xp, yp, n);
142 pain.fillPolygon(xp, yp, n, LColor::selection);
144 pain.rectangle(x, y - a, w, h, LColor::mathframe);
147 par()->draw(pain, x, y);
148 xx += par()->width();
150 setCursorVisible(false);
154 void InsetFormula::Metrics() const
156 const_cast<MathInset *>(par_)->Metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
159 vector<string> const InsetFormula::getLabelList() const
161 return par()->getLabelList();
165 UpdatableInset::RESULT
166 InsetFormula::localDispatch(BufferView * bv, kb_action action,
169 RESULT result = DISPATCHED;
174 bv->lockedInsetStoreUndo(Undo::INSERT);
175 mathcursor->breakLine();
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"));
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);
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);
245 case LFUN_MATH_EXTERN:
246 bv->lockedInsetStoreUndo(Undo::EDIT);
247 handleExtern(arg, bv);
251 case LFUN_MATH_MUTATE:
253 bv->lockedInsetStoreUndo(Undo::EDIT);
256 mathcursor->GetPos(x, y);
258 mathcursor->SetPos(x, y);
259 mathcursor->normalize();
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();
280 result = InsetFormulaBase::localDispatch(bv, action, arg);
287 void InsetFormula::handleExtern(const string & arg, BufferView *)
289 //string outfile = lyx::tempName("maple.out");
290 string outfile = "/tmp/lyx2" + arg + ".out";
292 par()->WriteNormal(os);
293 string code = os.str().c_str();
294 string script = "lyx2" + arg + " '" + code + "' " + outfile;
295 lyxerr << "calling: " << script << endl;
296 Systemcalls cmd(Systemcalls::System, script, 0);
298 ifstream is(outfile.c_str());
299 par(mathed_parse(is));
303 bool InsetFormula::display() const
305 return par_->GetType() != LM_OT_SIMPLE;
309 MathMatrixInset * InsetFormula::par() const
311 return static_cast<MathMatrixInset *>(par_);
314 void InsetFormula::par(MathInset * p)
317 par_ = p ? p : new MathMatrixInset;
321 Inset::Code InsetFormula::lyxCode() const
323 return Inset::MATH_CODE;
327 void InsetFormula::validate(LaTeXFeatures & features) const
329 par()->Validate(features);
333 int InsetFormula::ascent(BufferView *, LyXFont const &) const
335 return par()->ascent();
339 int InsetFormula::descent(BufferView *, LyXFont const &) const
341 return par()->descent();
345 int InsetFormula::width(BufferView *, LyXFont const &) const
347 return par()->width();