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