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