]> git.lyx.org Git - features.git/blob - src/mathed/MathMacroTemplate.h
* simplify notifyCursorLeaves logic: We now only call it from strategic
[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 edit(Cursor & cur, bool front, EntryDirection entry_from);
42         ///
43         bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
44         ///
45         void read(Buffer const &, Lexer & lex);
46         ///
47         void write(Buffer const &, 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(Buffer const &, odocstream &,
55                 OutputParams const &) const;
56         ///
57         bool noFontChange() const { return true; }
58
59         ///
60         docstring name() const;
61         ///
62         void getDefaults(std::vector<docstring> & defaults) const;
63         ///
64         docstring definition() const;
65         ///
66         docstring displayDefinition() const;
67         ///
68         size_t numArgs() const;
69         ///
70         size_t numOptionals() const;
71         ///
72         bool redefinition() const { return redefinition_; }
73         ///
74         MacroType type() const { return type_; }
75
76         /// check name and possible other formal properties
77         bool validMacro() const;
78         ///
79         bool validName() const;
80         /// Remove everything from the name which makes it invalid 
81         /// and return true iff it is valid.
82         bool fixNameAndCheckIfValid();
83         
84         /// request "external features"
85         virtual void validate(LaTeXFeatures &) const;
86
87         /// decide whether its a redefinition
88         void updateToContext(MacroContext const & mc) const;
89
90         ///
91         void draw(PainterInfo & pi, int x, int y) const;
92         ///
93         void metrics(MetricsInfo & mi, Dimension & dim) const;
94         /// identifies macro templates
95         MathMacroTemplate * asMacroTemplate() { return this; }
96         /// identifies macro templates
97         MathMacroTemplate const * asMacroTemplate() const { return this; }
98         ///
99         InsetCode lyxCode() const { return MATHMACRO_CODE; }
100         ///
101         void infoize(odocstream & os) const;
102
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, int from, int to);
119         /// shift every #n with from<=n, i.e. #n -> #(n-by)
120         void shiftArguments(size_t from, int by);
121         ///
122         void insertParameter(Cursor & cur, int pos, bool greedy = false);
123         ///
124         void removeParameter(Cursor & cur, int pos, bool greedy = false );
125         ///
126         void makeOptional(Cursor & cur);
127         ///
128         void makeNonOptional(Cursor & cur);
129         ///
130         idx_type defIdx() const { return optionals_ + 1; }
131         /// index of default value cell of optional parameter (#1 -> n=0)
132         idx_type optIdx(idx_type n) const { return n + 1; }
133         ///
134         idx_type displayIdx() const { return optionals_ + 2; }
135         ///
136         void updateLook() const;
137         /// The representation of the macro tempalte, with some holes to edit
138         mutable MathData look_;
139         ///
140         mutable int numargs_;
141         ///
142         int optionals_;
143         /// keeps the old optional default value when an 
144         /// optional argument is disabled
145         std::vector<MathData> optionalValues_;
146
147         /// (re)newcommand or def
148         mutable MacroType type_;
149         /// defined before already?
150         mutable bool redefinition_;
151         ///
152         void createLook() const;
153         ///
154         mutable bool lookOutdated_;
155         /// true if in pre-calculations of metrics to get height of boxes
156         mutable bool premetrics_;
157         ///
158         mutable int labelBoxAscent_;
159         ///
160         mutable int labelBoxDescent_;
161         ///
162         bool premetrics() const { return premetrics_; }
163         ///
164         int commonLabelBoxAscent() const { return labelBoxAscent_; }
165         ///
166         int commonLabelBoxDescent() const { return labelBoxDescent_; }
167 };
168
169
170 } // namespace lyx
171
172 #endif