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 front, EntryDirection entry_from);
43 bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
45 void read(Lexer & lex);
47 void write(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(odocstream &, OutputParams const &) const;
56 bool noFontChange() const { return true; }
59 docstring name() const;
61 void getDefaults(std::vector<docstring> & defaults) const;
63 docstring definition() const;
65 docstring displayDefinition() const;
67 size_t numArgs() const;
69 size_t numOptionals() const;
71 bool redefinition() const { return redefinition_; }
73 MacroType type() const { return type_; }
75 /// check name and possible other formal properties
76 bool validMacro() const;
78 bool validName() const;
79 /// Remove everything from the name which makes it invalid
80 /// and return true iff it is valid.
81 bool fixNameAndCheckIfValid();
83 /// request "external features"
84 virtual void validate(LaTeXFeatures &) const;
86 /// decide whether its a redefinition
87 void updateToContext(MacroContext const & mc) const;
90 void draw(PainterInfo & pi, int x, int y) const;
92 void metrics(MetricsInfo & mi, Dimension & dim) const;
93 /// identifies macro templates
94 MathMacroTemplate * asMacroTemplate() { return this; }
95 /// identifies macro templates
96 MathMacroTemplate const * asMacroTemplate() const { return this; }
98 InsetCode lyxCode() const { return MATHMACRO_CODE; }
100 void infoize(odocstream & os) const;
102 docstring contextMenu(BufferView const &, int, int) const;
106 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
107 /// do we want to handle this event?
108 bool getStatus(Cursor & cur, FuncRequest const & cmd,
109 FuncStatus & status) const;
112 friend class InsetLabelBox;
113 friend class DisplayLabelBox;
116 virtual Inset * clone() const;
118 /// remove #n with from<=n<=to
119 void removeArguments(Cursor & cur, int from, int to);
120 /// shift every #n with from<=n, i.e. #n -> #(n-by)
121 void shiftArguments(size_t from, int by);
123 void insertParameter(Cursor & cur, int pos, bool greedy = false, bool addarg = true);
125 void removeParameter(Cursor & cur, int pos, bool greedy = false);
127 void makeOptional(Cursor & cur);
129 void makeNonOptional(Cursor & cur);
131 idx_type defIdx() const { return optionals_ + 1; }
132 /// index of default value cell of optional parameter (#1 -> n=0)
133 idx_type optIdx(idx_type n) const { return n + 1; }
135 idx_type displayIdx() const { return optionals_ + 2; }
137 void updateLook() const;
138 /// look through the macro for #n arguments
139 int maxArgumentInDefinition() const;
140 /// add missing #n arguments up to \c maxArg
141 void insertMissingArguments(int maxArg);
143 void changeArity(Cursor & cur, int newNumArg);
144 /// find arguments in definition and adapt the arity accordingly
145 void commitEditChanges(Cursor & cur);
146 /// The representation of the macro template, with some holes to edit
147 mutable MathData look_;
149 mutable int numargs_;
151 mutable int argsInLook_;
154 /// keeps the old optional default value when an
155 /// optional argument is disabled
156 std::vector<MathData> optionalValues_;
158 /// (re)newcommand or def
159 mutable MacroType type_;
160 /// defined before already?
161 mutable bool redefinition_;
163 void createLook(int args) const;
165 mutable bool lookOutdated_;
166 /// true if in pre-calculations of metrics to get height of boxes
167 mutable bool premetrics_;
169 mutable int labelBoxAscent_;
171 mutable int labelBoxDescent_;
173 bool premetrics() const { return premetrics_; }
175 int commonLabelBoxAscent() const { return labelBoxAscent_; }
177 int commonLabelBoxDescent() const { return labelBoxDescent_; }