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"
24 /// This class contains the data for a macro.
25 class MathMacro : public InsetMathNest {
27 /// A macro can be built from an existing template
28 MathMacro(docstring const & name);
30 virtual MathMacro * asMacro() { return this; }
32 virtual MathMacro const * asMacro() const { return this; }
34 void draw(PainterInfo & pi, int x, int y) const;
35 /// draw selection background
36 void drawSelection(PainterInfo & pi, int x, int y) const;
38 void drawDecoration(PainterInfo & pi, int x, int y) const
39 { drawMarkers2(pi, x, y); }
41 void metrics(MetricsInfo & mi, Dimension & dim) const;
44 /// get cursor position
45 void cursorPos(BufferView const & bv, CursorSlice const & sl,
46 bool boundary, int & x, int & y) const;
48 void edit(Cursor & cur, bool left);
50 Inset * editXY(Cursor & cur, int x, int y);
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;
58 virtual bool notifyCursorLeaves(Cursor &);
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);
66 void validate(LaTeXFeatures &) const;
69 void write(WriteStream & os) const;
71 void maple(MapleStream &) const;
73 void mathmlize(MathStream &) const;
75 void octave(OctaveStream &) const;
77 void infoize(odocstream &) const;
79 void infoize2(odocstream &) const;
81 /// fold the macro in the next metrics call
82 void fold(Cursor & cur);
83 /// unfold the macro in the next metrics call
84 void unfold(Cursor & cur);
85 /// will it be folded or unfolded in the next metric call?
90 DISPLAY_INTERACTIVE_INIT,
96 DisplayMode displayMode() const { return displayMode_; }
99 bool extraBraces() const { return displayMode_ == DISPLAY_NORMAL && arity() > 0; }
103 docstring name() const;
105 bool validName() const;
107 size_t arity() const {
108 if (displayMode_ == DISPLAY_NORMAL )
109 return cells_.size();
115 size_t optionals() const { return optionals_; }
117 void setOptionals(int n) {
118 if (n <= int(nargs()))
123 friend class MathData;
124 friend class ArgumentProxy;
127 /// update the display mode (should only be called after detaching arguments)
128 void setDisplayMode(DisplayMode mode);
129 /// compute the next display mode
130 DisplayMode computeDisplayMode() const;
131 /// update macro definition
132 void updateMacro(MacroContext const & mc);
133 /// check if macro definition changed, argument changed etc. and adapt
134 void updateRepresentation(Cursor const * bvCur);
135 /// empty macro, put arguments into args, possibly strip arity-attachedArgsNum_ empty ones.
136 /// Includes the optional arguments.
137 void detachArguments(std::vector<MathData> & args, bool strip);
138 /// attach arguments (maybe less than arity at the end of an MathData),
139 /// including the optional ones (even if it can be empty here)
140 void attachArguments(std::vector<MathData> const & args, size_t arity, int optionals);
142 bool editing() { return editing_; }
144 MacroData const * macro() { return macro_; }
148 virtual Inset * clone() const;
149 /// the index of the cursor slice of the macro, or -1 if it is not edited
150 int cursorIdx(Cursor const & cur) const;
152 bool editMode(Cursor const & cur) const;
156 /// current display mode
157 DisplayMode displayMode_;
158 /// display mode before change
159 InsetMathSqrt expanded_;
160 /// number of arguments that were really attached
161 size_t attachedArgsNum_;
162 /// cursor position during last draw
163 idx_type previousCurIdx_;
164 /// optional argument attached? (only in DISPLAY_NORMAL mode)
166 /// fold mode to be set in next metrics call?
168 /// if macro_ == true, then here is a copy of the macro
169 /// don't use it for locking
170 MacroData macroBackup_;
171 /// if macroNotFound_ == false, then here is a reference to the macro
172 /// this might invalidate after metrics was called
173 MacroData const * macro_;
177 std::string requires_;
178 /// update macro representation