]> git.lyx.org Git - lyx.git/blob - src/mathed/math_xdata.C
enable direct input of #1...#9; some whitespace changes
[lyx.git] / src / mathed / math_xdata.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_scriptinset.h"
8 #include "math_support.h"
9 #include "Painter.h"
10 #include "debug.h"
11
12
13 extern MathScriptInset const * asScript(MathArray::const_iterator it);
14
15
16 MathXArray::MathXArray()
17         : width_(0), ascent_(0), descent_(0), xo_(0), yo_(0), size_()
18 {}
19
20
21 void MathXArray::metrics(MathMetricsInfo const & mi) const
22 {
23         size_ = mi;
24         mathed_char_dim(LM_TC_VAR, mi, 'I', ascent_, descent_, width_);
25
26         if (data_.empty()) 
27                 return;
28
29         math_font_max_dim(LM_TC_TEXTRM, mi, ascent_, descent_); 
30         width_ = 0;
31
32         //lyxerr << "MathXArray::metrics(): '" << data_ << "'\n";
33         
34         for (const_iterator it = begin(); it != end(); ++it) {
35                 MathInset const * p = it->nucleus();
36                 MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
37                 if (q) {
38                         q->metrics(p, mi);
39                         ascent_  = std::max(ascent_,  q->ascent2(p));
40                         descent_ = std::max(descent_, q->descent2(p));
41                         width_  += q->width2(p);        
42                         ++it;
43                 } else {
44                         p->metrics(mi);
45                         ascent_  = std::max(ascent_,  p->ascent());
46                         descent_ = std::max(descent_, p->descent());
47                         width_  += p->width();  
48                 }
49         }
50         //lyxerr << "MathXArray::metrics(): '" << ascent_ << " " 
51         //      << descent_ << " " << width_ << "'\n";
52 }
53
54
55 void MathXArray::draw(Painter & pain, int x, int y) const
56 {
57         xo_ = x;
58         yo_ = y;
59
60         if (data_.empty()) {
61                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
62                 return;
63         }
64
65         for (const_iterator it = begin(); it != end(); ++it) {
66                 MathInset const * p = it->nucleus();
67                 MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
68                 if (q) {
69                         q->draw(p, pain, x, y);
70                         x += q->width2(p);
71                         ++it;
72                 } else {
73                         p->draw(pain, x, y);
74                         x += p->width();
75                 }
76         }
77 }
78
79
80 int MathXArray::pos2x(size_type targetpos) const
81 {
82         int x = 0;
83         const_iterator target = std::min(begin() + targetpos, end());
84         for (const_iterator it = begin(); it < target; ++it) {
85                 MathInset const * p = it->nucleus();
86                 MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
87                 if (q) {
88                         ++it;
89                         if (it < target)
90                                 x += q->width2(p);
91                         else  // "half" position
92                                 x += q->dxx(p) + q->nwid(p);
93                 } else
94                         x += p->width();
95         }
96         return x;
97 }
98
99
100 MathArray::size_type MathXArray::x2pos(int targetx) const
101 {
102         const_iterator it = begin();
103         int lastx = 0;
104         int currx = 0;
105         for ( ; currx < targetx && it < end(); ++it) {
106                 lastx = currx;
107
108                 int wid = 0;
109                 MathInset const * p = it->nucleus();
110                 MathScriptInset const * q = 0;
111                 if (it + 1 != end())
112                         q = asScript(it);
113                 if (q) {
114                         wid = q->width2(p);
115                         ++it;
116                 } else
117                         wid = p->width();
118
119                 currx += wid;
120         }
121         if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
122                 --it;
123         return it - begin();
124 }
125
126
127 int MathXArray::dist(int x, int y) const
128 {
129         int xx = 0;
130         int yy = 0;
131
132         if (x < xo_)
133                 xx = xo_ - x;
134         else if (x > xo_ + width_)
135                 xx = x - xo_ - width_;
136
137         if (y < yo_ - ascent_)
138                 yy = yo_ - ascent_ - y;
139         else if (y > yo_ + descent_)
140                 yy = y - yo_ - descent_;
141
142         return xx + yy; 
143 }
144
145
146 void MathXArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
147 {
148         x1 = xo_;
149         x2 = xo_ + width_;
150         y1 = yo_ - ascent_;
151         y2 = yo_ + descent_;
152 }
153
154 /*
155 void MathXArray::findPos(MathPosFinder & f) const
156 {
157         double x = xo_;
158         double y = yo_; 
159         for (const_iterator it = begin(); it < end(); ++it) {
160                 // check this position in the cell first
161                 f.visit(x, y);
162                 f.nextPos();
163
164                 // check inset
165                 MathInset const * p = it->nucleus();
166                 p->findPos(f);
167
168                 // move on
169                 MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
170                 if (q) {
171                         x += q->width(p);
172                         f.nextPos();
173                         ++it;
174                 } else {
175                         x += p->width();
176                 }
177         }
178 }
179 */