]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacro.h
1d4b0ae1b028e778c190954df0e0125bc957a58e
[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 "InsetMathSqrt.h"
18 #include "MacroTable.h"
19 #include "MathData.h"
20
21 #include <map>
22
23 namespace lyx {
24
25 /// This class contains the data for a macro.
26 class MathMacro : public InsetMathNest {
27 public:
28         /// A macro can be built from an existing template
29         MathMacro(Buffer * buf, docstring const & name);
30         ///
31         virtual MathMacro * asMacro() { return this; }
32         ///
33         virtual MathMacro const * asMacro() const { return this; }
34         ///
35         void draw(PainterInfo & pi, int x, int y) const;
36         /// draw selection background
37         void drawSelection(PainterInfo & pi, int x, int y) const;
38         /// draw decorations.
39         void drawDecoration(PainterInfo & pi, int x, int y) const
40         { drawMarkers2(pi, x, y); }
41         ///
42         void metrics(MetricsInfo & mi, Dimension & dim) const;
43         ///
44         int kerning(BufferView const * bv) const;
45         /// get cursor position
46         void cursorPos(BufferView const & bv, CursorSlice const & sl,
47                 bool boundary, int & x, int & y) const;
48         ///
49         void edit(Cursor & cur, bool front, EntryDirection entry_from);
50         ///
51         Inset * editXY(Cursor & cur, int x, int y);
52
53         /// target pos when we enter the inset while moving forward
54         bool idxFirst(Cursor &) const;
55         /// target pos when we enter the inset while moving backwards
56         bool idxLast(Cursor &) const;
57
58         ///
59         virtual bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
60         
61         /// Remove cell (starting from 0)
62         void removeArgument(pos_type pos);
63         /// Insert empty cell (starting from 0)
64         void insertArgument(pos_type pos);
65
66         ///
67         void validate(LaTeXFeatures &) const;
68
69         ///
70         void write(WriteStream & os) const;
71         ///
72         void normalize(NormalStream & os) const;
73         ///
74         void maple(MapleStream &) const;
75         ///
76         void maxima(MaximaStream &) const;
77         ///
78         void mathematica(MathematicaStream &) const;
79         ///
80         void mathmlize(MathStream &) const;
81         ///
82         void htmlize(HtmlStream &) const;
83         ///
84         void octave(OctaveStream &) const;
85         ///
86         void infoize(odocstream &) const;
87         ///
88         void infoize2(odocstream &) const;
89
90         /// fold the macro in the next metrics call
91         void fold(Cursor & cur);
92         /// unfold the macro in the next metrics call
93         void unfold(Cursor & cur);
94         /// will it be folded or unfolded in the next metric call?
95         bool folded() const;
96                 
97         enum DisplayMode {
98                 DISPLAY_INIT,
99                 DISPLAY_INTERACTIVE_INIT,
100                 DISPLAY_UNFOLDED,
101                 DISPLAY_NORMAL
102         };
103
104         ///
105         DisplayMode displayMode() const { return displayMode_; }
106
107         ///
108         bool extraBraces() const { return displayMode_ == DISPLAY_NORMAL && arity() > 0; }
109
110         ///
111         docstring name() const;
112         ///
113         bool validName() const;
114         ///
115         size_t arity() const { 
116                 if (displayMode_ == DISPLAY_NORMAL )
117                         return cells_.size();
118                 else
119                         return 0;
120         }
121                 
122         ///
123         size_t optionals() const { return optionals_; }
124         ///
125         void setOptionals(int n) { 
126                 if (n <= int(nargs()))
127                         optionals_ = n;
128         }
129         /// Return the maximal number of arguments the macro is greedy for.
130         size_t appetite() const { return appetite_; }
131         ///
132         InsetCode lyxCode() const { return MATH_MACRO_CODE; }
133
134 protected:
135         friend class MathData;
136         friend class ArgumentProxy;
137         friend class Cursor;
138
139         /// update the display mode (should only be called after detaching arguments)
140         void setDisplayMode(DisplayMode mode, int appetite = -1);
141         /// compute the next display mode
142         DisplayMode computeDisplayMode() const;
143         /// update macro definition
144         void updateMacro(MacroContext const & mc);
145         /// check if macro definition changed, argument changed etc. and adapt
146         void updateRepresentation(Cursor * cur, MacroContext const & mc, UpdateType);
147         /// empty macro, put arguments into args, possibly strip arity-attachedArgsNum_ empty ones.
148         /// Includes the optional arguments.
149         void detachArguments(std::vector<MathData> & args, bool strip);
150         /// attach arguments (maybe less than arity at the end of an MathData),
151         /// including the optional ones (even if it can be empty here)
152         void attachArguments(std::vector<MathData> const & args, size_t arity, int optionals);
153         ///
154         MacroData const * macro() { return macro_; }
155         ///
156         bool editMetrics(BufferView const * bv) const;
157
158 private:
159         ///
160         virtual Inset * clone() const;
161         ///
162         bool editMode(BufferView const * bv) const;
163         
164         /// name of macro
165         docstring name_;
166         /// current display mode
167         DisplayMode displayMode_;
168         /// expanded macro with ArgumentProxies
169         InsetMathSqrt expanded_;
170         /// macro definition with #1,#2,.. insets
171         MathData definition_;
172         /// number of arguments that were really attached
173         size_t attachedArgsNum_;
174         /// optional argument attached? (only in DISPLAY_NORMAL mode)
175         size_t optionals_;
176         /// fold mode to be set in next metrics call?
177         bool nextFoldMode_;
178         /// if macro_ == true, then here is a copy of the macro
179         /// don't use it for locking
180         MacroData macroBackup_;
181         /// if macroNotFound_ == false, then here is a reference to the macro
182         /// this might invalidate after metrics was called
183         MacroData const * macro_;
184         ///
185         mutable std::map<BufferView const *, bool> editing_;
186         ///
187         std::string requires_;
188         /// update macro representation
189         bool needsUpdate_;
190         /// update lock to avoid loops
191         class UpdateLocker;
192         friend class UpdateLocker;
193         bool isUpdating_;
194         /// maximal number of arguments the macro is greedy for
195         size_t appetite_;
196
197 public:
198         ///
199         bool completionSupported(Cursor const &) const;
200         ///
201         bool inlineCompletionSupported(Cursor const & cur) const;
202         ///
203         bool automaticInlineCompletion() const;
204         ///
205         bool automaticPopupCompletion() const;
206         ///
207         CompletionList const * createCompletionList(Cursor const & cur) const;
208         ///
209         docstring completionPrefix(Cursor const & cur) const;
210         ///
211         bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
212         ///
213         void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
214 };
215
216 } // namespace lyx
217 #endif