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