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);
35 InsetMathMacroTemplate(Buffer * buf, docstring const & name, int numargs,
36 int optionals, MacroType type,
37 std::vector<MathData> const & optionalValues,
38 MathData const & def, MathData const & display);
39 /// parses from string, returns false if failed
40 bool fromString (const docstring & str);
42 bool editable() const override { return true; }
44 void edit(Cursor & cur, bool front, EntryDirection entry_from) override;
46 bool notifyCursorLeaves(Cursor const & old, Cursor & cur) override;
48 void read(support::Lexer & lex) override;
50 void write(std::ostream & os) const override;
52 void write(TeXMathStream & os) const override;
53 /// Output LaTeX code, but assume that the macro is not defined yet
54 /// if overwriteRedefinition is true
55 int write(TeXMathStream & os, bool overwriteRedefinition) const;
56 /// Nothing happens. This is simply to suppress the default output.
57 docstring xhtml(XMLStream &, OutputParams const &) const override;
59 int plaintext(odocstringstream &, OutputParams const &, size_t) const override;
61 bool inheritFont() const override { return false; }
63 docstring name() const override;
65 void getDefaults(std::vector<docstring> & defaults) const;
67 docstring definition() const;
69 docstring displayDefinition() const;
71 size_t numArgs() const;
73 size_t numOptionals() const;
75 bool redefinition() const { return redefinition_; }
77 MacroType type() const { return type_; }
79 /// check name and possible other formal properties
80 bool validMacro() const;
82 bool validName() const;
83 /// Remove everything from the name which makes it invalid
84 /// and return true iff it is valid.
85 bool fixNameAndCheckIfValid();
87 /// request "external features"
88 void validate(LaTeXFeatures &) const override;
90 /// decide whether its a redefinition
91 void updateToContext(MacroContext const & mc);
94 void draw(PainterInfo & pi, int x, int y) const override;
96 void metrics(MetricsInfo & mi, Dimension & dim) const override;
97 /// identifies macro templates
98 InsetMathMacroTemplate * asMacroTemplate() override { return this; }
99 /// identifies macro templates
100 InsetMathMacroTemplate const * asMacroTemplate() const override { return this; }
102 InsetCode lyxCode() const override { return MATHMACRO_CODE; }
104 void infoize(odocstream & os) const override;
106 std::string contextMenuName() const override;
108 void addToToc(DocIterator const & di, bool output_active,
109 UpdateType utype, TocBackend & backend) const override;
112 void doDispatch(Cursor & cur, FuncRequest & cmd) override;
113 /// do we want to handle this event?
114 bool getStatus(Cursor & cur, FuncRequest const & cmd,
115 FuncStatus & status) const override;
118 friend class InsetLabelBox;
119 friend class InsetDisplayLabelBox;
122 Inset * clone() const override;
124 /// remove #n with from<=n<=to
125 void removeArguments(Cursor & cur, DocIterator const & inset_pos,
127 /// shift every #n with from<=n, i.e. #n -> #(n-by)
128 void shiftArguments(size_t from, int by);
130 void insertParameter(Cursor & cur, DocIterator const & inset_pos,
131 int pos, bool greedy = false, bool addarg = true);
133 void removeParameter(Cursor & cur, DocIterator const & inset_pos,
134 int pos, bool greedy = false);
136 void makeOptional(Cursor & cur, DocIterator const & inset_pos);
138 void makeNonOptional(Cursor & cur, DocIterator const & inset_pos);
140 idx_type defIdx() const { return optionals_ + 1; }
141 /// index of default value cell of optional parameter (#1 -> n=0)
142 idx_type optIdx(idx_type n) const { return n + 1; }
144 idx_type displayIdx() const { return optionals_ + 2; }
146 void updateLook() const;
147 /// look through the macro for #n arguments
148 int maxArgumentInDefinition() const;
149 /// add missing #n arguments up to \c maxArg
150 void insertMissingArguments(int maxArg);
152 void changeArity(Cursor & cur, DocIterator const & inset_pos,
154 /// find arguments in definition and adapt the arity accordingly
155 void commitEditChanges(Cursor & cur, DocIterator const & inset_pos);
156 /// The representation of the macro template, with some holes to edit
157 mutable MathData look_;
159 mutable int numargs_;
161 mutable int argsInLook_;
164 /// keeps the old optional default value when an
165 /// optional argument is disabled
166 std::vector<MathData> optionalValues_;
168 /// (re)newcommand or def
169 mutable MacroType type_;
170 /// defined before already?
173 void createLook(int args) const;
175 mutable bool lookOutdated_;
176 /// true if in pre-calculations of metrics to get height of boxes
177 mutable bool premetrics_;
179 mutable int labelBoxAscent_;
181 mutable int labelBoxDescent_;
183 bool premetrics() const { return premetrics_; }
185 int commonLabelBoxAscent() const { return labelBoxAscent_; }
187 int commonLabelBoxDescent() const { return labelBoxDescent_; }