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