4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Alejandro Aguilar Sierra
10 * Full author contact details are available in file CREDITS.
16 #include "InsetMathNest.h"
17 #include "InsetMathSqrt.h"
18 #include "MacroTable.h"
25 /// This class contains the data for a macro.
26 class MathMacro : public InsetMathNest {
28 /// A macro can be built from an existing template
29 MathMacro(Buffer * buf, docstring const & name);
31 virtual MathMacro * asMacro() { return this; }
33 virtual MathMacro const * asMacro() const { return this; }
35 void draw(PainterInfo & pi, int x, int y) const;
36 /// draw selection background
37 void drawSelection(PainterInfo & pi, int x, int y) const;
39 void drawDecoration(PainterInfo & pi, int x, int y) const
40 { drawMarkers2(pi, x, y); }
42 void metrics(MetricsInfo & mi, Dimension & dim) const;
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;
49 void edit(Cursor & cur, bool front, EntryDirection entry_from);
51 Inset * editXY(Cursor & cur, int x, int y);
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;
59 virtual bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
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);
67 void validate(LaTeXFeatures &) const;
70 void write(WriteStream & os) const;
72 void maple(MapleStream &) const;
74 void mathmlize(MathStream &) const;
76 void htmlize(HtmlStream &) const;
78 void octave(OctaveStream &) const;
80 void infoize(odocstream &) const;
82 void infoize2(odocstream &) const;
84 /// fold the macro in the next metrics call
85 void fold(Cursor & cur);
86 /// unfold the macro in the next metrics call
87 void unfold(Cursor & cur);
88 /// will it be folded or unfolded in the next metric call?
93 DISPLAY_INTERACTIVE_INIT,
99 DisplayMode displayMode() const { return displayMode_; }
102 bool extraBraces() const { return displayMode_ == DISPLAY_NORMAL && arity() > 0; }
105 docstring name() const;
107 bool validName() const;
109 size_t arity() const {
110 if (displayMode_ == DISPLAY_NORMAL )
111 return cells_.size();
117 size_t optionals() const { return optionals_; }
119 void setOptionals(int n) {
120 if (n <= int(nargs()))
123 /// Return the maximal number of arguments the macro is greedy for.
124 size_t appetite() const { return appetite_; }
126 InsetCode lyxCode() const { return MATH_MACRO_CODE; }
129 friend class MathData;
130 friend class ArgumentProxy;
133 /// update the display mode (should only be called after detaching arguments)
134 void setDisplayMode(DisplayMode mode, int appetite = -1);
135 /// compute the next display mode
136 DisplayMode computeDisplayMode() const;
137 /// update macro definition
138 void updateMacro(MacroContext const & mc);
139 /// check if macro definition changed, argument changed etc. and adapt
140 void updateRepresentation();
141 /// empty macro, put arguments into args, possibly strip arity-attachedArgsNum_ empty ones.
142 /// Includes the optional arguments.
143 void detachArguments(std::vector<MathData> & args, bool strip);
144 /// attach arguments (maybe less than arity at the end of an MathData),
145 /// including the optional ones (even if it can be empty here)
146 void attachArguments(std::vector<MathData> const & args, size_t arity, int optionals);
148 MacroData const * macro() { return macro_; }
150 bool editMetrics(BufferView const * bv) const;
154 virtual Inset * clone() const;
156 bool editMode(BufferView const * bv) const;
160 /// current display mode
161 DisplayMode displayMode_;
162 /// expanded macro with ArgumentProxies
163 InsetMathSqrt expanded_;
164 /// macro definition with #1,#2,.. insets
165 MathData definition_;
166 /// number of arguments that were really attached
167 size_t attachedArgsNum_;
168 /// optional argument attached? (only in DISPLAY_NORMAL mode)
170 /// fold mode to be set in next metrics call?
172 /// if macro_ == true, then here is a copy of the macro
173 /// don't use it for locking
174 MacroData macroBackup_;
175 /// if macroNotFound_ == false, then here is a reference to the macro
176 /// this might invalidate after metrics was called
177 MacroData const * macro_;
179 mutable std::map<BufferView const *, bool> editing_;
181 std::string requires_;
182 /// update macro representation
184 /// maximal number of arguments the macro is greedy for
189 bool completionSupported(Cursor const &) const;
191 bool inlineCompletionSupported(Cursor const & cur) const;
193 bool automaticInlineCompletion() const;
195 bool automaticPopupCompletion() const;
197 CompletionList const * createCompletionList(Cursor const & cur) const;
199 docstring completionPrefix(Cursor const & cur) const;
201 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
203 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;