]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macro.h
Andreas' patch to prevent crash on click on previewd inset
[lyx.git] / src / mathed / math_macro.h
index 6269857f4c468e9ec6cb61769f81fbd9f4946963..2a3637578f4bf3f26b8b86ea8080d0de97065441 100644 (file)
 // -*- C++ -*-
-/*
- *  File:        math_macro.h
- *  Purpose:     Declaration of macro class for mathed 
- *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
- *  Created:     November 1996
- *  Description: WYSIWYG math macros
+/**
+ * \file math_macro.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *  Dependencies: Mathed
+ * \author Alejandro Aguilar Sierra
+ * \author André Pönitz
  *
- *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
- *
- *   Version: 0.2, Mathed & Lyx project.
- *
- *   This code is under the GNU General Public Licence version 2 or later.
+ * Full author contact details are available in file CREDITS.
  */
-#ifndef MATH_MACRO
-#define MATH_MACRO
-
-#ifdef __GNUG__
-#pragma interface
-#endif
-
-#include "math_defs.h"
-#include "debug.h"
 
-///
-typedef MathParInset * MathParInsetP;
-///
-typedef MathedArray * MathedArrayP;
+#ifndef MATH_MACRO_H
+#define MATH_MACRO_H
 
-class MathMacroTemplate;
+#include "math_nestinset.h"
+#include "math_data.h"
+#include "math_nestinset.h"
+#include "math_macrotable.h"
 
 
-/// This class contains the data for a macro
-class MathMacro : public MathParInset
-{
+/// This class contains the data for a macro.
+class MathMacro : public MathNestInset {
 public:
-    /// A macro can only be builded from an existing template
-    explicit
-    MathMacro(MathMacroTemplate *);
-    /// or from another macro.
-    explicit
-    MathMacro(MathMacro *);
-    ///
-    ~MathMacro();
-    ///
-    void draw(Painter &, int, int);
-    ///
-    void Metrics();
-    ///
-    MathedInset * Clone();
-    ///
-    void Write(std::ostream &, bool fragile);
-    ///
-    bool setArgumentIdx(int);
-    ///
-    int getArgumentIdx() const;
-    ///
-    int getMaxArgumentIdx() const;
-    ///
-    int GetColumns() const;
-    ///
-    void GetXY(int &, int &) const;
-    ///
-    void SetFocus(int, int);
-    ///
-    MathedArray * GetData();
-    ///
-    MathedRowSt * getRowSt() const { return args_[idx].row; }
-    ///
-    void SetData(MathedArray *);
-    ///
-    MathedTextCodes getTCode() const { return tcode; }
-    ///
-    bool Permit(short) const;
-    
-private:
-    ///
-    MathMacroTemplate * tmplate;
-    ///
-    struct MacroArgumentBase {
-       /// Position of the macro
-       int x, y;
+       /// A macro can be built from an existing template
+       MathMacro(std::string const & name, int numargs);
        ///
-       MathedRowSt * row;
+       void draw(PainterInfo & pi, int x, int y) const;
        ///
-       MathedArray * array;
+       void drawExpanded(PainterInfo & pi, int x, int y) const;
        ///
-       MacroArgumentBase() { x = y = 0;  array = 0; row = 0; }
-    };
-    std::vector<MacroArgumentBase> args_;
-    ///
-    int idx;
-    ///
-    int nargs;
-    ///
-    MathedTextCodes tcode;
-    ///
-    friend class MathMacroTemplate;
-};
-
-
-/// An argument
-class MathMacroArgument: public MathParInset {
-public:
-    ///
-    MathMacroArgument() {
-           expnd_mode = false;
-           number = 1;
-           SetType(LM_OT_MACRO_ARG);
-    }
-    ///
-    explicit
-    MathMacroArgument(int);
-    ///
-    ~MathMacroArgument() { lyxerr << "help, destroyme!" << std::endl; }
-    ///
-    MathedInset * Clone() { return this; }
-    ///
-    void Metrics();
-    ///
-    void draw(Painter &, int x, int baseline);
-    ///
-    void Write(std::ostream &, bool fragile);
-    ///
-    void setNumber(int n) { number = n; }
-    /// Is expanded or not
-    void setExpand(bool e) { expnd_mode = e; }
-    /// Is expanded or not
-    bool getExpand() const { return expnd_mode; }
-private:
-    ///
-    bool expnd_mode;
-    ///
-    int number;
-};
-
-
-/// This class contains the macro definition
-class MathMacroTemplate: public MathParInset {
-public:
-    /// A template constructor needs all the data
-    explicit
-    MathMacroTemplate(string const &, int na = 0, int f = 0);
-    ///
-    ~MathMacroTemplate();
-    ///
-    void draw(Painter &, int, int);
-    ///
-    void Metrics();
-    ///
-    void WriteDef(std::ostream &, bool fragile);
-    /// useful for special insets
-    void  setTCode(MathedTextCodes t) { tcode = t; }
-    ///
-    MathedTextCodes getTCode() const { return tcode; }
-    /// 
-    void setArgument(MathedArray *, int i= 0);
-    /// Number of arguments
-    int getNoArgs() const { return nargs; }
-    ///
-    void GetMacroXY(int, int &, int &) const;
-    ///
-    MathParInset * getMacroPar(int) const;
-    ///
-    void SetMacroFocus(int &, int, int);
-    ///
-    void setEditMode(bool);
+       void metrics(MetricsInfo & mi, Dimension & dim) const;
+       ///
+       std::string name() const;
+       ///
+       void setExpansion(MathArray const & exp, MathArray const & args) const;
 
-    /// Replace the appropriate arguments with a specific macro's data
-    void update(MathMacro * m = 0);
-      
-private:
-    ///
-    short flags;
-    ///
-    MathedTextCodes tcode;
-    ///
-    std::vector<MathMacroArgument> args_;
-    ///
-    int nargs;
-    ///
-    friend class MathMacro;
-};
-     
+       ///
+       void validate(LaTeXFeatures &) const;
 
-///
-typedef MathMacro * MathMacroP;
-///
-typedef MathMacroTemplate * MathMacroTemplateP;
+       ///
+       void maple(MapleStream &) const;
+       ///
+       void mathmlize(MathMLStream &) const;
+       ///
+       void octave(OctaveStream &) const;
+       ///
+       void infoize(std::ostream &) const;
+       ///
+       void infoize2(std::ostream &) const;
 
-///
-class MathMacroTable {
-public:
-    ///
-    explicit
-    MathMacroTable(int);
-    ///
-    ~MathMacroTable();
-    ///
-    void addTemplate(MathMacroTemplate *);
-    ///
-    MathMacro * getMacro(string const &) const;
-    ///
-    MathMacroTemplate * getTemplate(string const &) const;
-    ///
-    void builtinMacros();
-    ///
-    static MathMacroTable mathMTable;
-    ///
-    static bool built;
-    
 private:
-    ///
-    const int max_macros;
-    ///
-    int num_macros;
-    ///
-    MathMacroTemplateP * macro_table;
+       virtual std::auto_ptr<InsetBase> doClone() const;
+       ///
+       void updateExpansion() const;
+       ///
+       void expand() const;
+
+       /// name of macro
+       std::string name_;
+       /// the unexpanded macro defintition
+       mutable MathArray tmpl_;
+       /// the matcro substituted with our args
+       mutable MathArray expanded_;
 };
 
 
-
-/*-----------------------  inlines  -------------------------*/
-
-inline
-bool MathMacro::setArgumentIdx(int i)
-{
-    if (i >= 0 && i < nargs) {
-       idx = i;
-       return true;
-    } else
-      return false;
-}
-
-inline
-int MathMacro::getArgumentIdx() const 
-{ 
-    return idx; 
-}
-
-inline
-int MathMacro::getMaxArgumentIdx() const 
-{ 
-    return nargs - 1; 
-} 
-
-
-inline
-MathedArray * MathMacro::GetData() 
-{ 
-    return args_[idx].array; 
-} 
-
-
-inline
-void MathMacro::SetData(MathedArray * a)
-{
-   args_[idx].array = a;
-}
-
-
-inline 
-MathMacro * MathMacroTable::getMacro(string const & name) const
-{
-       MathMacroTemplate * mt = getTemplate(name);
-       return (mt) ? new MathMacro(mt): 0;
-}
-
-
 #endif