1999-09-27 18:44:28 +00:00
|
|
|
/*
|
|
|
|
* File: formula.h
|
|
|
|
* 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.
|
|
|
|
*
|
2000-03-09 03:36:48 +00:00
|
|
|
* Copyright: 1996, 1997 Alejandro Aguilar Sierra
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
2001-02-15 12:22:01 +00:00
|
|
|
#pragma implementation
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "formulamacro.h"
|
|
|
|
#include "commandtags.h"
|
|
|
|
#include "math_cursor.h"
|
|
|
|
#include "math_parser.h"
|
|
|
|
#include "math_macro.h"
|
2001-04-24 16:13:38 +00:00
|
|
|
#include "math_macroarg.h"
|
2001-02-13 17:08:51 +00:00
|
|
|
#include "math_macrotable.h"
|
|
|
|
#include "math_macrotemplate.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "lyx_main.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "gettext.h"
|
2000-02-10 17:53:36 +00:00
|
|
|
#include "Painter.h"
|
2000-04-04 00:19:15 +00:00
|
|
|
#include "font.h"
|
2000-10-02 00:55:02 +00:00
|
|
|
#include "support/lyxlib.h"
|
2001-02-13 19:10:18 +00:00
|
|
|
#include "mathed/support.h"
|
2001-02-14 12:02:08 +00:00
|
|
|
#include "support/LOstream.h"
|
2001-02-26 12:53:35 +00:00
|
|
|
#include "debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
using std::ostream;
|
|
|
|
using std::istream;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-04-24 16:13:38 +00:00
|
|
|
extern MathedCursor * mathcursor;
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
InsetFormulaMacro::InsetFormulaMacro()
|
2001-02-15 12:22:01 +00:00
|
|
|
: InsetFormula(true)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-04-24 16:13:38 +00:00
|
|
|
par = &MathMacroTable::provideTemplate("unknown", 0);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 12:53:35 +00:00
|
|
|
InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
|
2001-04-24 16:13:38 +00:00
|
|
|
: InsetFormula(true)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-04-24 16:13:38 +00:00
|
|
|
par = &MathMacroTable::provideTemplate(nm, na);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-10 11:50:43 +00:00
|
|
|
Inset * InsetFormulaMacro::Clone(Buffer const &) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-04-24 16:13:38 +00:00
|
|
|
return new InsetFormulaMacro(*this);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-07 00:44:53 +00:00
|
|
|
os << "FormulaMacro ";
|
2001-04-24 16:13:38 +00:00
|
|
|
tmacro()->WriteDef(os, false);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/,
|
2000-03-09 16:13:43 +00:00
|
|
|
bool /*free_spacing*/) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-04-24 16:13:38 +00:00
|
|
|
tmacro()->WriteDef(os, true); // or false?
|
2000-09-05 14:15:09 +00:00
|
|
|
return 2;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
|
2000-03-06 02:42:40 +00:00
|
|
|
{
|
2001-02-15 12:22:01 +00:00
|
|
|
return Ascii(buf, os, 0);
|
2000-03-06 02:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
|
2000-03-06 02:42:40 +00:00
|
|
|
{
|
2001-02-15 12:22:01 +00:00
|
|
|
return Ascii(buf, os, 0);
|
2000-03-06 02:42:40 +00:00
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-01-06 02:44:26 +00:00
|
|
|
istream & is = lex.getStream();
|
2001-02-26 12:53:35 +00:00
|
|
|
mathed_parser_file(is, lex.GetLineNo());
|
2001-04-24 16:13:38 +00:00
|
|
|
|
|
|
|
MathParInset * tmp = new MathParInset;
|
2001-02-20 17:45:56 +00:00
|
|
|
MathedArray ar;
|
2001-04-24 16:13:38 +00:00
|
|
|
mathed_parse(ar, tmp, 0);
|
|
|
|
par = &MathMacroTable::provideTemplate(tmp->GetName(), tmp->xo());
|
|
|
|
par->setData(ar);
|
|
|
|
//cerr << "## InsetFormulaMacro::Read name: " << tmp->GetName() << endl;
|
|
|
|
//cerr << "## InsetFormulaMacro::Read nargs: " << tmp->xo() << endl;
|
|
|
|
//cerr << "## InsetFormulaMacro::Read 1: " << ar << endl;
|
2001-02-26 12:53:35 +00:00
|
|
|
|
2000-01-06 02:44:26 +00:00
|
|
|
// Update line number
|
|
|
|
lex.setLineNo(mathed_parser_lineno());
|
|
|
|
|
2000-05-15 14:49:36 +00:00
|
|
|
// reading of end_inset in the inset!!!
|
|
|
|
while (lex.IsOK()) {
|
|
|
|
lex.nextToken();
|
|
|
|
if (lex.GetString() == "\\end_inset")
|
|
|
|
break;
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2000-07-05 14:57:48 +00:00
|
|
|
int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-04-24 16:13:38 +00:00
|
|
|
return InsetFormula::ascent(pain, f);
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2000-07-05 14:57:48 +00:00
|
|
|
int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-04-24 16:13:38 +00:00
|
|
|
return InsetFormula::descent(pain, f);
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2001-04-24 16:13:38 +00:00
|
|
|
string InsetFormulaMacro::prefix() const
|
|
|
|
{
|
|
|
|
return string(" ") + _("Macro: ") + par->GetName() + ": ";
|
|
|
|
}
|
|
|
|
|
2000-07-05 14:57:48 +00:00
|
|
|
int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-04-24 16:13:38 +00:00
|
|
|
return 10 + lyxfont::width(prefix(), f) + InsetFormula::width(bv, f);
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2000-06-22 09:30:43 +00:00
|
|
|
void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
|
2000-06-23 15:02:46 +00:00
|
|
|
int baseline, float & x, bool cleared) const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2000-06-22 09:30:43 +00:00
|
|
|
Painter & pain = bv->painter();
|
2000-02-10 17:53:36 +00:00
|
|
|
LyXFont font(f);
|
2001-04-24 16:13:38 +00:00
|
|
|
|
|
|
|
// label
|
|
|
|
font.setColor(LColor::math);
|
2000-02-10 17:53:36 +00:00
|
|
|
|
2001-04-24 16:13:38 +00:00
|
|
|
int const y = baseline - ascent(bv, font) + 1;
|
|
|
|
int const w = width(bv, font) - 2;
|
|
|
|
int const h = (ascent(bv, font) + descent(bv, font) - 2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-04-24 16:13:38 +00:00
|
|
|
pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
|
|
|
|
pain.rectangle(int(x), y, w, h, LColor::mathframe);
|
|
|
|
|
|
|
|
pain.text(int(x + 2), baseline, prefix(), font);
|
|
|
|
x += width(bv, font);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-04-24 16:13:38 +00:00
|
|
|
// formula
|
|
|
|
float t = InsetFormula::width(bv, f) + 5;
|
|
|
|
x -= t;
|
|
|
|
InsetFormula::draw(bv, font, baseline, x, cleared);
|
|
|
|
x += t;
|
2000-04-04 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-24 16:13:38 +00:00
|
|
|
string const InsetFormulaMacro::EditMessage() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-04-24 16:13:38 +00:00
|
|
|
return _("Math macro editor mode");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-25 13:35:38 +00:00
|
|
|
UpdatableInset::RESULT
|
|
|
|
InsetFormulaMacro::LocalDispatch(BufferView * bv,
|
2001-02-23 16:10:03 +00:00
|
|
|
kb_action action, string const & arg)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-02-15 12:22:01 +00:00
|
|
|
if (action == LFUN_MATH_MACROARG) {
|
2001-04-24 16:13:38 +00:00
|
|
|
int const i = lyx::atoi(arg);
|
|
|
|
if (i > 0 && i <= tmacro()->nargs()) {
|
|
|
|
mathcursor->insertInset(new MathMacroArgument(i), LM_TC_INSET);
|
2001-02-15 12:22:01 +00:00
|
|
|
InsetFormula::UpdateLocal(bv);
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-02-15 12:22:01 +00:00
|
|
|
return DISPATCHED;
|
|
|
|
}
|
2001-04-24 16:13:38 +00:00
|
|
|
par->Metrics();
|
|
|
|
return InsetFormula::LocalDispatch(bv, action, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
MathMacroTemplate * InsetFormulaMacro::tmacro() const
|
|
|
|
{
|
|
|
|
return static_cast<MathMacroTemplate *>(par);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|