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