]> git.lyx.org Git - features.git/blob - src/mathed/MathMacroTemplate.h
* Lazy MathData to avoid unneeded interpretation of macro definitions
[features.git] / src / mathed / MathMacroTemplate.h
1 // -*- C++ -*-
2 /**
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.
6  *
7  * \author Alejandro Aguilar Sierra
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef MATH_MACROTEMPLATE_H
14 #define MATH_MACROTEMPLATE_H
15
16 #include "InsetMathNest.h"
17 #include "MacroTable.h"
18 #include "MathData.h"
19
20 #include "support/types.h"
21
22
23 namespace lyx {
24
25 /// This class contains the macro definition.
26 class MathMacroTemplate : public InsetMathNest {
27 public:
28         ///
29         MathMacroTemplate();
30         ///
31         MathMacroTemplate(docstring const & name, int nargs, int optional, 
32                 MacroType type,
33                 std::vector<MathData> const & optionalValues = std::vector<MathData>(),
34                 MathData const & def = MathData(),
35                 MathData const & display = MathData());
36         ///
37         explicit MathMacroTemplate(const docstring & str);
38         ///
39         EDITABLE editable() const { return HIGHLY_EDITABLE; }
40         ///
41         void read(Buffer const &, Lexer & lex);
42         ///
43         void write(Buffer const &, std::ostream & os) const;
44         ///
45         void write(WriteStream & os) const;
46         /// Output LaTeX code, but assume that the macro is not definied yet
47         /// if overwriteRedefinition is true
48         void write(WriteStream & os, bool overwriteRedefinition) const;
49         ///
50         int plaintext(Buffer const &, odocstream &,
51                 OutputParams const &) const;
52         ///
53         bool noFontChange() const { return true; }
54
55         ///
56         docstring name() const;
57         ///
58         void getDefaults(std::vector<docstring> & defaults) const;
59         ///
60         docstring definition() const;
61         ///
62         docstring displayDefinition() const;
63         ///
64         size_t numArgs() const;
65         ///
66         size_t numOptionals() const;
67         ///
68         bool redefinition() const { return redefinition_; }
69         ///
70         MacroType type() const { return type_; }
71
72         /// check name and possible other formal properties
73         bool validMacro() const;
74         ///
75         bool validName() const;
76         /// Remove everything from the name which makes it invalid 
77         /// and return true iff it is valid.
78         bool fixNameAndCheckIfValid();
79
80         /// decide whether its a redefinition
81         void updateToContext(MacroContext const & mc) const;
82
83         ///
84         void draw(PainterInfo & pi, int x, int y) const;
85         ///
86         void metrics(MetricsInfo & mi, Dimension & dim) const;
87         /// identifies macro templates
88         MathMacroTemplate * asMacroTemplate() { return this; }
89         /// identifies macro templates
90         MathMacroTemplate const * asMacroTemplate() const { return this; }
91         ///
92         InsetCode lyxCode() const { return MATHMACRO_CODE; }
93
94 protected:
95         ///
96         virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
97         /// do we want to handle this event?
98         bool getStatus(Cursor & cur, FuncRequest const & cmd,
99                 FuncStatus & status) const;
100
101 private:
102         virtual Inset * clone() const;
103
104         /// remove #n with from<=n<=to
105         void removeArguments(Cursor & cur, int from, int to);
106         /// shift every #n with from<=n, i.e. #n -> #(n-by)
107         void shiftArguments(size_t from, int by);
108         ///
109         void insertParameter(Cursor & cur, int pos, bool greedy = false);
110         ///
111         void removeParameter(Cursor & cur, int pos, bool greedy = false );
112         ///
113         void makeOptional(Cursor & cur);
114         ///
115         void makeNonOptional(Cursor & cur);
116         ///
117         idx_type defIdx() const { return optionals_ + 1; }
118         /// index of default value cell of optional parameter (#1 -> n=0)
119         idx_type optIdx(idx_type n) const { return n + 1; }
120         ///
121         idx_type displayIdx() const { return optionals_ + 2; }
122         /// The label with some holes to edit
123         mutable MathData label_;
124         ///
125         mutable int numargs_;
126         ///
127         int optionals_;
128         /// keeps the old optional default value when an 
129         /// optional argument is disabled
130         std::vector<MathData> optionalValues_;
131
132         /// (re)newcommand or def
133         mutable MacroType type_;
134         /// defined before already?
135         mutable bool redefinition_;
136 };
137
138
139 } // namespace lyx
140
141 #endif