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 & x, bool) const
122 MathInset::workwidth = bv->workWidth();
123 Painter & pain = bv->painter();
126 par()->Metrics(LM_ST_TEXT);
127 int w = par()->width() + 2;
128 int a = par()->ascent() + 1;
129 int h = par()->height() + 1;
131 if (mathcursor->formula() == this) {
132 if (mathcursor->Selection()) {
136 mathcursor->SelGetArea(xp, yp, n);
137 pain.fillPolygon(xp, yp, n, LColor::selection);
139 pain.rectangle(int(x - 1), y - a, w, h, LColor::green);
143 par()->draw(pain, int(x), y);
146 setCursorVisible(false);
150 vector<string> const InsetFormula::getLabelList() const
152 return par()->getLabelList();
156 UpdatableInset::RESULT
157 InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
160 RESULT result = DISPATCHED;
165 bv->lockedInsetStoreUndo(Undo::INSERT);
171 case LFUN_DELETE_LINE_FORWARD:
172 bv->lockedInsetStoreUndo(Undo::DELETE);
173 mathcursor->DelLine();
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"));
191 case LFUN_MATH_NONUMBER:
193 //lyxerr << "toggling line number\n";
195 bv->lockedInsetStoreUndo(Undo::INSERT);
196 int row = par()->nrows() - 1;
197 bool old = par()->numbered(row);
198 bv->owner()->message(old ? _("No number") : _("Number"));
199 par()->numbered(row, !old);
205 case LFUN_INSERT_LABEL:
207 bv->lockedInsetStoreUndo(Undo::INSERT);
209 int row = par()->nrows() - 1;
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);
243 case LFUN_MATH_EXTERN:
244 HandleExtern(arg, bv);
248 case LFUN_MATH_MUTATE:
254 lyxerr << "take index from cursor\n";
259 case LFUN_MATH_DISPLAY:
260 if (par()->GetType() == LM_OT_SIMPLE)
261 par()->mutate(LM_OT_EQUATION);
263 par()->mutate(LM_OT_SIMPLE);
268 result = InsetFormulaBase::LocalDispatch(bv, action, arg);
275 void InsetFormula::HandleExtern(const string & arg, BufferView *)
277 //string outfile = lyx::tempName("maple.out");
278 string outfile = "/tmp/lyx2" + arg + ".out";
280 par()->WriteNormal(os);
281 string code = os.str().c_str();
282 string script = "lyx2" + arg + " '" + code + "' " + outfile;
283 lyxerr << "calling: " << script << endl;
284 Systemcalls cmd(Systemcalls::System, script, 0);
286 ifstream is(outfile.c_str());
287 par_ = mathed_parse(is);
290 bool InsetFormula::display() const
292 return par_->GetType() != LM_OT_SIMPLE;
296 MathMatrixInset * InsetFormula::par() const
298 return static_cast<MathMatrixInset *>(par_);
302 Inset::Code InsetFormula::LyxCode() const
304 return Inset::MATH_CODE;
308 void InsetFormula::Validate(LaTeXFeatures & features) const
310 par()->Validate(features);
314 int InsetFormula::ascent(BufferView *, LyXFont const &) const
316 return par()->ascent();
320 int InsetFormula::descent(BufferView *, LyXFont const &) const
322 return par()->descent();
326 int InsetFormula::width(BufferView *, LyXFont const &) const
328 par()->Metrics(LM_ST_TEXT);
329 return par()->width();
333 LyXFont const InsetFormula::ConvertFont(LyXFont const & f) const
335 // We have already discussed what was here
337 font.setLatex(LyXFont::OFF);