3 * \file math_macrotemplate.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Alejandro Aguilar Sierra
10 * Full author contact details are available in file CREDITS.
13 #ifndef MATH_MACROTEMPLATE_H
14 #define MATH_MACROTEMPLATE_H
16 #include "InsetMathNest.h"
17 #include "MacroTable.h"
20 #include "support/types.h"
25 /// This class contains the macro definition.
26 class MathMacroTemplate : public InsetMathNest {
31 MathMacroTemplate(docstring const & name, int nargs, int optional,
33 std::vector<MathData> const & optionalValues = std::vector<MathData>(),
34 MathData const & def = MathData(),
35 MathData const & display = MathData());
37 explicit MathMacroTemplate(const docstring & str);
39 EDITABLE editable() const { return HIGHLY_EDITABLE; }
41 void read(Buffer const &, Lexer & lex);
43 void write(Buffer const &, std::ostream & os) const;
45 void write(WriteStream & os) const;
46 /// Output LaTeX code, but assume that the macro is not definied yet
47 /// if overwriteRedefinition is true
48 void write(WriteStream & os, bool overwriteRedefinition) const;
50 int plaintext(Buffer const &, odocstream &,
51 OutputParams const &) const;
53 bool noFontChange() const { return true; }
56 docstring name() const;
58 void getDefaults(std::vector<docstring> & defaults) const;
60 docstring definition() const;
62 docstring displayDefinition() const;
64 size_t numArgs() const;
66 size_t numOptionals() const;
68 bool redefinition() const { return redefinition_; }
70 MacroType type() const { return type_; }
72 /// check name and possible other formal properties
73 bool validMacro() const;
75 bool validName() const;
76 /// Remove everything from the name which makes it invalid
77 /// and return true iff it is valid.
78 bool fixNameAndCheckIfValid();
80 /// decide whether its a redefinition
81 void updateToContext(MacroContext const & mc) const;
84 void draw(PainterInfo & pi, int x, int y) const;
86 void metrics(MetricsInfo & mi, Dimension & dim) const;
87 /// identifies macro templates
88 MathMacroTemplate * asMacroTemplate() { return this; }
89 /// identifies macro templates
90 MathMacroTemplate const * asMacroTemplate() const { return this; }
92 InsetCode lyxCode() const { return MATHMACRO_CODE; }
96 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
97 /// do we want to handle this event?
98 bool getStatus(Cursor & cur, FuncRequest const & cmd,
99 FuncStatus & status) const;
102 virtual Inset * clone() const;
104 /// remove #n with from<=n<=to
105 void removeArguments(Cursor & cur, int from, int to);
106 /// shift every #n with from<=n, i.e. #n -> #(n-by)
107 void shiftArguments(size_t from, int by);
109 void insertParameter(Cursor & cur, int pos, bool greedy = false);
111 void removeParameter(Cursor & cur, int pos, bool greedy = false );
113 void makeOptional(Cursor & cur);
115 void makeNonOptional(Cursor & cur);
117 idx_type defIdx() const { return optionals_ + 1; }
118 /// index of default value cell of optional parameter (#1 -> n=0)
119 idx_type optIdx(idx_type n) const { return n + 1; }
121 idx_type displayIdx() const { return optionals_ + 2; }
122 /// The label with some holes to edit
123 mutable MathData label_;
125 mutable int numargs_;
128 /// keeps the old optional default value when an
129 /// optional argument is disabled
130 std::vector<MathData> optionalValues_;
132 /// (re)newcommand or def
133 mutable MacroType type_;
134 /// defined before already?
135 mutable bool redefinition_;