]> git.lyx.org Git - lyx.git/blob - src/mathed/MathData.h
Coding style
[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 #include "MathAtom.h"
20
21 #include "support/strfwd.h"
22
23 #include <vector>
24
25
26 namespace lyx {
27
28 class BufferView;
29 class Cursor;
30 class DocIterator;
31 class LaTeXFeatures;
32 class ReplaceData;
33 class MacroContext;
34 class MathMacro;
35 class MetricsInfo;
36 class PainterInfo;
37 class TextMetricsInfo;
38 class TextPainter;
39
40
41 class MathData : private std::vector<MathAtom> {
42 public:
43         /// re-use inhertited stuff
44         typedef std::vector<MathAtom> base_type;
45         using base_type::const_iterator;
46         using base_type::iterator;
47         using base_type::size_type;
48         using base_type::difference_type;
49         using base_type::size;
50         using base_type::empty;
51         using base_type::clear;
52         using base_type::begin;
53         using base_type::end;
54         using base_type::push_back;
55         using base_type::pop_back;
56         using base_type::back;
57         using base_type::front;
58         ///
59         typedef size_type idx_type;
60         typedef size_type pos_type;
61
62 public:
63         ///
64         MathData() {}
65         ///
66         MathData(const_iterator from, const_iterator to);
67         ///
68         void append(MathData const & ar);
69
70         /// inserts single atom at position pos
71         void insert(size_type pos, MathAtom const & at);
72         /// inserts multiple atoms at position pos
73         void insert(size_type pos, MathData const & ar);
74
75         /// erase range from pos1 to pos2
76         void erase(iterator pos1, iterator pos2);
77         /// erase single atom
78         void erase(iterator pos);
79         /// erase range from pos1 to pos2
80         void erase(size_type pos1, size_type pos2);
81         /// erase single atom
82         void erase(size_type pos);
83
84         ///
85         void dump() const;
86         ///
87         void dump2() const;
88         ///
89         void replace(ReplaceData &);
90         ///
91         void substitute(MathData const & m);
92
93         /// looks for exact match
94         bool match(MathData const & ar) const;
95         /// looks for inclusion match starting at pos
96         bool matchpart(MathData const & ar, pos_type pos) const;
97         /// looks for containment, return == size mean not found
98         size_type find(MathData const & ar) const;
99         /// looks for containment, return == size mean not found
100         size_type find_last(MathData const & ar) const;
101         ///
102         bool contains(MathData const & ar) const;
103         ///
104         void validate(LaTeXFeatures &) const;
105
106         /// checked write access
107         MathAtom & operator[](pos_type);
108         /// checked read access
109         MathAtom const & operator[](pos_type) const;
110         /// rebuild cached metrics information
111         void metrics(MetricsInfo & mi, Dimension & dim) const;
112         ///
113         Dimension const & dimension(BufferView const &) const;
114
115         /// redraw cell using cache metrics information
116         void draw(PainterInfo & pi, int x, int y) const;
117         /// rebuild cached metrics information
118         void metricsT(TextMetricsInfo const & mi, Dimension & dim) const;
119         /// redraw cell using cache metrics information
120         void drawT(TextPainter & pi, int x, int y) const;
121         /// mark cell for re-drawing
122         void touch() const;
123
124         /// access to cached x coordinate of last drawing
125         int xo(BufferView const & bv) const;
126         /// access to cached y coordinate of last drawing
127         int yo(BufferView const & bv) const;
128         /// access to cached x coordinate of mid point of last drawing
129         int xm(BufferView const & bv) const;
130         /// access to cached y coordinate of mid point of last drawing
131         int ym(BufferView const & bv) const;
132         /// write access to coordinate;
133         void setXY(BufferView & bv, int x, int y) const;
134         /// returns x coordinate of given position in the array
135         int pos2x(BufferView const * bv, size_type pos) const;
136         /// returns position of given x coordinate
137         int pos2x(BufferView const * bv, size_type pos, int glue) const;
138         /// returns position of given x coordinate
139         size_type x2pos(BufferView const * bv, int pos) const;
140         /// returns position of given x coordinate fstarting from a certain pos
141         size_type x2pos(BufferView const * bv, int targetx, int glue) const;
142         /// returns distance of this cell to the point given by x and y
143         // assumes valid position and size cache
144         int dist(BufferView const & bv, int x, int y) const;
145
146         /// minimum ascent offset for superscript
147         int minasc() const { return minasc_; }
148         /// minimum descent offset for subscript
149         int mindes() const { return mindes_; }
150         /// level above/below which super/subscript should extend
151         int slevel() const { return slevel_; }
152         /// additional super/subscript shift
153         int sshift() const { return sshift_; }
154         /// superscript kerning
155         int kerning(BufferView const *) const { return kerning_; }
156         ///
157         void swap(MathData & ar) { base_type::swap(ar); }
158
159         /// attach/detach arguments to macros, updating the cur to 
160         /// stay visually at the same position (cur==0 is allowed)
161         void updateMacros(Cursor * cur, MacroContext const & mc);
162
163 protected:
164         /// cached values for super/subscript placement
165         mutable int minasc_;
166         mutable int mindes_;
167         mutable int slevel_;
168         mutable int sshift_;
169         mutable int kerning_;
170         
171 private:
172         /// is this an exact match at this position?
173         bool find1(MathData const & ar, size_type pos) const;
174
175         ///
176         void detachMacroParameters(Cursor * cur, const size_type macroPos);
177         ///
178         void attachMacroParameters(Cursor * cur, const size_type macroPos, 
179                 const size_type macroNumArgs, const int macroOptionals,
180                 const bool fromInitToNormalMode, const bool interactiveInit,
181                 const size_t appetite);
182         ///
183         void collectOptionalParameters(Cursor * cur, 
184                 const size_type numOptionalParams, std::vector<MathData> & params, 
185                 size_t & pos, MathAtom & scriptToPutAround,
186                 const pos_type macroPos, const int thisPos, const int thisSlice);
187         ///
188         void collectParameters(Cursor * cur, 
189                 const size_type numParams, std::vector<MathData> & params, 
190                 size_t & pos, MathAtom & scriptToPutAround,
191                 const pos_type macroPos, const int thisPos, const int thisSlice,
192                 const size_t appetite);
193 };
194
195 ///
196 std::ostream & operator<<(std::ostream & os, MathData const & ar);
197 ///
198 odocstream & operator<<(odocstream & os, MathData const & ar);
199
200
201 } // namespace lyx
202
203 #endif