]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.h
merge MathArray and MathXArray classes.
[lyx.git] / src / mathed / math_data.h
1 // -*- C++ -*-
2 /*
3  *   You are free to use and modify this code under the terms of
4  *   the GNU General Public Licence version 2 or later.
5  */
6
7 /** \class MathArray
8     \brief Low level container for math insets
9     \author Alejandro Aguilar Sierra
10     \author André Pönitz
11     \author Lars Gullik Bjønnes
12     \version February 2001
13   */
14
15 #ifndef MATH_DATA_H
16 #define MATH_DATA_H
17
18 #include <iosfwd>
19 #include <vector>
20
21 #include "math_atom.h"
22 #include "dimension.h"
23
24 class MathMacro;
25 class LaTeXFeatures;
26 class ReplaceData;
27 class MathMetricsInfo;
28 class MathPainterInfo;
29 class TextMetricsInfo;
30 class TextPainter;
31
32
33 #ifdef __GNUG__
34 #pragma interface
35 #endif
36
37
38 class MathArray : private std::vector<MathAtom> {
39 public:
40         /// re-use inhertited stuff
41         typedef std::vector<MathAtom> base_type;
42         using base_type::const_iterator;
43         using base_type::iterator;
44         using base_type::size_type;
45         using base_type::difference_type;
46         using base_type::size;
47         using base_type::empty;
48         using base_type::clear;
49         using base_type::begin;
50         using base_type::end;
51         using base_type::push_back;
52         using base_type::pop_back;
53         using base_type::back;
54         using base_type::front;
55         using base_type::swap;
56         ///
57         typedef size_type idx_type;
58         typedef size_type pos_type;
59         
60 public:
61         ///
62         MathArray();
63         ///
64         MathArray(const_iterator from, const_iterator to);
65         ///
66         void append(MathArray const & ar);
67
68         /// inserts single atom at position pos
69         void insert(size_type pos, MathAtom const & at);
70         /// inserts multiple atoms at position pos
71         void insert(size_type pos, MathArray const & ar);
72
73         /// erase range from pos1 to pos2
74         void erase(iterator pos1, iterator pos2);
75         /// erase single atom
76         void erase(iterator pos);
77         /// erase range from pos1 to pos2
78         void erase(size_type pos1, size_type pos2);
79         /// erase single atom
80         void erase(size_type pos);
81
82         ///
83         void dump() const;
84         ///
85         void dump2() const;
86         ///
87         void substitute(MathMacro const & macro);
88         ///
89         void replace(ReplaceData &);
90
91         /// looks for exact match
92         bool match(MathArray const & ar) const;
93         /// looks for inclusion match starting at pos
94         bool matchpart(MathArray const & ar, pos_type pos) const;
95         /// looks for containment, return == size mean not found
96         size_type find(MathArray const & ar) const;
97         /// looks for containment, return == size mean not found
98         size_type find_last(MathArray const & ar) const;
99         ///
100         bool contains(MathArray const & ar) const;
101         ///
102         void validate(LaTeXFeatures &) const;
103
104         /// checked write access
105         MathAtom & operator[](pos_type);
106         /// checked read access
107         MathAtom const & operator[](pos_type) const;
108         /// rebuild cached metrics information
109         Dimension const & metrics(MathMetricsInfo & mi) const;
110         /// redraw cell using cache metrics information
111         void draw(MathPainterInfo & pi, int x, int y) const;
112         /// rebuild cached metrics information
113         Dimension const & metricsT(TextMetricsInfo const & mi) const;
114         /// redraw cell using cache metrics information
115         void drawT(TextPainter & pi, int x, int y) const;
116         /// mark cell for re-drawing
117         void touch() const;
118
119         /// access to cached x coordinate of last drawing
120         int xo() const { return xo_; }
121         /// access to cached y coordinate of last drawing
122         int yo() const { return yo_; }
123         /// access to cached x coordinate of mid point of last drawing
124         int xm() const { return xo_ + dim_.w / 2; }
125         /// access to cached y coordinate of mid point of last drawing
126         int ym() const { return yo_ + (dim_.d - dim_.a) / 2; }
127         /// returns x coordinate of given position in the array
128         int pos2x(size_type pos) const;
129         /// returns position of given x coordinate
130         int pos2x(size_type pos1, size_type pos2, int glue) const;
131         /// returns position of given x coordinate
132         size_type x2pos(int pos) const;
133         /// returns position of given x coordinate fstarting from a certain pos
134         size_type x2pos(size_type startpos, int targetx, int glue) const;
135         /// returns distance of this cell to the point given by x and y
136         // assumes valid position and size cache
137         int dist(int x, int y) const;
138
139         /// ascent of this cell above the baseline
140         int ascent() const { return dim_.a; }
141         /// descent of this cell below the baseline
142         int descent() const { return dim_.d; }
143         /// height of the cell
144         int height() const { return dim_.a + dim_.d; }
145         /// width of this cell
146         int width() const { return dim_.w; }
147         /// dimensions of cell
148         Dimension const & dim() const   { return dim_; }
149         /// dimensions of cell
150         void setDim(Dimension const & d) const { dim_ = d; }
151         /// bounding box of this cell
152         void boundingBox(int & xlow, int & xhigh, int & ylow, int & yhigh);
153         /// gives center coordinates
154         void center(int & x, int & y) const;
155         /// adjust (x,y) to point on boundary on a straight line from the center
156         void towards(int & x, int & y) const;
157
158 private:
159         /// is this an exact match at this position?
160         bool find1(MathArray const & ar, size_type pos) const;
161
162         /// cached dimensions of cell
163         mutable Dimension dim_;
164         /// cached x coordinate of last drawing
165         mutable int xo_;
166         /// cached y coordinate of last drawing
167         mutable int yo_;
168         /// cached cleaness of cell
169         mutable bool clean_;
170         /// cached draw status of cell
171         mutable bool drawn_;
172 };
173
174 ///
175 std::ostream & operator<<(std::ostream & os, MathArray const & ar);
176
177
178 #endif