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