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.
|
|
|
|
*
|
|
|
|
* Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra
|
|
|
|
*
|
|
|
|
* 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 "bufferlist.h"
|
|
|
|
#include "lyx_cb.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "lyxscreen.h"
|
|
|
|
#include "lyxdraw.h"
|
|
|
|
#include "gettext.h"
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
void InsetFormulaMacro::Write(ostream & os)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
int InsetFormulaMacro::Latex(ostream & os, signed char /*fragile*/)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
tmacro->WriteDef(file);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
int InsetFormulaMacro::Linuxdoc(string &/*file*/)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
int InsetFormulaMacro::DocBook(string &/*file*/)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
void InsetFormulaMacro::Read(LyXLex & lex)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-07 00:44:53 +00:00
|
|
|
FILE * file = lex.getFile();
|
1999-09-27 18:44:28 +00:00
|
|
|
mathed_parser_file(file, lex.GetLineNo());
|
1999-12-13 00:05:34 +00:00
|
|
|
mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// Update line number
|
|
|
|
lex.setLineNo(mathed_parser_lineno());
|
|
|
|
|
|
|
|
MathMacroTable::mathMTable.addTemplate(tmacro);
|
|
|
|
name = tmacro->GetName();
|
|
|
|
par = tmacro;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
int InsetFormulaMacro::Ascent(LyXFont const & f) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
if (opened) {
|
|
|
|
tmacro->update();
|
|
|
|
return InsetFormula::Ascent(f);
|
|
|
|
}
|
|
|
|
return f.maxAscent()+3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
int InsetFormulaMacro::Descent(LyXFont const & f) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
if (opened) {
|
|
|
|
tmacro->update();
|
|
|
|
return InsetFormula::Descent(f);
|
|
|
|
}
|
|
|
|
return f.maxDescent()+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
int InsetFormulaMacro::Width(LyXFont const & f) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
if (opened) {
|
|
|
|
tmacro->update();
|
|
|
|
return InsetFormula::Width(f);
|
|
|
|
}
|
1999-10-02 16:21:10 +00:00
|
|
|
string ilabel(_("Macro: "));
|
1999-09-27 18:44:28 +00:00
|
|
|
ilabel += name;
|
|
|
|
return 6 + f.stringWidth(ilabel);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
void InsetFormulaMacro::Draw(LyXFont font, LyXScreen & scr,
|
|
|
|
int baseline, float & x)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
tmacro->update();
|
|
|
|
if (opened) {
|
|
|
|
tmacro->setEditMode(true);
|
|
|
|
InsetFormula::Draw(font, scr, baseline, x);
|
|
|
|
tmacro->setEditMode(false);
|
|
|
|
} else {
|
|
|
|
font.setColor(LyXFont::MATH);
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
int y = baseline - Ascent(font) + 1;
|
|
|
|
int w = Width(font) - 2, h = (Ascent(font) + Descent(font) - 2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
scr.fillRectangle(gc_lighted, int(x), y, w, h);
|
|
|
|
scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string s(_("Macro: "));
|
1999-09-27 18:44:28 +00:00
|
|
|
s += name;
|
|
|
|
scr.drawString(font, s, baseline, int(x +2));
|
|
|
|
x += Width(font) - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetFormulaMacro::Edit(int x, int y)
|
|
|
|
{
|
|
|
|
opened = true;
|
1999-11-23 14:39:02 +00:00
|
|
|
par = static_cast<MathParInset*>(tmacro->Clone());
|
1999-09-27 18:44:28 +00:00
|
|
|
InsetFormula::Edit(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetFormulaMacro::InsetUnlock()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
InsetFormula::InsetUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
bool InsetFormulaMacro::LocalDispatch(int action, char const * arg)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-11-15 11:06:41 +00:00
|
|
|
if (action == LFUN_MATH_MACROARG) {
|
1999-09-27 18:44:28 +00:00
|
|
|
int i = atoi(arg) - 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);
|
|
|
|
InsetFormula::UpdateLocal();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
tmacro->setEditMode(true);
|
|
|
|
tmacro->Metrics();
|
|
|
|
bool result = InsetFormula::LocalDispatch(action, arg);
|
|
|
|
tmacro->setEditMode(false);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|