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