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-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
|
|
|
|
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-02-26 12:53:35 +00:00
|
|
|
opened_ = false;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 12:53:35 +00:00
|
|
|
InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
|
|
|
|
: InsetFormula(true), name_(nm)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-02-26 12:53:35 +00:00
|
|
|
tmacro_ = MathMacroTable::mathMTable.getTemplate(name_);
|
2001-03-01 14:07:43 +00:00
|
|
|
if (!tmacro_.get()) {
|
|
|
|
tmacro_.reset(new MathMacroTemplate(name_, na));
|
2001-02-26 12:53:35 +00:00
|
|
|
MathMacroTable::mathMTable.addTemplate(tmacro_);
|
2001-02-15 12:22:01 +00:00
|
|
|
}
|
2001-02-26 12:53:35 +00:00
|
|
|
opened_ = false;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetFormulaMacro::~InsetFormulaMacro()
|
|
|
|
{
|
2001-03-01 14:07:43 +00:00
|
|
|
// We do not want the InsetFormula destructor to
|
|
|
|
// delete this. That is taken care of elsewhere (Lgb)
|
2001-02-15 12:22:01 +00:00
|
|
|
par = 0;
|
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-03-01 14:07:43 +00:00
|
|
|
// This should really use a proper copy constructor
|
|
|
|
return new InsetFormulaMacro(name_, 0);
|
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-02-26 12:53:35 +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-02-26 12:53:35 +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-02-20 17:45:56 +00:00
|
|
|
MathedArray ar;
|
2001-02-26 12:53:35 +00:00
|
|
|
|
|
|
|
mathed_parse(ar, 0, reinterpret_cast<MathParInset **>(&tmacro_));
|
2001-03-01 14:07:43 +00:00
|
|
|
// Since tmacro_ == 0 when mathed_parse is called we need to set
|
2001-02-28 17:21:16 +00:00
|
|
|
// its contents explicitly afterwards (Lgb)
|
2001-02-26 12:53:35 +00:00
|
|
|
tmacro_->setData(ar);
|
|
|
|
|
2000-01-06 02:44:26 +00:00
|
|
|
// Update line number
|
|
|
|
lex.setLineNo(mathed_parser_lineno());
|
|
|
|
|
2001-02-26 12:53:35 +00:00
|
|
|
MathMacroTable::mathMTable.addTemplate(tmacro_);
|
|
|
|
name_ = tmacro_->GetName();
|
2001-03-01 14:07:43 +00:00
|
|
|
par = tmacro_.get();
|
2001-02-26 12:53:35 +00:00
|
|
|
|
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-02-26 12:53:35 +00:00
|
|
|
if (opened_) {
|
2001-02-15 12:22:01 +00:00
|
|
|
return InsetFormula::ascent(pain, f);
|
|
|
|
}
|
|
|
|
return lyxfont::maxAscent(f) + 3;
|
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-02-26 12:53:35 +00:00
|
|
|
if (opened_) {
|
2001-02-15 12:22:01 +00:00
|
|
|
return InsetFormula::descent(pain, f);
|
|
|
|
}
|
|
|
|
return lyxfont::maxDescent(f) + 1;
|
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::width(BufferView * bv, LyXFont const & f) const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-02-26 12:53:35 +00:00
|
|
|
if (opened_) {
|
2001-02-15 12:22:01 +00:00
|
|
|
return InsetFormula::width(bv, f);
|
|
|
|
}
|
|
|
|
string ilabel(_("Macro: "));
|
2001-02-26 12:53:35 +00:00
|
|
|
ilabel += name_;
|
2001-02-15 12:22:01 +00:00
|
|
|
return 6 + lyxfont::width(ilabel, 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-02-26 12:53:35 +00:00
|
|
|
if (opened_) {
|
|
|
|
tmacro_->setEditMode(true);
|
2000-06-23 15:02:46 +00:00
|
|
|
InsetFormula::draw(bv, font, baseline, x, cleared);
|
2001-02-26 12:53:35 +00:00
|
|
|
tmacro_->setEditMode(false);
|
2000-02-10 17:53:36 +00:00
|
|
|
} else {
|
|
|
|
font.setColor(LColor::math);
|
|
|
|
|
2001-02-15 12:22:01 +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);
|
2000-02-10 17:53:36 +00:00
|
|
|
|
|
|
|
pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
|
|
|
|
pain.rectangle(int(x), y, w, h, LColor::mathframe);
|
|
|
|
|
|
|
|
string s(_("Macro: "));
|
2001-02-26 12:53:35 +00:00
|
|
|
s += name_;
|
2000-02-10 17:53:36 +00:00
|
|
|
pain.text(int(x + 2), baseline, s, font);
|
2000-07-05 14:57:48 +00:00
|
|
|
x += width(bv, font) - 1;
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
string const InsetFormulaMacro::EditMessage() const
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
return _("Math macro editor mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-25 12:06:15 +00:00
|
|
|
void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-02-26 12:53:35 +00:00
|
|
|
opened_ = true;
|
|
|
|
par = static_cast<MathParInset*>(tmacro_->Clone());
|
2001-02-15 12:22:01 +00:00
|
|
|
InsetFormula::Edit(bv, x, y, button);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-22 00:36:17 +00:00
|
|
|
void InsetFormulaMacro::InsetUnlock(BufferView * bv)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-02-26 12:53:35 +00:00
|
|
|
opened_ = false;
|
|
|
|
tmacro_->setData(par->GetData());
|
|
|
|
tmacro_->setEditMode(false);
|
2001-02-15 12:22:01 +00:00
|
|
|
InsetFormula::InsetUnlock(bv);
|
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-02-26 12:53:35 +00:00
|
|
|
int const i = lyx::atoi(arg) - 1;
|
|
|
|
if (i >= 0 && i < tmacro_->getNoArgs()) {
|
|
|
|
mathcursor->insertInset(tmacro_->getMacroPar(i),
|
2001-02-17 18:52:53 +00:00
|
|
|
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-02-26 12:53:35 +00:00
|
|
|
tmacro_->setEditMode(true);
|
|
|
|
tmacro_->Metrics();
|
2001-02-15 12:22:01 +00:00
|
|
|
RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
|
2001-02-26 12:53:35 +00:00
|
|
|
tmacro_->setEditMode(false);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-02-15 12:22:01 +00:00
|
|
|
return result;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|