]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/formulamacro.C
mathed95.diff
[lyx.git] / src / mathed / formulamacro.C
index 6f7e06bd8b3910d59d42a8b8369a2043561ff764..d0de1d9ee8bf96a0d408a77769e974b54ff96a9f 100644 (file)
@@ -1,23 +1,20 @@
 /*
- *  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. 
+ *  File:        formulamacro.C
+ *  Purpose:     Implementation of the formula macro LyX inset
+ *  Author:      André Pönitz
+ *  Created:     March 2001
+ *  Description: Allows the edition of math macros inside Lyx. 
  *
- *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
- *
- *  Version: 0.4, Lyx project.
+ *  Copyright: 2001  The 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>
-#include <cstdlib>
 
 #ifdef __GNUG__
-#pragma implementation "formulamacro.h"
+#pragma implementation
 #endif
 
 #include "formulamacro.h"
 #include "math_cursor.h"
 #include "math_parser.h"
 #include "math_macro.h"
+#include "math_macroarg.h"
+#include "math_macrotable.h"
+#include "math_macrotemplate.h"
+#include "math_matrixinset.h"
 #include "lyx_main.h"
 #include "BufferView.h"
 #include "gettext.h"
 #include "Painter.h"
 #include "font.h"
+#include "support/lyxlib.h"
+#include "mathed/support.h"
+#include "support/LOstream.h"
+#include "debug.h"
+#include "lyxlex.h"
 
 using std::ostream;
-using std::istream;
+
+extern MathCursor * mathcursor;
 
 InsetFormulaMacro::InsetFormulaMacro()
-       : InsetFormula(true)
+       : InsetFormulaBase(new MathMacroTemplate("unknown", 0))
+{}
+
+
+InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
+       : InsetFormulaBase(new MathMacroTemplate(nm, na))
 {
-    tmacro = 0;
-    opened = false;
+       MathMacroTable::insertTemplate(tmacro());
 }
 
 
-InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
-        : InsetFormula(true), name(nm)
+Inset * InsetFormulaMacro::clone(Buffer const &) const
 {
-    tmacro = MathMacroTable::mathMTable.getTemplate(name.c_str());
-    if (!tmacro) {
-       tmacro = new MathMacroTemplate(name.c_str(), na);
-       MathMacroTable::mathMTable.addTemplate(tmacro);
-    }
-    opened = false;
+       return new InsetFormulaMacro(*this);
 }
 
 
-InsetFormulaMacro::~InsetFormulaMacro()
+void InsetFormulaMacro::write(ostream & os) const
 {
-    par = 0;
+       os << "FormulaMacro ";
+       tmacro()->Write(os, false);
 }
 
 
-Inset * InsetFormulaMacro::Clone() const
+int InsetFormulaMacro::latex(ostream & os, bool fragile, 
+                            bool /*free_spacing*/) const
 {
-   return new InsetFormulaMacro(name);
+       tmacro()->Write(os, fragile);
+       return 2;
 }
 
-
-void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
+int InsetFormulaMacro::ascii(ostream & os, int) const
 {
-       os << "FormulaMacro ";
-       tmacro->WriteDef(os, false);
+       tmacro()->Write(os, false);
+       return 0;
 }
 
 
-int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/, 
-                            bool /*free_spacing*/) const
+int InsetFormulaMacro::linuxdoc(ostream & os) const
 {
-       tmacro->WriteDef(os, true); // or false?
-       return 1;
+       return ascii(os, 0);
 }
 
 
-int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
+int InsetFormulaMacro::docBook(ostream & os) const
 {
-    return Ascii(buf, os);
+       return ascii(os, 0);
 }
 
 
-int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
+void InsetFormulaMacro::read(LyXLex & lex)
 {
-    return Ascii(buf, os);
+       // Awful hack...
+       par_ = mathed_parse(lex);
+       MathMacroTable::insertTemplate(tmacro());
+       par_->Metrics(LM_ST_TEXT);
 }
 
 
-void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
+string InsetFormulaMacro::prefix() const
 {
-       istream & is = lex.getStream();
-       mathed_parser_file(is, lex.GetLineNo());   
-       mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
-    
-       // Update line number
-       lex.setLineNo(mathed_parser_lineno());
-       
-       MathMacroTable::mathMTable.addTemplate(tmacro);
-       name = tmacro->GetName();
-       par = tmacro;
-       // reading of end_inset in the inset!!!
-       while (lex.IsOK()) {
-               lex.nextToken();
-               if (lex.GetString() == "\\end_inset")
-                       break;
-       }
+       return string(" ") + _("Macro: ") + tmacro()->name() + ": ";
 }
 
 
