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