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>
|
1999-10-07 18:44:17 +00:00
|
|
|
#include <cstdlib>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation "formulamacro.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "formulamacro.h"
|
|
|
|
#include "commandtags.h"
|
|
|
|
#include "math_cursor.h"
|
|
|
|
#include "math_parser.h"
|
|
|
|
#include "math_macro.h"
|
|
|
|
#include "lyx_main.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "gettext.h"
|
2000-02-10 17:53:36 +00:00
|
|
|
#include "Painter.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
InsetFormulaMacro::InsetFormulaMacro()
|
|
|
|
: InsetFormula(true)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
tmacro = 0;
|
|
|
|
opened = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
|
|
|
|
: InsetFormula(true), name(nm)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
tmacro = MathMacroTable::mathMTable.getTemplate(name.c_str());
|
|
|
|
if (!tmacro) {
|
|
|
|
tmacro = new MathMacroTemplate(name.c_str(), na);
|
|
|
|
MathMacroTable::mathMTable.addTemplate(tmacro);
|
|
|
|
}
|
|
|
|
opened = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetFormulaMacro::~InsetFormulaMacro()
|
|
|
|
{
|
|
|
|
par = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
Inset * InsetFormulaMacro::Clone() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-11-22 16:19:48 +00:00
|
|
|
return new InsetFormulaMacro(name);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-18 22:22:42 +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 ";
|
|
|
|
Latex(os, 0);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-18 22:22:42 +00:00
|
|
|
int InsetFormulaMacro::Latex(ostream & os, signed char /*fragile*/) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
int ret = 1;
|
1999-12-07 00:44:53 +00:00
|
|
|
tmacro->WriteDef(os);
|
1999-09-27 18:44:28 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-02 02:19:43 +00:00
|
|
|
#ifndef USE_OSTREAM_ONLY
|
2000-02-18 22:22:42 +00:00
|
|
|
int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
tmacro->WriteDef(file);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-18 22:22:42 +00:00
|
|
|
int InsetFormulaMacro::Linuxdoc(string &/*file*/) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-18 22:22:42 +00:00
|
|
|
int InsetFormulaMacro::DocBook(string &/*file*/) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-03-06 02:42:40 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
int InsetFormulaMacro::Linuxdoc(ostream &) const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int InsetFormulaMacro::DocBook(ostream &) const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
void InsetFormulaMacro::Read(LyXLex & lex)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-01-06 02:44:26 +00:00
|
|
|
istream & is = lex.getStream();
|
|
|
|
mathed_parser_file(is, lex.GetLineNo());
|
|
|
|
mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-01-06 02:44:26 +00:00
|
|
|
// Update line number
|
|
|
|
lex.setLineNo(mathed_parser_lineno());
|
|
|
|
|
|
|
|
MathMacroTable::mathMTable.addTemplate(tmacro);
|
|
|
|
name = tmacro->GetName();
|
|
|
|
par = tmacro;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-10 17:53:36 +00:00
|
|
|
int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
|
|
|
|
{
|
|
|
|
if (opened) {
|
|
|
|
tmacro->update();
|
|
|
|
return InsetFormula::ascent(pain, f);
|
|
|
|
}
|
|
|
|
return f.maxAscent()+3;
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2000-02-10 17:53:36 +00:00
|
|
|
int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
|
|
|
|
{
|
|
|
|
if (opened) {
|
|
|
|
tmacro->update();
|
|
|
|
return InsetFormula::descent(pain, f);
|
|
|
|
}
|
|
|
|
return f.maxDescent()+1;
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2000-02-10 17:53:36 +00:00
|
|
|
int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
|
|
|
|
{
|
|
|
|
if (opened) {
|
|
|
|
tmacro->update();
|
|
|
|
return InsetFormula::width(pain, f);
|
|
|
|
}
|
|
|
|
string ilabel(_("Macro: "));
|
|
|
|
ilabel += name;
|
|
|
|
return 6 + f.stringWidth(ilabel);
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2000-02-10 17:53:36 +00:00
|
|
|
void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
|
2000-02-25 16:42:21 +00:00
|
|
|
int baseline, float & x) const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
|
|
|
LyXFont font(f);
|
|
|
|
tmacro->update();
|
|
|
|
if (opened) {
|
|
|
|
tmacro->setEditMode(true);
|
|
|
|
InsetFormula::draw(pain, font, baseline, x);
|
|
|
|
tmacro->setEditMode(false);
|
|
|
|
} else {
|
|
|
|
font.setColor(LColor::math);
|
|
|
|
|
|
|
|
int y = baseline - ascent(pain, font) + 1;
|
|
|
|
int w = width(pain, font) - 2;
|
|
|
|
int h = (ascent(pain, font) + descent(pain, font) - 2);
|
|
|
|
|
|
|
|
|
|
|
|
pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
|
|
|
|
pain.rectangle(int(x), y, w, h, LColor::mathframe);
|
|
|
|
|
|
|
|
string s(_("Macro: "));
|
|
|
|
s += name;
|
|
|
|
pain.text(int(x + 2), baseline, s, font);
|
|
|
|
x += width(pain, font) - 1;
|
|
|
|
}
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
opened = true;
|
1999-11-23 14:39:02 +00:00
|
|
|
par = static_cast<MathParInset*>(tmacro->Clone());
|
2000-02-25 12:06:15 +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
|
|
|
{
|
|
|
|
opened = false;
|
1999-11-23 14:39:02 +00:00
|
|
|
LyxArrayBase * tarray = tmacro->GetData();
|
1999-09-27 18:44:28 +00:00
|
|
|
MathedIter it(tarray);
|
|
|
|
it.Clear();
|
|
|
|
tmacro->SetData(par->GetData());
|
|
|
|
tmacro->setEditMode(false);
|
2000-02-22 00:36:17 +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,
|
|
|
|
int action, string const & arg)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-11-15 11:06:41 +00:00
|
|
|
if (action == LFUN_MATH_MACROARG) {
|
2000-02-25 12:06:15 +00:00
|
|
|
int i = atoi(arg.c_str()) - 1;
|
1999-11-23 14:39:02 +00:00
|
|
|
if (i >= 0 && i < tmacro->getNoArgs()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
|
2000-02-22 00:36:17 +00:00
|
|
|
InsetFormula::UpdateLocal(bv);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2000-02-25 12:06:15 +00:00
|
|
|
return DISPATCHED;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
tmacro->setEditMode(true);
|
|
|
|
tmacro->Metrics();
|
2000-02-25 12:06:15 +00:00
|
|
|
RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
|
1999-09-27 18:44:28 +00:00
|
|
|
tmacro->setEditMode(false);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|