-int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
+int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
 {
-    if (opened) {
-       tmacro->update();
-       return InsetFormula::ascent(pain, f);
-    }
-    return lyxfont::maxAscent(f) + 3;
+       return tmacro()->ascent() + 5;
 }
 
 
-int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
+int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
 {
-    if (opened) {
-       tmacro->update();
-       return InsetFormula::descent(pain, f);
-    }
-    return lyxfont::maxDescent(f) + 1;
+       return tmacro()->descent() + 5;
 }
 
 
-int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
+int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
 {
-    if (opened) {
-       tmacro->update();
-       return InsetFormula::width(pain, f);
-    }
-    string ilabel(_("Macro: "));
-    ilabel += name;
-    return 6 + lyxfont::width(ilabel, f);
+       tmacro()->Metrics(LM_ST_TEXT);
+       return 10 + lyxfont::width(prefix(), f) + tmacro()->width();
 }
 
 
 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
-                            int baseline, float & x) const
+                            int baseline, float & x, bool /*cleared*/) const
 {
        Painter & pain = bv->painter();
        LyXFont font(f);
-       tmacro->update();
-       if (opened) {
-               tmacro->setEditMode(true);
-               InsetFormula::draw(bv, 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);
 
+       // label
+       font.setColor(LColor::math);
        
-               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;
+       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->Selection()) {
+               int xp[10];
+               int yp[10];
+               int n;
+               mathcursor->SelGetArea(xp, yp, n);
+               pain.fillPolygon(xp, yp, n, LColor::selection);
        }
-}
 
+       pain.text(int(x + 2), baseline, prefix(), font);
+       x += width(bv, font);
 
-char const * InsetFormulaMacro::EditMessage() const 
-{
-       return _("Math macro editor mode");
+       // formula
+       float t = tmacro()->width() + 5;
+       x -= t;
+       tmacro()->draw(pain, int(x), baseline);
+       x += t;
 }
 
 
-void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
-{
-    opened = true;
-    par = static_cast<MathParInset*>(tmacro->Clone());
-    InsetFormula::Edit(bv, x, y, button);
+UpdatableInset::RESULT
+InsetFormulaMacro::localDispatch(BufferView * bv,
+                                kb_action action, string const & arg)
+{
+       RESULT result = DISPATCHED;
+       switch (action) {
+               case LFUN_MATH_MACROARG: {
+                       int const i = lyx::atoi(arg);
+                       lyxerr << "inserting macro arg " << i << "\n";
+                       if (i > 0 && i <= tmacro()->numargs()) {
+                               mathcursor->insert(new MathMacroArgument(i));
+                               updateLocal(bv);
+                       } else {
+                               lyxerr << "not in range 0.." << tmacro()->numargs() << "\n";
+                       }
+                       break;
+               }
+               
+               default:
+                       result = InsetFormulaBase::localDispatch(bv, action, arg);
+       }
+       return result;
 }
 
-              
-void InsetFormulaMacro::InsetUnlock(BufferView * bv)
+
+MathMacroTemplate * InsetFormulaMacro::tmacro() const
 {
-    opened = false;
-    LyxArrayBase * tarray = tmacro->GetData();
-    MathedIter it(tarray);
-    it.Clear();
-    tmacro->SetData(par->GetData());
-    tmacro->setEditMode(false);
-    InsetFormula::InsetUnlock(bv);
+       return static_cast<MathMacroTemplate *>(par_);
 }
 
 
-UpdatableInset::RESULT
-InsetFormulaMacro::LocalDispatch(BufferView * bv,
-                                int action, string const & arg)
-{
-    if (action == LFUN_MATH_MACROARG) {
-       int i = atoi(arg.c_str()) - 1;
-       if (i >= 0 && i < tmacro->getNoArgs()) {
-           mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
-           InsetFormula::UpdateLocal(bv);
-       }
-       
-       return DISPATCHED;
-    }
-    tmacro->setEditMode(true);
-    tmacro->Metrics();
-    RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
-    tmacro->setEditMode(false);
-    
-    return result;
+Inset::Code InsetFormulaMacro::lyxCode() const
+{
+       return Inset::MATHMACRO_CODE;
 }
+