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
|
2001-12-11 17:47:27 +00:00
|
|
|
|
* Author: Andr<EFBFBD> P<EFBFBD>nitz, based on ideas of Alejandro Aguilar Sierra
|
2001-07-06 12:09:32 +00:00
|
|
|
|
* Created: March 2001
|
2002-03-21 17:42:56 +00:00
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
|
|
#include "formulamacro.h"
|
2003-03-19 17:15:32 +00:00
|
|
|
|
#include "lfuns.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#include "math_cursor.h"
|
|
|
|
|
#include "math_parser.h"
|
|
|
|
|
#include "math_macro.h"
|
2001-02-13 17:08:51 +00:00
|
|
|
|
#include "math_macrotable.h"
|
2002-07-12 14:24:47 +00:00
|
|
|
|
#include "math_macrotemplate.h"
|
2003-03-21 14:20:48 +00:00
|
|
|
|
#include "metricsinfo.h"
|
2001-12-11 17:47:27 +00:00
|
|
|
|
#include "math_support.h"
|
|
|
|
|
#include "math_mathmlstream.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#include "BufferView.h"
|
|
|
|
|
#include "gettext.h"
|
2003-05-22 22:44:30 +00:00
|
|
|
|
#include "latexrunparams.h"
|
2002-05-23 09:21:32 +00:00
|
|
|
|
#include "frontends/Painter.h"
|
2002-05-24 14:34:32 +00:00
|
|
|
|
#include "frontends/font_metrics.h"
|
2000-10-02 00:55:02 +00:00
|
|
|
|
#include "support/lyxlib.h"
|
2003-06-02 10:03:27 +00:00
|
|
|
|
#include "support/lstrings.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-10-12 12:58:50 +00:00
|
|
|
|
#include "lyxtext.h"
|
2002-07-14 17:25:25 +00:00
|
|
|
|
#include "Lsstream.h"
|
2002-07-12 14:24:47 +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
|
|
|
|
|
2002-07-12 14:24:47 +00:00
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
InsetFormulaMacro::InsetFormulaMacro()
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-08-14 07:46:11 +00:00
|
|
|
|
// inset name is inherited from Inset
|
2001-08-07 08:40:14 +00:00
|
|
|
|
setInsetName("unknown");
|
2001-08-03 09:54:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-27 11:02:56 +00:00
|
|
|
|
InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
|
2001-08-03 09:54:48 +00:00
|
|
|
|
{
|
2002-02-01 17:01:30 +00:00
|
|
|
|
setInsetName(name);
|
2002-07-12 14:24:47 +00:00
|
|
|
|
MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs)));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-08-14 07:46:11 +00:00
|
|
|
|
InsetFormulaMacro::InsetFormulaMacro(string const & s)
|
|
|
|
|
{
|
2002-11-04 02:12:42 +00:00
|
|
|
|
std::istringstream is(STRCONV(s));
|
2002-07-12 14:24:47 +00:00
|
|
|
|
read(is);
|
2001-08-14 07:46:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-26 09:13:55 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-26 09:13:55 +00:00
|
|
|
|
// Inset * InsetFormulaMacro::clone(Buffer const &, bool) const
|
|
|
|
|
// {
|
|
|
|
|
// return new InsetFormulaMacro(*this);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
2001-12-03 16:24:50 +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 ";
|
2002-03-25 12:11:25 +00:00
|
|
|
|
WriteStream wi(os, false, false);
|
2001-10-19 11:25:48 +00:00
|
|
|
|
par()->write(wi);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-22 22:44:30 +00:00
|
|
|
|
int InsetFormulaMacro::latex(Buffer const *, ostream & os,
|
2003-05-23 08:59:47 +00:00
|
|
|
|
LatexRunParams const & runparams) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2003-05-23 09:23:03 +00:00
|
|
|
|
WriteStream wi(os, runparams.moving_arg, true);
|
2001-10-19 11:25:48 +00:00
|
|
|
|
par()->write(wi);
|
2000-09-05 14:15:09 +00:00
|
|
|
|
return 2;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-10-19 11:25:48 +00:00
|
|
|
|
|
2001-12-03 16:24:50 +00:00
|
|
|
|
int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
|
2001-06-25 00:06:33 +00:00
|
|
|
|
{
|
2002-03-25 12:11:25 +00:00
|
|
|
|
WriteStream wi(os, false, true);
|
2001-10-19 11:25:48 +00:00
|
|
|
|
par()->write(wi);
|
2001-06-25 00:06:33 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-09-24 13:38:52 +00:00
|
|
|
|
int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
|
2000-03-06 02:42:40 +00:00
|
|
|
|
{
|
2001-09-24 13:38:52 +00:00
|
|
|
|
return ascii(buf, os, 0);
|
2000-03-06 02:42:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
|
int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
|
2000-03-06 02:42:40 +00:00
|
|
|
|
{
|
2001-09-24 13:38:52 +00:00
|
|
|
|
return ascii(buf, os, 0);
|
2000-03-06 02:42:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-09-24 13:38:52 +00:00
|
|
|
|
void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2002-07-12 14:24:47 +00:00
|
|
|
|
read(lex.getStream());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InsetFormulaMacro::read(std::istream & is)
|
|
|
|
|
{
|
|
|
|
|
MathMacroTemplate * p = new MathMacroTemplate(is);
|
|
|
|
|
setInsetName(p->name());
|
2002-07-25 07:55:56 +00:00
|
|
|
|
MathMacroTable::create(MathAtom(p));
|
2003-06-02 10:03:27 +00:00
|
|
|
|
//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
|
|
|
|
{
|
2003-06-02 10:03:27 +00:00
|
|
|
|
return bformat(_(" Macro: %s: "), getInsetName());
|
2000-02-10 17:53:36 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2003-06-02 17:58:19 +00:00
|
|
|
|
void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
|
2000-02-10 17:53:36 +00:00
|
|
|
|
{
|
2003-06-02 10:03:27 +00:00
|
|
|
|
par()->metrics(mi, dim_);
|
|
|
|
|
dim_.asc += 5;
|
|
|
|
|
dim_.des += 5;
|
|
|
|
|
dim_.wid += 10 + font_metrics::width(prefix(), mi.base.font);
|
|
|
|
|
dim = dim_;
|
2000-02-10 17:53:36 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
MathAtom const & InsetFormulaMacro::par() const
|
2001-04-24 16:13:38 +00:00
|
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
|
return MathMacroTable::provide(getInsetName());
|
2001-06-25 00:06:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
MathAtom & InsetFormulaMacro::par()
|
2001-06-25 00:06:33 +00:00
|
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
|
return MathMacroTable::provide(getInsetName());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
2001-08-01 13:28:45 +00:00
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
Inset::Code InsetFormulaMacro::lyxCode() const
|
2001-08-01 13:28:45 +00:00
|
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
|
return Inset::MATHMACRO_CODE;
|
2001-08-01 13:28:45 +00:00
|
|
|
|
}
|
2001-08-03 09:54:48 +00:00
|
|
|
|
|
|
|
|
|
|
2003-05-30 06:48:24 +00:00
|
|
|
|
void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
|
2001-08-03 09:54:48 +00:00
|
|
|
|
{
|
|
|
|
|
// label
|
2003-05-30 06:48:24 +00:00
|
|
|
|
LyXFont font = p.base.font;
|
2001-08-03 09:54:48 +00:00
|
|
|
|
font.setColor(LColor::math);
|
2002-03-21 17:42:56 +00:00
|
|
|
|
|
2003-05-30 06:48:24 +00:00
|
|
|
|
PainterInfo pi(p.base.bv);
|
2002-08-19 14:39:35 +00:00
|
|
|
|
pi.base.style = LM_ST_TEXT;
|
|
|
|
|
pi.base.font = font;
|
|
|
|
|
|
2003-06-02 17:58:19 +00:00
|
|
|
|
int const a = y - dim_.asc + 1;
|
|
|
|
|
int const w = dim_.wid - 2;
|
|
|
|
|
int const h = dim_.height() - 2;
|
2001-08-03 09:54:48 +00:00
|
|
|
|
|
|
|
|
|
// LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
|
2002-08-19 14:39:35 +00:00
|
|
|
|
pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
|
|
|
|
|
pi.pain.rectangle(x, a, w, h, LColor::mathframe);
|
2001-08-03 09:54:48 +00:00
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
|
if (mathcursor &&
|
|
|
|
|
const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
|
2002-08-19 14:39:35 +00:00
|
|
|
|
mathcursor->drawSelection(pi);
|
2001-08-03 09:54:48 +00:00
|
|
|
|
|
2002-08-19 14:39:35 +00:00
|
|
|
|
pi.pain.text(x + 2, y, prefix(), font);
|
2001-08-03 09:54:48 +00:00
|
|
|
|
|
|
|
|
|
// formula
|
2003-05-30 06:48:24 +00:00
|
|
|
|
par()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);
|
2002-04-03 13:53:46 +00:00
|
|
|
|
xo_ = x;
|
2001-10-24 09:16:06 +00:00
|
|
|
|
yo_ = y;
|
2001-08-03 09:54:48 +00:00
|
|
|
|
}
|