]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacroTemplate.h
* cellDim_ is not needed anymore
[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();
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 left);
42         ///
43         Inset * editXY(Cursor & cur, int x, int y);
44         ///
45         bool notifyCursorLeaves(Cursor & cur);
46         ///
47         void read(Buffer const &, Lexer & lex);
48         ///
49         void write(Buffer const &, std::ostream & os) const;
50         ///
51         void write(WriteStream & os) const;
52         /// Output LaTeX code, but assume that the macro is not definied yet
53         /// if overwriteRedefinition is true
54         void write(WriteStream & os, bool overwriteRedefinition) const;
55         ///
56         int plaintext(Buffer const &, odocstream &,
57                 OutputParams const &) const;
58         ///
59         bool noFontChange() const { return true; }
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         /// 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 protected:
103         ///
104         virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
105         /// do we want to handle this event?
106         bool getStatus(Cursor & cur, FuncRequest const & cmd,
107                 FuncStatus & status) const;
108
109 private:
110         friend class InsetLabelBox;
111         friend class DisplayLabelBox;
112         
113         ///
114         virtual Inset * clone() const;
115
116         /// remove #n with from<=n<=to
117         void removeArguments(Cursor & cur, int from, int to);
118         /// shift every #n with from<=n, i.e. #n -> #(n-by)
119         void shiftArguments(size_t from, int by);
120         ///
121         void insertParameter(Cursor & cur, int pos, bool greedy = false);
122         ///
123         void removeParameter(Cursor & cur, int pos, bool greedy = false );
124         ///
125         void makeOptional(Cursor & cur);
126         ///
127         void makeNonOptional(Cursor & cur);
128         ///
129         idx_type defIdx() const { return optionals_ + 1; }
130         /// index of default value cell of optional parameter (#1 -> n=0)
131         idx_type optIdx(idx_type n) const { return n + 1; }
132         ///
133         idx_type displayIdx() const { return optionals_ + 2; }
134         ///
135         void updateLook() const;
136         /// The representation of the macro tempalte, with some holes to edit
137         mutable MathData look_;
138         ///
139         mutable int numargs_;
140         ///
141         int optionals_;
142         /// keeps the old optional default value when an 
143         /// optional argument is disabled
144         std::vector<MathData> optionalValues_;
145
146         /// (re)newcommand or def
147         mutable MacroType type_;
148         /// defined before already?
149         mutable bool redefinition_;
150         ///
151         void createLook() const;
152         ///
153         mutable bool lookOutdated_;
154         /// true if in pre-calculations of metrics to get height of boxes
155         mutable bool premetrics_;
156         ///
157         mutable int labelBoxAscent_;
158         ///
159         mutable int labelBoxDescent_;
160         ///
161         bool premetrics() const { return premetrics_; }
162         ///
163         int commonLabelBoxAscent() const { return labelBoxAscent_; }
164         ///
165         int commonLabelBoxDescent() const { return labelBoxDescent_; }
166 };
167
168
169 } // namespace lyx
170
171 #endif