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 InsetMathMacroTemplate : public InsetMathNest {
30 explicit InsetMathMacroTemplate(Buffer * buf);
32 InsetMathMacroTemplate(Buffer * buf, docstring const & name, int numargs,
33 int optionals, 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 override { return true; }
42 void edit(Cursor & cur, bool front, EntryDirection entry_from) override;
44 bool notifyCursorLeaves(Cursor const & old, Cursor & cur) override;
46 void read(Lexer & lex) override;
48 void write(std::ostream & os) const override;
50 void write(TeXMathStream & os) const override;
51 /// Output LaTeX code, but assume that the macro is not defined yet
52 /// if overwriteRedefinition is true
53 int write(TeXMathStream & os, bool overwriteRedefinition) const;
54 /// Nothing happens. This is simply to suppress the default output.
55 docstring xhtml(XMLStream &, OutputParams const &) const override;
57 int plaintext(odocstringstream &, OutputParams const &, size_t) const override;
59 bool inheritFont() const override { return false; }
61 docstring name() const override;
63 void getDefaults(std::vector<docstring> & defaults) const;
65 docstring definition() const;
67 docstring displayDefinition() const;
69 size_t numArgs() const;
71 size_t numOptionals() const;
73 bool redefinition() const { return redefinition_; }
75 MacroType type() const { return type_; }
77 /// check name and possible other formal properties
78 bool validMacro() const;
80 bool validName() const;
81 /// Remove everything from the name which makes it invalid
82 /// and return true iff it is valid.
83 bool fixNameAndCheckIfValid();
85 /// request "external features"
86 void validate(LaTeXFeatures &) const override;
88 /// decide whether its a redefinition
89 void updateToContext(MacroContext const & mc);
92 void draw(PainterInfo & pi, int x, int y) const override;
94 void metrics(MetricsInfo & mi, Dimension & dim) const override;
95 /// identifies macro templates
96 InsetMathMacroTemplate * asMacroTemplate() override { return this; }
97 /// identifies macro templates
98 InsetMathMacroTemplate const * asMacroTemplate() const override { return this; }
100 InsetCode lyxCode() const override { return MATHMACRO_CODE; }
102 void infoize(odocstream & os) const override;
104 std::string contextMenuName() const override;
106 void addToToc(DocIterator const & di, bool output_active,
107 UpdateType utype, TocBackend & backend) const override;
110 void doDispatch(Cursor & cur, FuncRequest & cmd) override;
111 /// do we want to handle this event?
112 bool getStatus(Cursor & cur, FuncRequest const & cmd,
113 FuncStatus & status) const override;
116 friend class InsetLabelBox;
117 friend class InsetDisplayLabelBox;
120 Inset * clone() const override;
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?
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_; }