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"
25 /// This class contains the macro definition.
26 class MathMacroTemplate : public InsetMathNest {
31 MathMacroTemplate(docstring const & name, int nargs, int optional,
33 std::vector<MathData> const & optionalValues = std::vector<MathData>(),
34 MathData const & def = MathData(),
35 MathData const & display = MathData());
37 explicit MathMacroTemplate(const docstring & str);
39 EDITABLE editable() const { return HIGHLY_EDITABLE; }
41 void edit(Cursor & cur, bool left);
43 Inset * editXY(Cursor & cur, int x, int y);
45 bool notifyCursorLeaves(Cursor & cur);
47 void read(Buffer const &, Lexer & lex);
49 void write(Buffer const &, std::ostream & os) const;
51 void write(WriteStream & os) const;
52 /// Output LaTeX code, but assume that the macro is not definied yet
53 /// if overwriteRedefinition is true
54 void write(WriteStream & os, bool overwriteRedefinition) const;
56 int plaintext(Buffer const &, odocstream &,
57 OutputParams const &) const;
59 bool noFontChange() const { return true; }
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 /// decide whether its a redefinition
87 void updateToContext(MacroContext const & mc) const;
90 void draw(PainterInfo & pi, int x, int y) const;
92 void metrics(MetricsInfo & mi, Dimension & dim) const;
93 /// identifies macro templates
94 MathMacroTemplate * asMacroTemplate() { return this; }
95 /// identifies macro templates
96 MathMacroTemplate const * asMacroTemplate() const { return this; }
98 InsetCode lyxCode() const { return MATHMACRO_CODE; }
100 void infoize(odocstream & os) const;
104 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
105 /// do we want to handle this event?
106 bool getStatus(Cursor & cur, FuncRequest const & cmd,
107 FuncStatus & status) const;
110 virtual Inset * clone() const;
112 /// remove #n with from<=n<=to
113 void removeArguments(Cursor & cur, int from, int to);
114 /// shift every #n with from<=n, i.e. #n -> #(n-by)
115 void shiftArguments(size_t from, int by);
117 void insertParameter(Cursor & cur, int pos, bool greedy = false);
119 void removeParameter(Cursor & cur, int pos, bool greedy = false );
121 void makeOptional(Cursor & cur);
123 void makeNonOptional(Cursor & cur);
125 idx_type defIdx() const { return optionals_ + 1; }
126 /// index of default value cell of optional parameter (#1 -> n=0)
127 idx_type optIdx(idx_type n) const { return n + 1; }
129 idx_type displayIdx() const { return optionals_ + 2; }
131 void updateLook() const;
132 /// The representation of the macro tempalte, with some holes to edit
133 mutable MathData look_;
135 mutable int numargs_;
138 /// keeps the old optional default value when an
139 /// optional argument is disabled
140 std::vector<MathData> optionalValues_;
142 /// (re)newcommand or def
143 mutable MacroType type_;
144 /// defined before already?
145 mutable bool redefinition_;
147 mutable bool editing_;
149 void createLook() const;
151 mutable bool lookOutdated_;
152 /// true if in pre-calculations of metrics to get height of boxes
153 mutable bool premetrics_;
155 mutable int labelBoxAscent_;
157 mutable int labelBoxDescent_;
160 friend class InsetLabelBox;
162 bool premetrics() const { return premetrics_; }
164 int commonLabelBoxAscent() const { return labelBoxAscent_; }
166 int commonLabelBoxDescent() const { return labelBoxDescent_; }