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"
41 using std::ostringstream;
49 extern char const * latex_mathenv[];
50 extern MathCursor * mathcursor;
53 // quite a hack i know. Should be done with return values...
54 int number_of_newlines = 0;
57 InsetFormula::InsetFormula()
58 : InsetFormulaBase(new MathMatrixInset)
62 InsetFormula::InsetFormula(MathInsetTypes t)
63 : InsetFormulaBase(new MathMatrixInset(t))
67 InsetFormula::InsetFormula(string const & s)
70 istringstream is(s.c_str());
71 par(mathed_parse(is));
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(lex));
122 void InsetFormula::draw(BufferView * bv, LyXFont const &,
123 int y, float & xx, bool) const
127 MathInset::workwidth = bv->workWidth();
128 Painter & pain = bv->painter();
130 int w = par()->width();
131 int h = par()->height();
132 int a = par()->ascent();
133 pain.fillRectangle(int(x), y - a, w, h, LColor::mathbg);
138 if (mathcursor->formula() == this) {
139 if (mathcursor->Selection()) {
143 mathcursor->SelGetArea(xp, yp, n);
144 pain.fillPolygon(xp, yp, n, LColor::selection);
146 pain.rectangle(int(x), y - a, w, h, LColor::mathframe);
150 par()->draw(pain, int(x), y);
151 xx += par()->width();
153 setCursorVisible(false);
157 void InsetFormula::Metrics() const
159 const_cast<MathInset *>(par_)->Metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
162 vector<string> const InsetFormula::getLabelList() const
164 return par()->getLabelList();
168 UpdatableInset::RESULT
169 InsetFormula::localDispatch(BufferView * bv, kb_action action,
172 RESULT result = DISPATCHED;
177 bv->lockedInsetStoreUndo(Undo::INSERT);
178 mathcursor->breakLine();
182 case LFUN_MATH_NUMBER:
184 //lyxerr << "toggling all numbers\n";
186 bv->lockedInsetStoreUndo(Undo::INSERT);
187 bool old = par()->numberedType();
188 for (int row = 0; row < par()->nrows(); ++row)
189 par()->numbered(row, !old);
190 bv->owner()->message(old ? _("No number") : _("Number"));
196 case LFUN_MATH_NONUMBER:
198 //lyxerr << "toggling line number\n";
200 bv->lockedInsetStoreUndo(Undo::INSERT);
201 int row = mathcursor->row();
202 bool old = par()->numbered(row);
203 bv->owner()->message(old ? _("No number") : _("Number"));
204 par()->numbered(row, !old);
210 case LFUN_INSERT_LABEL:
212 bv->lockedInsetStoreUndo(Undo::INSERT);
214 int row = mathcursor->row();
215 string old_label = par()->label(row);
216 string new_label = arg;
218 if (new_label.empty()) {
219 string const default_label =
220 (lyxrc.label_init_length >= 0) ? "eq:" : "";
221 pair<bool, string> const res = old_label.empty()
222 ? askForText(_("Enter new label to insert:"), default_label)
223 : askForText(_("Enter label:"), old_label);
225 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
228 new_label = frontStrip(strip(res.second));
231 //if (new_label == old_label)
232 // break; // Nothing to do
234 if (!new_label.empty()) {
235 lyxerr << "setting label to '" << new_label << "'\n";
236 par()->numbered(row, true);
239 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
242 par()->label(row, new_label);
248 case LFUN_MATH_EXTERN:
249 bv->lockedInsetStoreUndo(Undo::EDIT);
250 handleExtern(arg, bv);
254 case LFUN_MATH_MUTATE:
256 bv->lockedInsetStoreUndo(Undo::EDIT);
259 mathcursor->GetPos(x, y);
261 mathcursor->SetPos(x, y);
262 mathcursor->normalize();
267 case LFUN_MATH_DISPLAY:
271 mathcursor->GetPos(x, y);
272 if (par()->GetType() == LM_OT_SIMPLE) {
273 par()->mutate(LM_OT_EQUATION);
274 par()->numbered(0, false);
277 par()->mutate(LM_OT_SIMPLE);
278 mathcursor->SetPos(x, y);
279 mathcursor->normalize();
285 result = InsetFormulaBase::localDispatch(bv, action, arg);
292 void InsetFormula::handleExtern(const string & arg, BufferView *)
294 //string outfile = lyx::tempName("maple.out");
295 string outfile = "/tmp/lyx2" + arg + ".out";
297 par()->WriteNormal(os);
298 string code = os.str().c_str();
299 string script = "lyx2" + arg + " '" + code + "' " + outfile;
300 lyxerr << "calling: " << script << endl;
301 Systemcalls cmd(Systemcalls::System, script, 0);
303 ifstream is(outfile.c_str());
304 par(mathed_parse(is));
308 bool InsetFormula::display() const
310 return par_->GetType() != LM_OT_SIMPLE;
314 MathMatrixInset * InsetFormula::par() const
316 return static_cast<MathMatrixInset *>(par_);
319 void InsetFormula::par(MathInset * p)
322 par_ = p ? p : new MathMatrixInset;
326 Inset::Code InsetFormula::lyxCode() const
328 return Inset::MATH_CODE;
332 void InsetFormula::validate(LaTeXFeatures & features) const
334 par()->Validate(features);
338 int InsetFormula::ascent(BufferView *, LyXFont const &) const
340 return par()->ascent();
344 int InsetFormula::descent(BufferView *, LyXFont const &) const
346 return par()->descent();
350 int InsetFormula::width(BufferView *, LyXFont const &) const
352 return par()->width();