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