A bit more re-organization.
[lyx.git] / src / mathed / MathMacro.h
blobb431a9a9a516c9344117d618cab328cd3ee6ae72
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.
7 * \author Alejandro Aguilar Sierra
8 * \author André Pönitz
10 * Full author contact details are available in file CREDITS.
13 #ifndef MATH_MACRO_H
14 #define MATH_MACRO_H
16 #include "InsetMathNest.h"
17 #include "InsetMathSqrt.h"
18 #include "MacroTable.h"
19 #include "MathData.h"
21 #include <map>
23 namespace lyx {
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);
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;
58 ///
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);
66 ///
67 void validate(LaTeXFeatures &) const;
69 ///
70 void write(WriteStream & os) const;
71 ///
72 void maple(MapleStream &) const;
73 ///
74 void mathmlize(MathStream &) const;
75 ///
76 void octave(OctaveStream &) const;
77 ///
78 void infoize(odocstream &) const;
79 ///
80 void infoize2(odocstream &) const;
82 /// fold the macro in the next metrics call
83 void fold(Cursor & cur);
84 /// unfold the macro in the next metrics call
85 void unfold(Cursor & cur);
86 /// will it be folded or unfolded in the next metric call?
87 bool folded() const;
89 enum DisplayMode {
90 DISPLAY_INIT,
91 DISPLAY_INTERACTIVE_INIT,
92 DISPLAY_UNFOLDED,
93 DISPLAY_NORMAL,
96 ///
97 DisplayMode displayMode() const { return displayMode_; }
99 ///
100 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();
110 else
111 return 0;
115 size_t optionals() const { return optionals_; }
117 void setOptionals(int n) {
118 if (n <= int(nargs()))
119 optionals_ = n;
121 /// Return the maximal number of arguments the macro is greedy for.
122 size_t appetite() const { return appetite_; }
124 InsetCode lyxCode() const { return MATH_MACRO_CODE; }
126 protected:
127 friend class MathData;
128 friend class ArgumentProxy;
129 friend class Cursor;
131 /// update the display mode (should only be called after detaching arguments)
132 void setDisplayMode(DisplayMode mode, int appetite = -1);
133 /// compute the next display mode
134 DisplayMode computeDisplayMode() const;
135 /// update macro definition
136 void updateMacro(MacroContext const & mc);
137 /// check if macro definition changed, argument changed etc. and adapt
138 void updateRepresentation();
139 /// empty macro, put arguments into args, possibly strip arity-attachedArgsNum_ empty ones.
140 /// Includes the optional arguments.
141 void detachArguments(std::vector<MathData> & args, bool strip);
142 /// attach arguments (maybe less than arity at the end of an MathData),
143 /// including the optional ones (even if it can be empty here)
144 void attachArguments(std::vector<MathData> const & args, size_t arity, int optionals);
146 MacroData const * macro() { return macro_; }
148 bool editMetrics(BufferView const * bv) const;
150 private:
152 virtual Inset * clone() const;
154 bool editMode(BufferView const * bv) const;
156 /// name of macro
157 docstring name_;
158 /// current display mode
159 DisplayMode displayMode_;
160 /// expanded macro with ArgumentProxies
161 InsetMathSqrt expanded_;
162 /// macro definition with #1,#2,.. insets
163 MathData definition_;
164 /// number of arguments that were really attached
165 size_t attachedArgsNum_;
166 /// optional argument attached? (only in DISPLAY_NORMAL mode)
167 size_t optionals_;
168 /// fold mode to be set in next metrics call?
169 bool nextFoldMode_;
170 /// if macro_ == true, then here is a copy of the macro
171 /// don't use it for locking
172 MacroData macroBackup_;
173 /// if macroNotFound_ == false, then here is a reference to the macro
174 /// this might invalidate after metrics was called
175 MacroData const * macro_;
177 mutable std::map<BufferView const *, bool> editing_;
179 std::string requires_;
180 /// update macro representation
181 bool needsUpdate_;
182 /// maximal number of arguments the macro is greedy for
183 size_t appetite_;
185 public:
187 bool completionSupported(Cursor const &) const;
189 bool inlineCompletionSupported(Cursor const & cur) const;
191 bool automaticInlineCompletion() const;
193 bool automaticPopupCompletion() const;
195 CompletionList const * createCompletionList(Cursor const & cur) const;
197 docstring completionPrefix(Cursor const & cur) const;
199 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
201 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
204 } // namespace lyx
205 #endif