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;
55 extern LyXFont WhichFont(short type, int size);
59 // quite a hack i know. Should be done with return values...
60 int number_of_newlines = 0;
63 InsetFormula::InsetFormula()
64 : InsetFormulaBase(new MathMatrixInset)
68 InsetFormula::InsetFormula(MathInsetTypes t)
69 : InsetFormulaBase(new MathMatrixInset(t))
74 Inset * InsetFormula::clone(Buffer const &) const
76 return new InsetFormula(*this);
80 void InsetFormula::write(Buffer const * buf, ostream & os) const
83 latex(buf, os, false, false);
87 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
89 par()->Write(os, fragile);
94 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
96 par()->Write(os, false);
101 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
103 return ascii(buf, os, 0);
107 int InsetFormula::docBook(Buffer const * buf, ostream & os) const
109 return ascii(buf, os, 0);
113 void InsetFormula::read(Buffer const *, LyXLex & lex)
115 par_ = mathed_parse(lex);
119 void InsetFormula::draw(BufferView * bv, LyXFont const &,
120 int y, float & xx, bool) const
124 MathInset::workwidth = bv->workWidth();
125 Painter & pain = bv->painter();
127 int w = par()->width();
128 int h = par()->height();
129 int a = par()->ascent();
130 pain.fillRectangle(int(x), y - a, w, h, LColor::mathbg);
133 par()->Metrics(LM_ST_TEXT);
135 if (mathcursor->formula() == this) {
136 if (mathcursor->Selection()) {
140 mathcursor->SelGetArea(xp, yp, n);
141 pain.fillPolygon(xp, yp, n, LColor::selection);
143 pain.rectangle(int(x), y - a, w, h, LColor::mathframe);
147 par()->draw(pain, int(x), y);
148 xx += par()->width();
150 setCursorVisible(false);
154 vector<string> const InsetFormula::getLabelList() const
156 return par()->getLabelList();
160 UpdatableInset::RESULT
161 InsetFormula::localDispatch(BufferView * bv, kb_action action,
164 RESULT result = DISPATCHED;
169 bv->lockedInsetStoreUndo(Undo::INSERT);
175 case LFUN_DELETE_LINE_FORWARD:
176 bv->lockedInsetStoreUndo(Undo::DELETE);
177 mathcursor->DelLine();
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 handleExtern(arg, bv);
252 case LFUN_MATH_MUTATE:
258 lyxerr << "take index from cursor\n";
263 case LFUN_MATH_DISPLAY:
264 if (par()->GetType() == LM_OT_SIMPLE)
265 par()->mutate(LM_OT_EQUATION);
267 par()->mutate(LM_OT_SIMPLE);
272 result = InsetFormulaBase::localDispatch(bv, action, arg);
279 void InsetFormula::handleExtern(const string & arg, BufferView *)
281 //string outfile = lyx::tempName("maple.out");
282 string outfile = "/tmp/lyx2" + arg + ".out";
284 par()->WriteNormal(os);
285 string code = os.str().c_str();
286 string script = "lyx2" + arg + " '" + code + "' " + outfile;
287 lyxerr << "calling: " << script << endl;
288 Systemcalls cmd(Systemcalls::System, script, 0);
290 ifstream is(outfile.c_str());
291 par_ = mathed_parse(is);
294 bool InsetFormula::display() const
296 return par_->GetType() != LM_OT_SIMPLE;
300 MathMatrixInset * InsetFormula::par() const
302 return static_cast<MathMatrixInset *>(par_);
306 Inset::Code InsetFormula::lyxCode() const
308 return Inset::MATH_CODE;
312 void InsetFormula::validate(LaTeXFeatures & features) const
314 par()->Validate(features);
318 int InsetFormula::ascent(BufferView *, LyXFont const &) const
320 return par()->ascent() + 4;
324 int InsetFormula::descent(BufferView *, LyXFont const &) const
326 return par()->descent();
330 int InsetFormula::width(BufferView *, LyXFont const &) const
332 par()->Metrics(LM_ST_TEXT);
333 return par()->width();
337 LyXFont const InsetFormula::ConvertFont(LyXFont const & f) const
339 // We have already discussed what was here
341 font.setLatex(LyXFont::OFF);