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"
26 /// This class contains the macro definition.
27 class MathMacroTemplate : public InsetMathNest {
30 MathMacroTemplate(Buffer * buf);
32 MathMacroTemplate(Buffer * buf, docstring const & name, int nargs,
33 int optional, MacroType type,
34 std::vector<MathData> const & optionalValues = std::vector<MathData>(),
35 MathData const & def = MathData(),
36 MathData const & display = MathData());
38 MathMacroTemplate(Buffer * buf, const docstring & str);
40 bool editable() const { return true; }
42 void edit(Cursor & cur, bool front, EntryDirection entry_from);
44 bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
46 void read(Lexer & lex);
48 void write(std::ostream & os) const;
50 void write(WriteStream & os) const;
51 /// Output LaTeX code, but assume that the macro is not definied yet
52 /// if overwriteRedefinition is true
53 int write(WriteStream & os, bool overwriteRedefinition) const;
54 /// Nothing happens. This is simply to suppress the default output.
55 docstring xhtml(XHTMLStream &, OutputParams const &) const;
57 int plaintext(odocstringstream &, OutputParams const &, size_t) const;
59 bool inheritFont() const { return false; }
62 docstring name() const;
64 void getDefaults(std::vector<docstring> & defaults) const;
66 docstring definition() const;
68 docstring displayDefinition() const;
70 size_t numArgs() const;
72 size_t numOptionals() const;
74 bool redefinition() const { return redefinition_; }
76 MacroType type() const { return type_; }
78 /// check name and possible other formal properties
79 bool validMacro() const;
81 bool validName() const;
82 /// Remove everything from the name which makes it invalid
83 /// and return true iff it is valid.
84 bool fixNameAndCheckIfValid();
86 /// request "external features"
87 virtual void validate(LaTeXFeatures &) const;
89 /// decide whether its a redefinition
90 void updateToContext(MacroContext const & mc) const;
93 void draw(PainterInfo & pi, int x, int y) const;
95 void metrics(MetricsInfo & mi, Dimension & dim) const;
96 /// identifies macro templates
97 MathMacroTemplate * asMacroTemplate() { return this; }
98 /// identifies macro templates
99 MathMacroTemplate const * asMacroTemplate() const { return this; }
101 InsetCode lyxCode() const { return MATHMACRO_CODE; }
103 void infoize(odocstream & os) const;
105 std::string contextMenuName() const;
108 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
109 /// do we want to handle this event?
110 bool getStatus(Cursor & cur, FuncRequest const & cmd,
111 FuncStatus & status) const;
114 friend class InsetLabelBox;
115 friend class DisplayLabelBox;
118 virtual Inset * clone() const;
120 /// remove #n with from<=n<=to
121 void removeArguments(Cursor & cur, DocIterator const & inset_pos,
123 /// shift every #n with from<=n, i.e. #n -> #(n-by)
124 void shiftArguments(size_t from, int by);
126 void insertParameter(Cursor & cur, DocIterator const & inset_pos,
127 int pos, bool greedy = false, bool addarg = true);
129 void removeParameter(Cursor & cur, DocIterator const & inset_pos,
130 int pos, bool greedy = false);
132 void makeOptional(Cursor & cur, DocIterator const & inset_pos);
134 void makeNonOptional(Cursor & cur, DocIterator const & inset_pos);
136 idx_type defIdx() const { return optionals_ + 1; }
137 /// index of default value cell of optional parameter (#1 -> n=0)
138 idx_type optIdx(idx_type n) const { return n + 1; }
140 idx_type displayIdx() const { return optionals_ + 2; }
142 void updateLook() const;
143 /// look through the macro for #n arguments
144 int maxArgumentInDefinition() const;
145 /// add missing #n arguments up to \c maxArg
146 void insertMissingArguments(int maxArg);
148 void changeArity(Cursor & cur, DocIterator const & inset_pos,
150 /// find arguments in definition and adapt the arity accordingly
151 void commitEditChanges(Cursor & cur, DocIterator const & inset_pos);
152 /// The representation of the macro template, with some holes to edit
153 mutable MathData look_;
155 mutable int numargs_;
157 mutable int argsInLook_;
160 /// keeps the old optional default value when an
161 /// optional argument is disabled
162 std::vector<MathData> optionalValues_;
164 /// (re)newcommand or def
165 mutable MacroType type_;
166 /// defined before already?
167 mutable bool redefinition_;
169 void createLook(int args) const;
171 mutable bool lookOutdated_;
172 /// true if in pre-calculations of metrics to get height of boxes
173 mutable bool premetrics_;
175 mutable int labelBoxAscent_;
177 mutable int labelBoxDescent_;
179 bool premetrics() const { return premetrics_; }
181 int commonLabelBoxAscent() const { return labelBoxAscent_; }
183 int commonLabelBoxDescent() const { return labelBoxDescent_; }