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 edit(Cursor & cur, bool left);
43 bool notifyCursorLeaves(Cursor & cur);
45 void read(Buffer const &, Lexer & lex);
47 void write(Buffer const &, std::ostream & os) const;
49 void write(WriteStream & os) const;
50 /// Output LaTeX code, but assume that the macro is not definied yet
51 /// if overwriteRedefinition is true
52 void write(WriteStream & os, bool overwriteRedefinition) const;
54 int plaintext(Buffer const &, odocstream &,
55 OutputParams const &) const;
57 bool noFontChange() const { return true; }
60 docstring name() const;
62 void getDefaults(std::vector<docstring> & defaults) const;
64 docstring definition() const;
66 docstring displayDefinition() const;
68 size_t numArgs() const;
70 size_t numOptionals() const;
72 bool redefinition() const { return redefinition_; }
74 MacroType type() const { return type_; }
76 /// check name and possible other formal properties
77 bool validMacro() const;
79 bool validName() const;
80 /// Remove everything from the name which makes it invalid
81 /// and return true iff it is valid.
82 bool fixNameAndCheckIfValid();
84 /// decide whether its a redefinition
85 void updateToContext(MacroContext const & mc) const;
88 void draw(PainterInfo & pi, int x, int y) const;
90 void metrics(MetricsInfo & mi, Dimension & dim) const;
91 /// identifies macro templates
92 MathMacroTemplate * asMacroTemplate() { return this; }
93 /// identifies macro templates
94 MathMacroTemplate const * asMacroTemplate() const { return this; }
96 InsetCode lyxCode() const { return MATHMACRO_CODE; }
98 void infoize(odocstream & os) const;
102 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
103 /// do we want to handle this event?
104 bool getStatus(Cursor & cur, FuncRequest const & cmd,
105 FuncStatus & status) const;
108 virtual Inset * clone() const;
110 /// remove #n with from<=n<=to
111 void removeArguments(Cursor & cur, int from, int to);
112 /// shift every #n with from<=n, i.e. #n -> #(n-by)
113 void shiftArguments(size_t from, int by);
115 void insertParameter(Cursor & cur, int pos, bool greedy = false);
117 void removeParameter(Cursor & cur, int pos, bool greedy = false );
119 void makeOptional(Cursor & cur);
121 void makeNonOptional(Cursor & cur);
123 idx_type defIdx() const { return optionals_ + 1; }
124 /// index of default value cell of optional parameter (#1 -> n=0)
125 idx_type optIdx(idx_type n) const { return n + 1; }
127 idx_type displayIdx() const { return optionals_ + 2; }
128 /// The label with some holes to edit
129 mutable MathData label_;
131 mutable int numargs_;
134 /// keeps the old optional default value when an
135 /// optional argument is disabled
136 std::vector<MathData> optionalValues_;
138 /// (re)newcommand or def
139 mutable MacroType type_;
140 /// defined before already?
141 mutable bool redefinition_;
143 mutable Dimension cellDim_;