]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacroTemplate.h
4a496e0857f967c9ea5fa8b2081e9abca2a2a22b
[lyx.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(Buffer * buf);
30         ///
31         MathMacroTemplate(Buffer * buf, docstring const & name, int nargs,
32                 int optional, MacroType type, 
33                 std::vector<MathData> const & optionalValues = std::vector<MathData>(),
34                 MathData const & def = MathData(),
35                 MathData const & display = MathData());
36         ///
37         MathMacroTemplate(Buffer * buf, const docstring & str);
38         ///
39         bool editable() const { return true; }
40         ///
41         void edit(Cursor & cur, bool front, EntryDirection entry_from);
42         ///
43         bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
44         ///
45         void read(Lexer & lex);
46         ///
47         void write(std::ostream & os) const;
48         ///
49         void write(WriteStream & os) const;
50         /// Output LaTeX code, but assume that the macro is not definied yet
51         /// if overwriteRedefinition is true
52         void write(WriteStream & os, bool overwriteRedefinition) const;
53         ///
54         int plaintext(odocstream &, OutputParams const &) const;
55         ///
56         bool noFontChange() const { return true; }
57
58         ///
59         docstring name() const;
60         ///
61         void getDefaults(std::vector<docstring> & defaults) const;
62         ///
63         docstring definition() const;
64         ///
65         docstring displayDefinition() const;
66         ///
67         size_t numArgs() const;
68         ///
69         size_t numOptionals() const;
70         ///
71         bool redefinition() const { return redefinition_; }
72         ///
73         MacroType type() const { return type_; }
74
75         /// check name and possible other formal properties
76         bool validMacro() const;
77         ///
78         bool validName() const;
79         /// Remove everything from the name which makes it invalid 
80         /// and return true iff it is valid.
81         bool fixNameAndCheckIfValid();
82         
83         /// request "external features"
84         virtual void validate(LaTeXFeatures &) const;
85
86         /// decide whether its a redefinition
87         void updateToContext(MacroContext const & mc) const;
88
89         ///
90         void draw(PainterInfo & pi, int x, int y) const;
91         ///
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; }
97         ///
98         InsetCode lyxCode() const { return MATHMACRO_CODE; }
99         ///
100         void infoize(odocstream & os) const;
101         ///
102         docstring contextMenu(BufferView const &, int, int) const;
103 protected:
104         ///
105         virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
106         /// do we want to handle this event?
107         bool getStatus(Cursor & cur, FuncRequest const & cmd,
108                 FuncStatus & status) const;
109
110 private:
111         friend class InsetLabelBox;
112         friend class DisplayLabelBox;
113         
114         ///
115         virtual Inset * clone() const;
116
117         /// remove #n with from<=n<=to
118         void removeArguments(Cursor & cur, DocIterator const & inset_pos,
119                 int from, int to);
120         /// shift every #n with from<=n, i.e. #n -> #(n-by)
121         void shiftArguments(size_t from, int by);
122         ///
123         void insertParameter(Cursor & cur, DocIterator const & inset_pos,
124                 int pos, bool greedy = false, bool addarg = true); 
125         ///
126         void removeParameter(Cursor & cur, DocIterator const & inset_pos,
127                 int pos, bool greedy = false);
128         ///
129         void makeOptional(Cursor & cur, DocIterator const & inset_pos);
130         ///
131         void makeNonOptional(Cursor & cur, DocIterator const & inset_pos);
132         ///
133         idx_type defIdx() const { return optionals_ + 1; }
134         /// index of default value cell of optional parameter (#1 -> n=0)
135         idx_type optIdx(idx_type n) const { return n + 1; }
136         ///
137         idx_type displayIdx() const { return optionals_ + 2; }
138         ///
139         void updateLook() const;
140         /// look through the macro for #n arguments
141         int maxArgumentInDefinition() const;
142         /// add missing #n arguments up to \c maxArg
143         void insertMissingArguments(int maxArg);
144         /// change the arity
145         void changeArity(Cursor & cur, DocIterator const & inset_pos,
146                 int newNumArg);
147         /// find arguments in definition and adapt the arity accordingly
148         void commitEditChanges(Cursor & cur, DocIterator const & inset_pos);
149         /// The representation of the macro template, with some holes to edit
150         mutable MathData look_;
151         ///
152         mutable int numargs_;
153         ///
154         mutable int argsInLook_;
155         ///
156         int optionals_;
157         /// keeps the old optional default value when an 
158         /// optional argument is disabled
159         std::vector<MathData> optionalValues_;
160
161         /// (re)newcommand or def
162         mutable MacroType type_;
163         /// defined before already?
164         mutable bool redefinition_;
165         ///
166         void createLook(int args) const;
167         ///
168         mutable bool lookOutdated_;
169         /// true if in pre-calculations of metrics to get height of boxes
170         mutable bool premetrics_;
171         ///
172         mutable int labelBoxAscent_;
173         ///
174         mutable int labelBoxDescent_;
175         ///
176         bool premetrics() const { return premetrics_; }
177         ///
178         int commonLabelBoxAscent() const { return labelBoxAscent_; }
179         ///
180         int commonLabelBoxDescent() const { return labelBoxDescent_; }
181 };
182
183
184 } // namespace lyx
185
186 #endif