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