]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathMacro.h
g-brief loads babel internally. So don't load it ourselves.
[lyx.git] / src / mathed / InsetMathMacro.h
1 // -*- C++ -*-
2 /**
3  * \file InsetMathMacro.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_MACRO_H
14 #define MATH_MACRO_H
15
16 #include "InsetMathNest.h"
17 #include "MacroTable.h"
18 #include "MathData.h"
19
20 #include <map>
21
22 namespace lyx {
23
24 /// This class contains the data for a macro.
25 class InsetMathMacro : public InsetMathNest {
26 public:
27         /// A macro can be built from an existing template
28         InsetMathMacro(Buffer * buf, docstring const & name);
29         ///
30         InsetMathMacro(InsetMathMacro const &);
31         ///
32         InsetMathMacro & operator=(InsetMathMacro const &);
33         ///
34         ~InsetMathMacro();
35         ///
36         InsetMathMacro * asMacro() override { return this; }
37         ///
38         InsetMathMacro const * asMacro() const override { return this; }
39         ///
40         marker_type marker(BufferView const *) const override;
41         /// If the macro is in normal edit mode, dissolve its contents in
42         /// the row. Otherwise, just insert the inset.
43         bool addToMathRow(MathRow &, MetricsInfo & mi) const override;
44
45         /// Whether the inset allows \(no)limits
46         bool allowsLimitsChange() const;
47         /// The default limits value
48         Limits defaultLimits(bool display) const override;
49         /// whether the inset has limit-like sub/superscript
50         Limits limits() const override;
51         /// sets types of sub/superscripts
52         void limits(Limits lim) override;
53
54         ///
55         void beforeMetrics() const override;
56         ///
57         void afterMetrics() const override;
58         ///
59         void beforeDraw(PainterInfo const &) const override;
60         ///
61         void afterDraw(PainterInfo const &) const override;
62
63         ///
64         void metrics(MetricsInfo & mi, Dimension & dim) const override;
65         /// was the macro in edit mode when computing metrics?
66         bool editMetrics(BufferView const * bv) const;
67         ///
68         void draw(PainterInfo & pi, int x, int y) const override;
69         ///
70         int kerning(BufferView const * bv) const override;
71         /// get cursor position
72         void cursorPos(BufferView const & bv, CursorSlice const & sl,
73                 bool boundary, int & x, int & y) const override;
74         ///
75         void edit(Cursor & cur, bool front, EntryDirection entry_from) override;
76         ///
77         Inset * editXY(Cursor & cur, int x, int y) override;
78
79         /// target pos when we enter the inset while moving forward
80         bool idxFirst(Cursor &) const override;
81         /// target pos when we enter the inset while moving backwards
82         bool idxLast(Cursor &) const override;
83
84         ///
85         bool notifyCursorLeaves(Cursor const & old, Cursor & cur) override;
86
87         /// Remove cell (starting from 0)
88         void removeArgument(pos_type pos);
89         /// Insert empty cell (starting from 0)
90         void insertArgument(pos_type pos);
91
92         ///
93         void validate(LaTeXFeatures &) const override;
94         ///
95         mode_type currentMode() const override;
96
97         /// Assumes that macros are up-to-date
98         void write(TeXMathStream & os) const override;
99         ///
100         void normalize(NormalStream & os) const override;
101         ///
102         void maple(MapleStream &) const override;
103         ///
104         void maxima(MaximaStream &) const override;
105         ///
106         void mathematica(MathematicaStream &) const override;
107         ///
108         void mathmlize(MathMLStream &) const override;
109         ///
110         void htmlize(HtmlStream &) const override;
111         ///
112         void octave(OctaveStream &) const override;
113         ///
114         void infoize(odocstream &) const override;
115         ///
116         void infoize2(odocstream &) const override;
117
118         /// fold the macro in the next metrics call
119         void fold(Cursor & cur);
120         /// unfold the macro in the next metrics call
121         void unfold(Cursor & cur);
122         /// will it be folded or unfolded in the next metric call?
123         bool folded() const;
124
125         enum DisplayMode {
126                 DISPLAY_INIT,
127                 DISPLAY_INTERACTIVE_INIT,
128                 DISPLAY_UNFOLDED,
129                 DISPLAY_NORMAL
130         };
131
132         ///
133         DisplayMode displayMode() const;
134
135         ///
136         bool extraBraces() const override;
137
138         ///
139         docstring name() const override;
140         /// FIXME: Often dangling.
141         MacroData const * macro() const;
142         ///
143         docstring macroName() const;
144         /// Level of nesting in macros (including this one)
145         int nesting() const;
146         ///
147         bool validName() const;
148         ///
149         size_t arity() const;
150
151         ///
152         size_t optionals() const;
153         ///
154         void setOptionals(int n);
155         /// Return the maximal number of arguments the macro is greedy for.
156         size_t appetite() const;
157         ///
158         InsetCode lyxCode() const override { return MATH_MACRO_CODE; }
159         /// This is not used for display; however whether it is mathrel determines
160         /// how to split equations intelligently.
161         MathClass mathClass() const override;
162         /// Override so as to set Buffer for definition_ member, too.
163         void setBuffer(Buffer &) override;
164
165 protected:
166         friend class MathData;
167         friend class ArgumentProxy;
168         friend class Cursor;
169
170         /// update the display mode (should only be called after detaching arguments)
171         void setDisplayMode(DisplayMode mode, int appetite = -1);
172         /// compute the next display mode
173         DisplayMode computeDisplayMode() const;
174         /// update macro definition
175         void updateMacro(MacroContext const & mc);
176         /// check if macro definition changed, argument changed etc. and adapt
177         void updateRepresentation(Cursor * cur, MacroContext const & mc,
178                                   UpdateType, int nesting);
179         /// empty macro, put arguments into args, possibly strip arity-attachedArgsNum_ empty ones.
180         /// Includes the optional arguments.
181         void detachArguments(std::vector<MathData> & args, bool strip);
182         /// attach arguments (maybe less than arity at the end of an MathData),
183         /// including the optional ones (even if it can be empty here)
184         void attachArguments(std::vector<MathData> const & args, size_t arity, int optionals);
185
186 private:
187         /// This function is needed for now because of two shortfalls of the current
188         /// implementation: the macro() pointer is often dangling, in which case we
189         /// fall back to a backup copy, and the macro is not known at inset
190         /// creation, in which case we fall back to the global macro with this name.
191         MacroData const * macroBackup() const;
192         ///
193         Inset * clone() const override;
194         ///
195         bool editMode(BufferView const * bv) const;
196
197         ///
198         class Private;
199         ///
200         Private * d;
201         /// update lock to avoid loops
202         class UpdateLocker;
203         friend class UpdateLocker;
204
205 public:
206         ///
207         bool completionSupported(Cursor const &) const override;
208         ///
209         bool inlineCompletionSupported(Cursor const & cur) const override;
210         ///
211         bool automaticInlineCompletion() const override;
212         ///
213         bool automaticPopupCompletion() const override;
214         ///
215         CompletionList const * createCompletionList(Cursor const & cur) const override;
216         ///
217         docstring completionPrefix(Cursor const & cur) const override;
218         ///
219         bool insertCompletion(Cursor & cur, docstring const & s, bool finished) override;
220         ///
221         void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const override;
222 };
223
224 } // namespace lyx
225 #endif