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 mathcursor->drawSelection(pain);
138 pain.rectangle(x, y - a, w, h, LColor::mathframe);
141 par()->draw(pain, x, y);
142 xx += par()->width();
144 setCursorVisible(false);
148 void InsetFormula::Metrics() const
150 const_cast<MathInset *>(par_)->Metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
153 vector<string> const InsetFormula::getLabelList() const
155 return par()->getLabelList();
159 UpdatableInset::RESULT
160 InsetFormula::localDispatch(BufferView * bv, kb_action action,
163 RESULT result = DISPATCHED;
168 bv->lockedInsetStoreUndo(Undo::INSERT);
171 mathcursor->GetPos(x, y);
172 mathcursor->breakLine();
173 mathcursor->normalize();
174 updateLocal(bv, true);
177 case LFUN_MATH_NUMBER:
179 //lyxerr << "toggling all numbers\n";
181 bv->lockedInsetStoreUndo(Undo::INSERT);
182 bool old = par()->numberedType();
183 for (int row = 0; row < par()->nrows(); ++row)
184 par()->numbered(row, !old);
185 bv->owner()->message(old ? _("No number") : _("Number"));
186 updateLocal(bv, true);
191 case LFUN_MATH_NONUMBER:
193 //lyxerr << "toggling line number\n";
195 bv->lockedInsetStoreUndo(Undo::INSERT);
196 int row = mathcursor->row();
197 bool old = par()->numbered(row);
198 bv->owner()->message(old ? _("No number") : _("Number"));
199 par()->numbered(row, !old);
200 updateLocal(bv, true);
205 case LFUN_INSERT_LABEL:
207 bv->lockedInsetStoreUndo(Undo::INSERT);
209 int row = mathcursor->row();
210 string old_label = par()->label(row);
211 string new_label = arg;
213 if (new_label.empty()) {
214 string const default_label =
215 (lyxrc.label_init_length >= 0) ? "eq:" : "";
216 pair<bool, string> const res = old_label.empty()
217 ? askForText(_("Enter new label to insert:"), default_label)
218 : askForText(_("Enter label:"), old_label);
220 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
223 new_label = frontStrip(strip(res.second));
226 //if (new_label == old_label)
227 // break; // Nothing to do
229 if (!new_label.empty()) {
230 lyxerr << "setting label to '" << new_label << "'\n";
231 par()->numbered(row, true);
234 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
237 par()->label(row, new_label);
239 updateLocal(bv, true);
243 case LFUN_MATH_EXTERN:
244 bv->lockedInsetStoreUndo(Undo::EDIT);
245 handleExtern(arg, bv);
246 updateLocal(bv, true);
249 case LFUN_MATH_MUTATE:
251 bv->lockedInsetStoreUndo(Undo::EDIT);
254 mathcursor->GetPos(x, y);
256 mathcursor->SetPos(x, y);
257 mathcursor->normalize();
258 updateLocal(bv, true);
262 case LFUN_MATH_DISPLAY:
266 mathcursor->GetPos(x, y);
267 if (par()->GetType() == LM_OT_SIMPLE)
268 par()->mutate(LM_OT_EQUATION);
270 par()->mutate(LM_OT_SIMPLE);
271 mathcursor->SetPos(x, y);
272 mathcursor->normalize();
273 updateLocal(bv, true);
278 result = InsetFormulaBase::localDispatch(bv, action, arg);
285 void InsetFormula::handleExtern(const string & arg, BufferView *)
287 //string outfile = lyx::tempName("maple.out");
288 string outfile = "/tmp/lyx2" + arg + ".out";
290 par()->WriteNormal(os);
291 string code = os.str().c_str();
292 string script = "lyx2" + arg + " '" + code + "' " + outfile;
293 lyxerr << "calling: " << script << endl;
294 Systemcalls cmd(Systemcalls::System, script, 0);
296 ifstream is(outfile.c_str());
297 par(mathed_parse(is));
301 bool InsetFormula::display() const
303 return par_->GetType() != LM_OT_SIMPLE;
307 MathMatrixInset * InsetFormula::par() const
309 return static_cast<MathMatrixInset *>(par_);
312 void InsetFormula::par(MathInset * p)
315 par_ = p ? p : new MathMatrixInset;
319 Inset::Code InsetFormula::lyxCode() const
321 return Inset::MATH_CODE;
325 void InsetFormula::validate(LaTeXFeatures & features) const
327 par()->Validate(features);
331 int InsetFormula::ascent(BufferView *, LyXFont const &) const
333 return par()->ascent() + 1;
337 int InsetFormula::descent(BufferView *, LyXFont const &) const
339 return par()->descent() + 1;
343 int InsetFormula::width(BufferView *, LyXFont const &) const
346 return par()->width();