]> git.lyx.org Git - lyx.git/blob - src/mathed/MathData.h
Kill macro mode when using LFUN_ESCAPE
[lyx.git] / src / mathed / MathData.h
1 // -*- C++ -*-
2 /**
3  * \file MathData.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  * \author Lars Gullik Bjønnes
10  * \author Stefan Schimanski
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef MATH_DATA_H
16 #define MATH_DATA_H
17
18 #include "Dimension.h"
19
20 #include "MathAtom.h"
21 #include "MathRow.h"
22
23 #include "OutputEnums.h"
24
25 #include "support/strfwd.h"
26
27 #include <cstddef>
28 #include <vector>
29 #include <map>
30
31
32 namespace lyx {
33
34 class Buffer;
35 class BufferView;
36 class Cursor;
37 class DocIterator;
38 class LaTeXFeatures;
39 class ReplaceData;
40 class MacroContext;
41 class InsetMathMacro;
42 class MetricsInfo;
43 class PainterInfo;
44 class ParIterator;
45 class TextMetricsInfo;
46 class TextPainter;
47
48
49 class MathData : private std::vector<MathAtom> {
50 public:
51         /// re-use inhertited stuff
52         typedef std::vector<MathAtom> base_type;
53         using base_type::const_iterator;
54         using base_type::iterator;
55         using base_type::size_type;
56         using base_type::difference_type;
57         using base_type::size;
58         using base_type::empty;
59         using base_type::clear;
60         using base_type::begin;
61         using base_type::end;
62         using base_type::push_back;
63         using base_type::pop_back;
64         using base_type::back;
65         using base_type::front;
66         ///
67         typedef size_type idx_type;
68         typedef size_type pos_type;
69
70 public:
71         ///
72         MathData(Buffer * buf = 0) : minasc_(0), mindes_(0), slevel_(0),
73                                      sshift_(0), kerning_(0), buffer_(buf) {}
74         ///
75         MathData(Buffer * buf, const_iterator from, const_iterator to);
76         ///
77         Buffer * buffer() { return buffer_; }
78         ///
79         Buffer const * buffer() const { return buffer_; }
80         ///
81         void append(MathData const & ar);
82
83         /// inserts single atom at position pos
84         void insert(size_type pos, MathAtom const & at);
85         /// inserts multiple atoms at position pos
86         void insert(size_type pos, MathData const & ar);
87
88         /// erase range from pos1 to pos2
89         void erase(iterator pos1, iterator pos2);
90         /// erase single atom
91         void erase(iterator pos);
92         /// erase range from pos1 to pos2
93         void erase(size_type pos1, size_type pos2);
94         /// erase single atom
95         void erase(size_type pos);
96
97         ///
98         void dump() const;
99         ///
100         void dump2() const;
101         ///
102         void replace(ReplaceData &);
103         ///
104         void substitute(MathData const & m);
105
106         /// looks for exact match
107         bool match(MathData const & ar) const;
108         /// looks for inclusion match starting at pos
109         bool matchpart(MathData const & ar, pos_type pos) const;
110         /// looks for containment, return == size mean not found
111         size_type find(MathData const & ar) const;
112         /// looks for containment, return == size mean not found
113         size_type find_last(MathData const & ar) const;
114         ///
115         bool contains(MathData const & ar) const;
116         ///
117         void validate(LaTeXFeatures &) const;
118
119         /// checked write access
120         MathAtom & operator[](pos_type);
121         /// checked read access
122         MathAtom const & operator[](pos_type) const;
123
124         /// Add this array to a math row. Return true if contents got added
125         bool addToMathRow(MathRow &, MetricsInfo & mi) const;
126
127         /// rebuild cached metrics information
128         void metrics(MetricsInfo & mi, Dimension & dim) const;
129         ///
130         Dimension const & dimension(BufferView const &) const;
131
132         /// draw the selection over the cell
133         void drawSelection(PainterInfo & pi, int x, int y) const;
134         /// redraw cell using cache metrics information
135         void draw(PainterInfo & pi, int x, int y) const;
136         /// rebuild cached metrics information
137         void metricsT(TextMetricsInfo const & mi, Dimension & dim) const;
138         /// redraw cell using cache metrics information
139         void drawT(TextPainter & pi, int x, int y) const;
140         /// approximate the math class of the data
141         MathClass mathClass() const;
142
143         /// access to cached x coordinate of last drawing
144         int xo(BufferView const & bv) const;
145         /// access to cached y coordinate of last drawing
146         int yo(BufferView const & bv) const;
147         /// access to cached x coordinate of mid point of last drawing
148         int xm(BufferView const & bv) const;
149         /// access to cached y coordinate of mid point of last drawing
150         int ym(BufferView const & bv) const;
151         /// write access to coordinate;
152         void setXY(BufferView & bv, int x, int y) const;
153         /// returns x coordinate of given position in the array
154         int pos2x(BufferView const * bv, size_type pos) const;
155         /// returns position of given x coordinate
156         size_type x2pos(BufferView const * bv, int targetx) const;
157         /// returns distance of this cell to the point given by x and y
158         // assumes valid position and size cache
159         int dist(BufferView const & bv, int x, int y) const;
160
161         /// minimum ascent offset for superscript
162         int minasc() const { return minasc_; }
163         /// minimum descent offset for subscript
164         int mindes() const { return mindes_; }
165         /// level above/below which super/subscript should extend
166         int slevel() const { return slevel_; }
167         /// additional super/subscript shift
168         int sshift() const { return sshift_; }
169         /// Italic correction as described in InsetMathScript.h
170         int kerning(BufferView const *) const { return kerning_; }
171         ///
172         void swap(MathData & ar) { base_type::swap(ar); }
173
174         /// attach/detach arguments to macros, updating the cur to
175         /// stay visually at the same position (cur==0 is allowed)
176         void updateMacros(Cursor * cur, MacroContext const & mc, UpdateType, int nesting);
177         ///
178         void updateBuffer(ParIterator const &, UpdateType);
179         ///
180         void setBuffer(Buffer & b) { buffer_ = &b; }
181
182 protected:
183         /// cached values for super/subscript placement
184         mutable int minasc_;
185         mutable int mindes_;
186         mutable int slevel_;
187         mutable int sshift_;
188         mutable int kerning_;
189         Buffer * buffer_;
190
191         /// cached object that describes typeset data
192         mutable std::map<BufferView*, MathRow> mrow_cache_;
193
194 private:
195         /// is this an exact match at this position?
196         bool find1(MathData const & ar, size_type pos) const;
197
198         ///
199         void detachMacroParameters(DocIterator * dit, const size_type macroPos);
200         ///
201         void attachMacroParameters(Cursor * cur, const size_type macroPos,
202                 const size_type macroNumArgs, const int macroOptionals,
203                 const bool fromInitToNormalMode, const bool interactiveInit,
204                 const size_t appetite);
205         ///
206         void collectOptionalParameters(Cursor * cur,
207                 const size_type numOptionalParams, std::vector<MathData> & params,
208                 size_t & pos, MathAtom & scriptToPutAround,
209                 const pos_type macroPos, const int thisPos, const int thisSlice);
210         ///
211         void collectParameters(Cursor * cur,
212                 const size_type numParams, std::vector<MathData> & params,
213                 size_t & pos, MathAtom & scriptToPutAround,
214                 const pos_type macroPos, const int thisPos, const int thisSlice,
215                 const size_t appetite);
216 };
217
218 ///
219 std::ostream & operator<<(std::ostream & os, MathData const & ar);
220 ///
221 odocstream & operator<<(odocstream & os, MathData const & ar);
222
223
224 } // namespace lyx
225
226 #endif