1999-09-27 18:44:28 +00:00
|
|
|
|
/*
|
2001-07-06 12:09:32 +00:00
|
|
|
|
* File: formulamacro.C
|
|
|
|
|
* Purpose: Implementation of the formula macro LyX inset
|
|
|
|
|
* Author: Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
* Created: March 2001
|
|
|
|
|
* Description: Allows the edition of math macros inside Lyx.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*
|
2001-07-06 12:09:32 +00:00
|
|
|
|
* Copyright: 2001 The LyX Project
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*
|
|
|
|
|
* 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"
|
2001-06-25 00:06:33 +00:00
|
|
|
|
#include "math_matrixinset.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"
|
2001-06-25 00:06:33 +00:00
|
|
|
|
#include "lyxlex.h"
|
2001-07-29 15:34:18 +00:00
|
|
|
|
#include "lyxfont.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-06-27 15:57:57 +00:00
|
|
|
|
using std::ostream;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
extern MathCursor * mathcursor;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
InsetFormulaMacro::InsetFormulaMacro()
|
2001-08-03 09:54:48 +00:00
|
|
|
|
: tmacro_(new MathMacroTemplate("unknown", 0))
|
2001-06-25 00:06:33 +00:00
|
|
|
|
{}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2001-02-26 12:53:35 +00:00
|
|
|
|
InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
|
2001-08-03 09:54:48 +00:00
|
|
|
|
: tmacro_(new MathMacroTemplate(nm, na))
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-08-03 09:54:48 +00:00
|
|
|
|
MathMacroTable::insertTemplate(tmacro_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InsetFormulaMacro::~InsetFormulaMacro()
|
|
|
|
|
{
|
|
|
|
|
#ifdef WITH_WARNINGS
|
|
|
|
|
#warning Need to unregister from MathMacroTable.
|
|
|
|
|
#endif
|
|
|
|
|
// Instead of unregister an delete leak this until it gets fixed
|
|
|
|
|
//delete tmacro_;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-07-06 15:57:54 +00:00
|
|
|
|
Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-08-03 09:54:48 +00:00
|
|
|
|
#ifdef WITH_WARNINGS
|
|
|
|
|
#warning This should not be needed in reality...
|
|
|
|
|
#endif
|
2001-04-24 16:13:38 +00:00
|
|
|
|
return new InsetFormulaMacro(*this);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
|
void InsetFormulaMacro::write(ostream & os) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-12-07 00:44:53 +00:00
|
|
|
|
os << "FormulaMacro ";
|
2001-08-03 09:54:48 +00:00
|
|
|
|
tmacro().write(os, false);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
|
int InsetFormulaMacro::latex(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-08-03 09:54:48 +00:00
|
|
|
|
tmacro().write(os, fragile);
|
2000-09-05 14:15:09 +00:00
|
|
|
|
return 2;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
|
int InsetFormulaMacro::ascii(ostream & os, int) const
|
2001-06-25 00:06:33 +00:00
|
|
|
|
{
|
2001-08-03 09:54:48 +00:00
|
|
|
|
tmacro().write(os, false);
|
2001-06-25 00:06:33 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
|
int InsetFormulaMacro::linuxdoc(ostream & os) const
|
2000-03-06 02:42:40 +00:00
|
|
|
|
{
|
2001-07-06 12:09:32 +00:00
|
|
|
|
return ascii(os, 0);
|
2000-03-06 02:42:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
|
int InsetFormulaMacro::docBook(ostream & os) const
|
2000-03-06 02:42:40 +00:00
|
|
|
|
{
|
2001-07-06 12:09:32 +00:00
|
|
|
|
return ascii(os, 0);
|
2000-03-06 02:42:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
|
void InsetFormulaMacro::read(LyXLex & lex)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
|
// Awful hack...
|
2001-08-03 09:54:48 +00:00
|
|
|
|
delete tmacro_;
|
|
|
|
|
tmacro_ = mathed_parse_macro(lex);
|
|
|
|
|
MathMacroTable::insertTemplate(tmacro_);
|
|
|
|
|
metrics();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
|
|
|
|
string InsetFormulaMacro::prefix() const
|
2000-02-10 17:53:36 +00:00
|
|
|
|
{
|
2001-08-03 09:54:48 +00:00
|
|
|
|
return string(" ") + _("Macro: ") + tmacro().name() + ": ";
|
2000-02-10 17:53:36 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
|
2000-02-10 17:53:36 +00:00
|
|
|
|
{
|
2001-08-03 09:54:48 +00:00
|
|
|
|
return tmacro().ascent() + 5;
|
2000-02-10 17:53:36 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
|
2001-04-24 16:13:38 +00:00
|
|
|
|
{
|
2001-08-03 09:54:48 +00:00
|
|
|
|
return tmacro().descent() + 5;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
|
|
|
|
int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
|
2000-02-10 17:53:36 +00:00
|
|
|
|
{
|
2001-08-03 09:54:48 +00:00
|
|
|
|
metrics();
|
|
|
|
|
return 10 + lyxfont::width(prefix(), f) + tmacro().width();
|
2000-02-10 17:53:36 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
|
|
2000-02-25 13:35:38 +00:00
|
|
|
|
UpdatableInset::RESULT
|
2001-06-28 10:25:20 +00:00
|
|
|
|
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-06-25 00:06:33 +00:00
|
|
|
|
RESULT result = DISPATCHED;
|
|
|
|
|
switch (action) {
|
|
|
|
|
case LFUN_MATH_MACROARG: {
|
|
|
|
|
int const i = lyx::atoi(arg);
|
|
|
|
|
lyxerr << "inserting macro arg " << i << "\n";
|
2001-08-03 09:54:48 +00:00
|
|
|
|
if (i > 0 && i <= tmacro().numargs()) {
|
2001-06-25 00:06:33 +00:00
|
|
|
|
mathcursor->insert(new MathMacroArgument(i));
|
2001-07-17 09:46:07 +00:00
|
|
|
|
updateLocal(bv, true);
|
2001-06-25 00:06:33 +00:00
|
|
|
|
} else {
|
2001-08-03 09:54:48 +00:00
|
|
|
|
lyxerr << "not in range 0.." << tmacro().numargs() << "\n";
|
2001-06-25 00:06:33 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2001-02-15 12:22:01 +00:00
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
|
|
|
|
default:
|
2001-06-28 10:25:20 +00:00
|
|
|
|
result = InsetFormulaBase::localDispatch(bv, action, arg);
|
2001-02-15 12:22:01 +00:00
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
return result;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
2001-08-03 09:54:48 +00:00
|
|
|
|
MathMacroTemplate const & InsetFormulaMacro::tmacro() const
|
2001-04-24 16:13:38 +00:00
|
|
|
|
{
|
2001-08-03 09:54:48 +00:00
|
|
|
|
return *tmacro_;
|
2001-06-25 00:06:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-06-28 10:25:20 +00:00
|
|
|
|
Inset::Code InsetFormulaMacro::lyxCode() const
|
2001-06-25 00:06:33 +00:00
|
|
|
|
{
|
|
|
|
|
return Inset::MATHMACRO_CODE;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
2001-08-01 13:28:45 +00:00
|
|
|
|
|
|
|
|
|
MathInsetTypes InsetFormulaMacro::getType() const
|
|
|
|
|
{
|
|
|
|
|
return LM_OT_MACRO;
|
|
|
|
|
}
|
2001-08-03 09:54:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MathInset * InsetFormulaMacro::par() const
|
|
|
|
|
{
|
|
|
|
|
return const_cast<MathMacroTemplate *>(tmacro_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InsetFormulaMacro::metrics() const
|
|
|
|
|
{
|
|
|
|
|
par()->metrics(LM_ST_TEXT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
|
|
|
|
|
int baseline, float & x, bool /*cleared*/) const
|
|
|
|
|
{
|
|
|
|
|
Painter & pain = bv->painter();
|
|
|
|
|
LyXFont font(f);
|
|
|
|
|
|
|
|
|
|
// label
|
|
|
|
|
font.setColor(LColor::math);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
|
|
|
|
|
pain.fillRectangle(int(x), y , w, h, LColor::mathmacrobg);
|
|
|
|
|
pain.rectangle(int(x), y, w, h, LColor::mathframe);
|
|
|
|
|
|
|
|
|
|
if (mathcursor && mathcursor->formula() == this)
|
|
|
|
|
mathcursor->drawSelection(pain);
|
|
|
|
|
|
|
|
|
|
pain.text(int(x + 2), baseline, prefix(), font);
|
|
|
|
|
x += width(bv, font);
|
|
|
|
|
|
|
|
|
|
// formula
|
|
|
|
|
float t = tmacro().width() + 5;
|
|
|
|
|
x -= t;
|
|
|
|
|
par()->draw(pain, int(x), baseline);
|
|
|
|
|
x += t;
|
|
|
|
|
}
|
|
|
|
|
|