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.
19 #include "support/LOstream.h"
22 #pragma implementation
26 #include "commandtags.h"
27 #include "math_cursor.h"
28 #include "math_parser.h"
30 #include "BufferView.h"
34 #include "lyx_gui_misc.h"
35 #include "support/LOstream.h"
36 #include "support/lyxlib.h"
37 #include "support/syscall.h"
42 #include "math_matrixinset.h"
43 #include "mathed/support.h"
45 using std::ostringstream;
53 extern char const * latex_mathenv[];
54 extern MathCursor * mathcursor;
57 // quite a hack i know. Should be done with return values...
58 int number_of_newlines = 0;
61 InsetFormula::InsetFormula()
62 : InsetFormulaBase(new MathMatrixInset)
66 InsetFormula::InsetFormula(MathInsetTypes t)
67 : InsetFormulaBase(new MathMatrixInset(t))
71 InsetFormula::InsetFormula(string const & s)
74 istringstream is(s.c_str());
75 par(mathed_parse(is));
79 Inset * InsetFormula::clone(Buffer const &, bool) const
81 return new InsetFormula(*this);
85 void InsetFormula::write(ostream & os) const
88 latex(os, false, false);
92 int InsetFormula::latex(ostream & os, bool fragile, bool) const
94 par()->Write(os, fragile);
99 int InsetFormula::ascii(ostream & os, int) const
101 par()->Write(os, false);
106 int InsetFormula::linuxdoc(ostream & os) const
112 int InsetFormula::docBook(ostream & os) const
118 void InsetFormula::read(LyXLex & lex)
120 par(mathed_parse(lex));
124 void InsetFormula::draw(BufferView * bv, LyXFont const &,
125 int y, float & xx, bool) const
129 MathInset::workwidth = bv->workWidth();
130 Painter & pain = bv->painter();
132 int w = par()->width();
133 int h = par()->height();
134 int a = par()->ascent();
135 pain.fillRectangle(int(x), y - a, w, h, LColor::mathbg);
138 par()->Metrics(LM_ST_TEXT);
140 if (mathcursor->formula() == this) {
141 if (mathcursor->Selection()) {
145 mathcursor->SelGetArea(xp, yp, n);
146 pain.fillPolygon(xp, yp, n, LColor::selection);
148 pain.rectangle(int(x), y - a, w, h, LColor::mathframe);
152 par()->draw(pain, int(x), y);
153 xx += par()->width();
155 setCursorVisible(false);
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);
271 par()->numbered(0, false);
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));
304 bool InsetFormula::display() const
306 return par_->GetType() != LM_OT_SIMPLE;
310 MathMatrixInset * InsetFormula::par() const
312 return static_cast<MathMatrixInset *>(par_);
315 void InsetFormula::par(MathInset * p)
318 par_ = p ? p : new MathMatrixInset;
322 Inset::Code InsetFormula::lyxCode() const
324 return Inset::MATH_CODE;
328 void InsetFormula::validate(LaTeXFeatures & features) const
330 par()->Validate(features);
334 int InsetFormula::ascent(BufferView *, LyXFont const &) const
336 return par()->ascent() + 4;
340 int InsetFormula::descent(BufferView *, LyXFont const &) const
342 return par()->descent();
346 int InsetFormula::width(BufferView *, LyXFont const &) const
348 par()->Metrics(LM_ST_TEXT);
349 return par()->width();