]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacro.h
* removed MathMacro:: from declaration.
[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 namespace lyx {
22
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(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() 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 left);
49         ///
50         Inset * editXY(Cursor & cur, int x, int y);
51
52         /// target pos when we enter the inset from the left by pressing "Right"
53         bool idxFirst(Cursor &) const;
54         /// target pos when we enter the inset from the right by pressing "Left"
55         bool idxLast(Cursor &) const;
56
57         ///
58         virtual bool notifyCursorLeaves(Cursor &);
59         
60         /// Remove cell (starting from 0)
61         void removeArgument(size_t pos);
62         /// Insert empty cell (starting from 0)
63         void insertArgument(size_t pos);
64
65         ///
66         void validate(LaTeXFeatures &) const;
67
68         ///
69         void write(WriteStream & os) const;
70         ///
71         void maple(MapleStream &) const;
72         ///
73         void mathmlize(MathStream &) const;
74         ///
75         void octave(OctaveStream &) const;
76         ///
77         void infoize(odocstream &) const;
78         ///
79         void infoize2(odocstream &) const;
80
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?
86         bool folded() const;
87                 
88         enum DisplayMode {
89                 DISPLAY_INIT,
90                 DISPLAY_UNFOLDED,
91                 DISPLAY_NORMAL,
92         };
93
94         ///
95         DisplayMode displayMode() const { return displayMode_; }
96
97         ///
98         bool extraBraces() const { return displayMode_ == DISPLAY_NORMAL && arity() > 0; }
99
100         
101         ///
102         docstring name() const;
103         ///
104         bool validName() const;
105         ///
106         size_t arity() const { 
107                 if (displayMode_ == DISPLAY_NORMAL )
108                         return cells_.size();
109                 else
110                         return 0;
111         }
112                 
113         ///
114         int optionals() const { return optionals_; }
115         ///
116         void setOptionals(int n) { 
117                 if (n <= int(nargs()))
118                         optionals_ = n;
119         }
120         
121 protected:
122         friend class MathData;
123         friend class ArgumentProxy;
124
125         /// update the display mode (should only be called after detaching arguments)
126         void setDisplayMode(DisplayMode mode);
127         /// compute the next display mode
128         DisplayMode computeDisplayMode(MetricsInfo const & mi) const;
129         /// update macro definition
130         void updateMacro(MetricsInfo & mi);
131         /// check if macro definition changed, argument changed etc. and adapt
132         void updateRepresentation(MetricsInfo & mi);
133         /// empty macro, put arguments into args, possibly strip arity-attachedArgsNum_ empty ones.
134         /// Includes the optional arguments.
135         void detachArguments(std::vector<MathData> & args, bool strip);
136         /// attach arguments (maybe less than arity at the end of an MathData),
137         /// including the optional ones (even if it can be empty here)
138         void attachArguments(std::vector<MathData> const & args, size_t arity, int optionals);
139         ///
140         bool editing() { return editing_; }
141         ///
142         MacroData const * macro() { return macro_; }
143
144 private:
145         ///
146         virtual Inset * clone() const;
147         /// the index of the cursor slice of the macro, or -1 if it is not edited
148         int cursorIdx(Cursor const & cur) const;
149         ///
150         bool editMode(Cursor const & cur) const;
151         
152         /// name of macro
153         docstring name_;
154         /// current display mode
155         DisplayMode displayMode_;
156         /// display mode before change
157         InsetMathSqrt expanded_;
158         /// number of arguments that were really attached
159         size_t attachedArgsNum_;
160         /// cursor position during last draw
161         int previousCurIdx_;
162         /// optional argument attached? (only in DISPLAY_NORMAL mode)
163         int optionals_;
164         /// fold mode to be set in next metrics call?
165         bool nextFoldMode_;
166         /// if macro_ == true, then here is a copy of the macro
167         /// don't use it for locking
168         MacroData macroBackup_;
169         /// if macroNotFound_ == false, then here is a reference to the macro
170         /// this might invalidate after metrics was called
171         MacroData const * macro_;
172         ///
173         bool editing_;
174         ///
175         std::string requires_;
176         /// update macro representation
177         bool needsUpdate_;
178 };
179
180 } // namespace lyx
181 #endif