1999-09-27 18:44:28 +00:00
|
|
|
/*
|
2001-02-14 19:35:25 +00:00
|
|
|
* File: formula.C
|
|
|
|
* Purpose: Implementation of formula inset
|
|
|
|
* Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
|
|
|
|
* Created: January 1996
|
|
|
|
* Description: Allows the edition of math paragraphs inside Lyx.
|
|
|
|
*
|
|
|
|
* Copyright: 1996-1998 Alejandro Aguilar Sierra
|
|
|
|
*
|
|
|
|
* Version: 0.4, Lyx project.
|
|
|
|
*
|
|
|
|
* You are free to use and modify this code under the terms of
|
|
|
|
* the GNU General Public Licence version 2 or later.
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
2001-04-25 15:43:57 +00:00
|
|
|
#include <fstream>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
#include "support/LOstream.h"
|
2000-03-28 02:18:55 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef __GNUG__
|
2001-02-15 12:22:01 +00:00
|
|
|
#pragma implementation
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "formula.h"
|
|
|
|
#include "commandtags.h"
|
|
|
|
#include "math_cursor.h"
|
|
|
|
#include "math_parser.h"
|
|
|
|
#include "lyx_main.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "lyxtext.h"
|
|
|
|
#include "gettext.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
#include "debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "lyx_gui_misc.h"
|
1999-12-07 00:44:53 +00:00
|
|
|
#include "support/LOstream.h"
|
2001-04-25 15:43:57 +00:00
|
|
|
#include "support/lyxlib.h"
|
|
|
|
#include "support/syscall.h"
|
1999-12-19 22:35:36 +00:00
|
|
|
#include "LyXView.h"
|
2000-02-10 17:53:36 +00:00
|
|
|
#include "Painter.h"
|
2000-04-04 00:19:15 +00:00
|
|
|
#include "font.h"
|
2000-12-11 09:46:09 +00:00
|
|
|
#include "lyxrc.h"
|
2001-02-13 13:28:32 +00:00
|
|
|
#include "math_matrixinset.h"
|
2001-02-13 19:10:18 +00:00
|
|
|
#include "mathed/support.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-04-25 15:43:57 +00:00
|
|
|
using std::ostringstream;
|
2000-04-04 00:19:15 +00:00
|
|
|
using std::ostream;
|
2001-04-25 15:43:57 +00:00
|
|
|
using std::ifstream;
|
2000-04-04 00:19:15 +00:00
|
|
|
using std::istream;
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::pair;
|
|
|
|
using std::endl;
|
2000-05-19 16:46:01 +00:00
|
|
|
using std::vector;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-02-16 09:25:43 +00:00
|
|
|
extern char const * latex_mathenv[];
|
2001-06-25 00:06:33 +00:00
|
|
|
extern MathCursor * mathcursor;
|
|
|
|
extern LyXFont WhichFont(short type, int size);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
// quite a hack i know. Should be done with return values...
|
|
|
|
int number_of_newlines = 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
InsetFormula::InsetFormula()
|
|
|
|
: InsetFormulaBase(new MathMatrixInset)
|
|
|
|
{}
|
1999-11-24 22:14:46 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
InsetFormula::InsetFormula(MathInsetTypes t)
|
|
|
|
: InsetFormulaBase(new MathMatrixInset(t))
|
|
|
|
{}
|
1999-11-24 22:14:46 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2000-10-10 11:50:43 +00:00
|
|
|
Inset * InsetFormula::Clone(Buffer const &) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return new InsetFormula(*this);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
void InsetFormula::Write(Buffer const * buf, ostream & os) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-07 00:44:53 +00:00
|
|
|
os << "Formula ";
|
2000-06-12 11:27:15 +00:00
|
|
|
Latex(buf, os, false, false);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
par()->Write(os, fragile);
|
|
|
|
return 1;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 15:25:14 +00:00
|
|
|
int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
|
2000-04-24 20:58:23 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
par()->Write(os, false);
|
|
|
|
return 1;
|
2000-04-24 20:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
|
2000-03-06 02:42:40 +00:00
|
|
|
{
|
2001-02-14 19:35:25 +00:00
|
|
|
return Ascii(buf, os, 0);
|
2000-03-06 02:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
|
2000-03-06 02:42:40 +00:00
|
|
|
{
|
2001-02-14 19:35:25 +00:00
|
|
|
return Ascii(buf, os, 0);
|
2000-03-06 02:42:40 +00:00
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void InsetFormula::Read(Buffer const *, LyXLex & lex)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
par_ = mathed_parse(lex);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void InsetFormula::draw(BufferView * bv, LyXFont const &,
|
|
|
|
int y, float & x, bool) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
MathInset::workwidth = bv->workWidth();
|
|
|
|
Painter & pain = bv->painter();
|
2001-02-14 19:35:25 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
if (mathcursor) {
|
|
|
|
par()->Metrics(LM_ST_TEXT);
|
|
|
|
int w = par()->width() + 2;
|
|
|
|
int a = par()->ascent() + 1;
|
|
|
|
int h = par()->height() + 1;
|
|
|
|
|
|
|
|
if (mathcursor->formula() == this) {
|
|
|
|
if (mathcursor->Selection()) {
|
|
|
|
int xp[10];
|
|
|
|
int yp[10];
|
|
|
|
int n;
|
|
|
|
mathcursor->SelGetArea(xp, yp, n);
|
|
|
|
pain.fillPolygon(xp, yp, n, LColor::selection);
|
|
|
|
}
|
|
|
|
pain.rectangle(int(x - 1), y - a, w, h, LColor::green);
|
|
|
|
}
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
par()->draw(pain, int(x), y);
|
|
|
|
x += par()->width();
|
2001-02-14 19:35:25 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
setCursorVisible(false);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
vector<string> const InsetFormula::getLabelList() const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return par()->getLabelList();
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
UpdatableInset::RESULT
|
|
|
|
InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
|
|
|
|
string const & arg)
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
RESULT result = DISPATCHED;
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
switch (action) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_BREAKLINE:
|
|
|
|
bv->lockedInsetStoreUndo(Undo::INSERT);
|
|
|
|
par()->breakLine();
|
|
|
|
UpdateLocal(bv);
|
|
|
|
break;
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-02-03 21:02:22 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_DELETE_LINE_FORWARD:
|
|
|
|
bv->lockedInsetStoreUndo(Undo::DELETE);
|
|
|
|
mathcursor->DelLine();
|
|
|
|
UpdateLocal(bv);
|
|
|
|
break;
|
2001-02-14 19:35:25 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_MATH_NUMBER:
|
|
|
|
{
|
|
|
|
//lyxerr << "toggling all numbers\n";
|
|
|
|
if (display()) {
|
|
|
|
bv->lockedInsetStoreUndo(Undo::INSERT);
|
|
|
|
bool old = par()->numberedType();
|
|
|
|
for (int row = 0; row < par()->nrows(); ++row)
|
|
|
|
par()->numbered(row, !old);
|
|
|
|
bv->owner()->message(old ? _("No number") : _("Number"));
|
|
|
|
UpdateLocal(bv);
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
break;
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_MATH_NONUMBER:
|
|
|
|
{
|
|
|
|
//lyxerr << "toggling line number\n";
|
|
|
|
if (display()) {
|
|
|
|
bv->lockedInsetStoreUndo(Undo::INSERT);
|
|
|
|
int row = par()->nrows() - 1;
|
|
|
|
bool old = par()->numbered(row);
|
|
|
|
bv->owner()->message(old ? _("No number") : _("Number"));
|
|
|
|
par()->numbered(row, !old);
|
|
|
|
UpdateLocal(bv);
|
|
|
|
}
|
|
|
|
break;
|
2001-02-14 19:35:25 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_INSERT_LABEL:
|
|
|
|
{
|
|
|
|
bv->lockedInsetStoreUndo(Undo::INSERT);
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
int row = par()->nrows() - 1;
|
|
|
|
string old_label = par()->label(row);
|
|
|
|
string new_label = arg;
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
if (new_label.empty()) {
|
|
|
|
string const default_label =
|
|
|
|
(lyxrc.label_init_length >= 0) ? "eq:" : "";
|
|
|
|
pair<bool, string> const res = old_label.empty()
|
|
|
|
? askForText(_("Enter new label to insert:"), default_label)
|
|
|
|
: askForText(_("Enter label:"), old_label);
|
|
|
|
|
|
|
|
lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
|
|
|
|
if (!res.first)
|
|
|
|
break;
|
|
|
|
new_label = frontStrip(strip(res.second));
|
|
|
|
}
|
2001-02-14 19:35:25 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
//if (new_label == old_label)
|
|
|
|
// break; // Nothing to do
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
if (!new_label.empty()) {
|
|
|
|
lyxerr << "setting label to '" << new_label << "'\n";
|
|
|
|
par()->numbered(row, true);
|
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
|
|
|
|
bv->redraw();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
par()->label(row, new_label);
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
UpdateLocal(bv);
|
|
|
|
break;
|
2001-02-14 19:35:25 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_MATH_EXTERN:
|
|
|
|
HandleExtern(arg, bv);
|
|
|
|
UpdateLocal(bv);
|
|
|
|
break;
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_MATH_MUTATE:
|
|
|
|
par()->mutate(arg);
|
|
|
|
UpdateLocal(bv);
|
|
|
|
break;
|
2001-02-14 19:35:25 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_TABINSERT:
|
|
|
|
lyxerr << "take index from cursor\n";
|
|
|
|
par()->splitCell(0);
|
|
|
|
UpdateLocal(bv);
|
|
|
|
break;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
case LFUN_MATH_DISPLAY:
|
|
|
|
if (par()->GetType() == LM_OT_SIMPLE)
|
|
|
|
par()->mutate(LM_OT_EQUATION);
|
|
|
|
else
|
|
|
|
par()->mutate(LM_OT_SIMPLE);
|
|
|
|
UpdateLocal(bv);
|
|
|
|
break;
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
default:
|
|
|
|
result = InsetFormulaBase::LocalDispatch(bv, action, arg);
|
2001-02-14 19:35:25 +00:00
|
|
|
}
|
2000-05-19 16:46:01 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
return result;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void InsetFormula::HandleExtern(const string & arg, BufferView *)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
//string outfile = lyx::tempName("maple.out");
|
|
|
|
string outfile = "/tmp/lyx2" + arg + ".out";
|
|
|
|
ostringstream os;
|
|
|
|
par()->WriteNormal(os);
|
|
|
|
string code = os.str().c_str();
|
|
|
|
string script = "lyx2" + arg + " '" + code + "' " + outfile;
|
|
|
|
lyxerr << "calling: " << script << endl;
|
|
|
|
Systemcalls cmd(Systemcalls::System, script, 0);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
ifstream is(outfile.c_str());
|
|
|
|
par_ = mathed_parse(is);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
bool InsetFormula::display() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return par_->GetType() != LM_OT_SIMPLE;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
MathMatrixInset * InsetFormula::par() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return static_cast<MathMatrixInset *>(par_);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
Inset::Code InsetFormula::LyxCode() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return Inset::MATH_CODE;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void InsetFormula::Validate(LaTeXFeatures & features) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
par()->Validate(features);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
int InsetFormula::ascent(BufferView *, LyXFont const &) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return par()->ascent();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2001-02-16 09:25:43 +00:00
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
int InsetFormula::descent(BufferView *, LyXFont const &) const
|
2001-02-16 09:25:43 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return par()->descent();
|
2001-02-16 09:25:43 +00:00
|
|
|
}
|
2001-03-16 12:08:14 +00:00
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
int InsetFormula::width(BufferView *, LyXFont const &) const
|
2001-03-16 12:08:14 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
par()->Metrics(LM_ST_TEXT);
|
|
|
|
return par()->width();
|
2001-03-16 12:08:14 +00:00
|
|
|
}
|
2001-04-25 15:43:57 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
/*
|
|
|
|
LyXFont const InsetFormula::ConvertFont(LyXFont const & f) const
|
2001-04-25 15:43:57 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
// We have already discussed what was here
|
|
|
|
LyXFont font(f);
|
|
|
|
font.setLatex(LyXFont::OFF);
|
|
|
|
return font;
|
2001-04-25 15:43:57 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
*/
|