]> git.lyx.org Git - lyx.git/blob - src/mathed/xarray.C
58d1b77a8c6059b3773e0180825fcce061bf65f9
[lyx.git] / src / mathed / xarray.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "xarray.h"
8 #include "math_inset.h"
9 #include "mathed/support.h"
10 #include "math_defs.h"
11 #include "Painter.h"
12 #include "debug.h"
13
14 using std::max;
15 using std::min;
16
17
18 MathXArray::MathXArray()
19         : width_(0), ascent_(0), descent_(0), xo_(0), yo_(0), style_(LM_ST_TEXT)
20 {}
21
22
23 void MathXArray::metrics(MathStyles st) const
24 {
25         if (data_.empty()) {
26                 mathed_char_dim(LM_TC_VAR, st, 'I', ascent_, descent_, width_); 
27                 return;
28         }
29         
30         ascent_  = 0;
31         descent_ = 0;
32         width_   = 0;
33         style_   = st;
34
35         //lyxerr << "MathXArray::metrics(): '" << data_ << "'\n";
36         for (int pos = 0; pos < data_.size(); ++pos) {
37                 MathInset const * p = data_.nextInset(pos);
38                 p->metrics(st);
39                 int asc  = p->ascent();
40                 int des  = p->descent();
41                 int wid  = p->width();
42                 ascent_  = max(ascent_, asc);
43                 descent_ = max(descent_, des);
44                 width_   += wid;
45         }
46 }
47
48
49 void MathXArray::draw(Painter & pain, int x, int y) const
50 {
51         xo_ = x;
52         yo_ = y;
53
54         if (data_.empty()) {
55                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
56                 return;
57         }
58
59         for (int pos = 0; pos < data_.size(); ++pos) {
60                 MathInset const * p = data_.nextInset(pos);
61                 p->draw(pain, x, y);
62                 x += p->width();
63         }
64 }
65
66
67 int MathXArray::pos2x(int targetpos) const
68 {
69         int x = 0;
70         targetpos = min(targetpos, data_.size());
71         for (int pos = 0; pos < targetpos; ++pos) 
72                 x += width(pos);
73         return x;
74 }
75
76
77 int MathXArray::x2pos(int targetx) const
78 {
79         int pos   = 0;
80         int lastx = 0;
81         int currx = 0;
82         for ( ; currx < targetx && pos < data_.size(); ++pos) {
83                 lastx = currx;
84                 currx += width(pos);
85         }
86         if (abs(lastx - targetx) < abs(currx - targetx) && pos > 0)
87                 --pos;
88         return pos;
89 }
90
91
92 int MathXArray::width(int pos) const
93 {
94         if (pos >= data_.size())
95                 return 0;
96         return data_.nextInset(pos)->width();
97 }
98
99
100 std::ostream & operator<<(std::ostream & os, MathXArray const & ar)
101 {
102         os << ar.data_;
103         return os;
104 }
105