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());
37 /// parses from string, returns false if failed
38 bool fromString (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);
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;
107 void addToToc(DocIterator const & di, bool output_active,
108 UpdateType utype) const;
111 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
112 /// do we want to handle this event?
113 bool getStatus(Cursor & cur, FuncRequest const & cmd,
114 FuncStatus & status) const;
117 friend class InsetLabelBox;
118 friend class DisplayLabelBox;
121 virtual Inset * clone() const;
123 /// remove #n with from<=n<=to
124 void removeArguments(Cursor & cur, DocIterator const & inset_pos,
126 /// shift every #n with from<=n, i.e. #n -> #(n-by)
127 void shiftArguments(size_t from, int by);
129 void insertParameter(Cursor & cur, DocIterator const & inset_pos,
130 int pos, bool greedy = false, bool addarg = true);
132 void removeParameter(Cursor & cur, DocIterator const & inset_pos,
133 int pos, bool greedy = false);
135 void makeOptional(Cursor & cur, DocIterator const & inset_pos);
137 void makeNonOptional(Cursor & cur, DocIterator const & inset_pos);
139 idx_type defIdx() const { return optionals_ + 1; }
140 /// index of default value cell of optional parameter (#1 -> n=0)
141 idx_type optIdx(idx_type n) const { return n + 1; }
143 idx_type displayIdx() const { return optionals_ + 2; }
145 void updateLook() const;
146 /// look through the macro for #n arguments
147 int maxArgumentInDefinition() const;
148 /// add missing #n arguments up to \c maxArg
149 void insertMissingArguments(int maxArg);
151 void changeArity(Cursor & cur, DocIterator const & inset_pos,
153 /// find arguments in definition and adapt the arity accordingly
154 void commitEditChanges(Cursor & cur, DocIterator const & inset_pos);
155 /// The representation of the macro template, with some holes to edit
156 mutable MathData look_;
158 mutable int numargs_;
160 mutable int argsInLook_;
163 /// keeps the old optional default value when an
164 /// optional argument is disabled
165 std::vector<MathData> optionalValues_;
167 /// (re)newcommand or def
168 mutable MacroType type_;
169 /// defined before already?
172 void createLook(int args) const;
174 mutable bool lookOutdated_;
175 /// true if in pre-calculations of metrics to get height of boxes
176 mutable bool premetrics_;
178 mutable int labelBoxAscent_;
180 mutable int labelBoxDescent_;
182 bool premetrics() const { return premetrics_; }
184 int commonLabelBoxAscent() const { return labelBoxAscent_; }
186 int commonLabelBoxDescent() const { return labelBoxDescent_; }