]> git.lyx.org Git - lyx.git/blob - src/mathed/math_xdata.C
cosmetics: rename (x)array.[Ch] into math_(x)data.[Ch]
[lyx.git] / src / mathed / math_xdata.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_inset.h"
8 #include "math_scriptinset.h"
9 #include "math_support.h"
10 #include "math_defs.h"
11 #include "Painter.h"
12 #include "debug.h"
13
14
15 MathXArray::MathXArray()
16         : width_(0), ascent_(0), descent_(0), xo_(0), yo_(0), size_()
17 {}
18
19
20 void MathXArray::metrics(MathMetricsInfo const & st) const
21 {
22         size_ = st;
23         mathed_char_dim(LM_TC_VAR, st, 'I', ascent_, descent_, width_);
24
25         if (data_.empty()) 
26                 return;
27
28         math_font_max_dim(LM_TC_TEXTRM, st, ascent_, descent_); 
29         width_ = 0;
30
31         //lyxerr << "MathXArray::metrics(): '" << data_ << "'\n";
32         
33         for (const_iterator it = begin(); it != end(); ++it) {
34                 MathInset const * p = it->nucleus();
35                 if (MathScriptInset const * q = data_.asScript(it)) {
36                         q->metrics(p, st);
37                         ascent_  = std::max(ascent_,  q->ascent(p));
38                         descent_ = std::max(descent_, q->descent(p));
39                         width_  += q->width(p); 
40                         ++it;
41                 } else {
42                         p->metrics(st);
43                         ascent_  = std::max(ascent_,  p->ascent());
44                         descent_ = std::max(descent_, p->descent());
45                         width_  += p->width();  
46                 }
47         }
48         //lyxerr << "MathXArray::metrics(): '" << ascent_ << " " 
49         //      << descent_ << " " << width_ << "'\n";
50 }
51
52
53 void MathXArray::draw(Painter & pain, int x, int y) const
54 {
55         xo_ = x;
56         yo_ = y;
57
58         if (data_.empty()) {
59                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
60                 return;
61         }
62
63         for (const_iterator it = begin(); it != end(); ++it) {
64                 MathInset const * p = it->nucleus();
65                 if (MathScriptInset const * q = data_.asScript(it)) {
66                         q->draw(p, pain, x, y);
67                         x += q->width(p);
68                         ++it;
69                 } else {
70                         p->draw(pain, x, y);
71                         x += p->width();
72                 }
73         }
74 }
75
76
77 int MathXArray::pos2x(size_type targetpos) const
78 {
79         int x = 0;
80         const_iterator target = std::min(begin() + targetpos, end());
81         for (const_iterator it = begin(); it < target; ++it) {
82                 MathInset const * p = it->nucleus();
83                 if (MathScriptInset const * q = data_.asScript(it)) {
84                         ++it;
85                         if (it < target)
86                                 x += q->width(p);
87                         else  // "half" position
88                                 x += q->dxx(p) + q->nwid(p);
89                 } else
90                         x += p->width();
91         }
92         return x;
93 }
94
95
96 MathArray::size_type MathXArray::x2pos(int targetx) const
97 {
98         const_iterator it = begin();
99         int lastx = 0;
100         int currx = 0;
101         for ( ; currx < targetx && it < end(); ++it) {
102                 lastx = currx;
103
104                 int wid = 0;
105                 MathInset const * p = it->nucleus();
106                 if (MathScriptInset const * q = data_.asScript(it)) {
107                         wid = q->width(p);
108                         ++it;
109                 } else
110                         wid = p->width();
111
112                 currx += wid;
113         }
114         if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
115                 --it;
116         return it - begin();
117 }