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();
131 int w = par()->width();
132 int h = par()->height();
133 int a = par()->ascent();
134 pain.fillRectangle(int(x), y - a, w, h, LColor::mathbg);
137 if (mathcursor->formula() == this) {
138 if (mathcursor->Selection()) {
142 mathcursor->SelGetArea(xp, yp, n);
143 pain.fillPolygon(xp, yp, n, LColor::selection);
145 pain.rectangle(int(x), y - a, w, h, LColor::mathframe);
149 par()->draw(pain, int(x), y);
150 xx += par()->width();
152 setCursorVisible(false);
156 void InsetFormula::Metrics() const
158 const_cast<MathInset *>(par_)->Metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
161 vector<string> const InsetFormula::getLabelList() const
163 return par()->getLabelList();
167 UpdatableInset::RESULT
168 InsetFormula::localDispatch(BufferView * bv, kb_action action,
171 RESULT result = DISPATCHED;
176 bv->lockedInsetStoreUndo(Undo::INSERT);
177 mathcursor->breakLine();
181 case LFUN_MATH_NUMBER:
183 //lyxerr << "toggling all numbers\n";
185 bv->lockedInsetStoreUndo(Undo::INSERT);
186 bool old = par()->numberedType();
187 for (int row = 0; row < par()->nrows(); ++row)
188 par()->numbered(row, !old);
189 bv->owner()->message(old ? _("No number") : _("Number"));
195 case LFUN_MATH_NONUMBER:
197 //lyxerr << "toggling line number\n";
199 bv->lockedInsetStoreUndo(Undo::INSERT);
200 int row = mathcursor->row();
201 bool old = par()->numbered(row);
202 bv->owner()->message(old ? _("No number") : _("Number"));
203 par()->numbered(row, !old);
209 case LFUN_INSERT_LABEL:
211 bv->lockedInsetStoreUndo(Undo::INSERT);
213 int row = mathcursor->row();
214 string old_label = par()->label(row);
215 string new_label = arg;
217 if (new_label.empty()) {
218 string const default_label =
219 (lyxrc.label_init_length >= 0) ? "eq:" : "";
220 pair<bool, string> const res = old_label.empty()
221 ? askForText(_("Enter new label to insert:"), default_label)
222 : askForText(_("Enter label:"), old_label);
224 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
227 new_label = frontStrip(strip(res.second));
230 //if (new_label == old_label)
231 // break; // Nothing to do
233 if (!new_label.empty()) {
234 lyxerr << "setting label to '" << new_label << "'\n";
235 par()->numbered(row, true);
238 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
241 par()->label(row, new_label);
247 case LFUN_MATH_EXTERN:
248 bv->lockedInsetStoreUndo(Undo::EDIT);
249 handleExtern(arg, bv);
253 case LFUN_MATH_MUTATE:
255 bv->lockedInsetStoreUndo(Undo::EDIT);
258 mathcursor->GetPos(x, y);
260 mathcursor->SetPos(x, y);
261 mathcursor->normalize();
266 case LFUN_MATH_DISPLAY:
270 mathcursor->GetPos(x, y);
271 if (par()->GetType() == LM_OT_SIMPLE)
272 par()->mutate(LM_OT_EQUATION);
274 par()->mutate(LM_OT_SIMPLE);
275 mathcursor->SetPos(x, y);
276 mathcursor->normalize();
282 result = InsetFormulaBase::localDispatch(bv, action, arg);
289 void InsetFormula::handleExtern(const string & arg, BufferView *)
291 //string outfile = lyx::tempName("maple.out");
292 string outfile = "/tmp/lyx2" + arg + ".out";
294 par()->WriteNormal(os);
295 string code = os.str().c_str();
296 string script = "lyx2" + arg + " '" + code + "' " + outfile;
297 lyxerr << "calling: " << script << endl;
298 Systemcalls cmd(Systemcalls::System, script, 0);
300 ifstream is(outfile.c_str());
301 par(mathed_parse(is));
305 bool InsetFormula::display() const
307 return par_->GetType() != LM_OT_SIMPLE;
311 MathMatrixInset * InsetFormula::par() const
313 return static_cast<MathMatrixInset *>(par_);
316 void InsetFormula::par(MathInset * p)
319 par_ = p ? p : new MathMatrixInset;
323 Inset::Code InsetFormula::lyxCode() const
325 return Inset::MATH_CODE;
329 void InsetFormula::validate(LaTeXFeatures & features) const
331 par()->Validate(features);
335 int InsetFormula::ascent(BufferView *, LyXFont const &) const
337 return par()->ascent();
341 int InsetFormula::descent(BufferView *, LyXFont const &) const
343 return par()->descent();
347 int InsetFormula::width(BufferView *, LyXFont const &) const
349 return par()->width();