]> git.lyx.org Git - lyx.git/blob - src/mathed/xarray.C
4aa396bfa8debfa15333ff9cb299c257734eb52d
[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
15 MathXArray::MathXArray()
16         : width_(0), ascent_(0), descent_(0), xo_(0), yo_(0), style_(LM_ST_TEXT)
17 {}
18
19
20 void MathXArray::metrics(MathStyles st) const
21 {
22         style_   = 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, LM_ST_TEXT, ascent_, descent_); 
29         width_   = 0;
30
31         //lyxerr << "MathXArray::metrics(): '" << data_ << "'\n";
32         for (int pos = 0; pos < data_.size(); ++pos) {
33                 MathAtom const * p = data_.at(pos);
34                 p->metrics(st);
35                 ascent_  = std::max(ascent_,  p->ascent());
36                 descent_ = std::max(descent_, p->descent());
37                 width_  += p->width();
38         }
39 }
40
41
42 void MathXArray::draw(Painter & pain, int x, int y) const
43 {
44         xo_ = x;
45         yo_ = y;
46
47         if (data_.empty()) {
48                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
49                 return;
50         }
51
52         for (int pos = 0; pos < data_.size(); ++pos) {
53                 MathAtom const * p = data_.at(pos);
54                 p->draw(pain, x, y);
55                 x += p->width();
56         }
57 }
58
59
60 int MathXArray::pos2x(int targetpos) const
61 {
62         int x = 0;
63         targetpos = std::min(targetpos, data_.size());
64         for (int pos = 0; pos < targetpos; ++pos) 
65                 x += width(pos);
66         return x;
67 }
68
69
70 int MathXArray::x2pos(int targetx) const
71 {
72         int pos   = 0;
73         int lastx = 0;
74         int currx = 0;
75         for ( ; currx < targetx && pos < data_.size(); ++pos) {
76                 lastx = currx;
77                 currx += width(pos);
78         }
79         if (abs(lastx - targetx) < abs(currx - targetx) && pos > 0)
80                 --pos;
81         return pos;
82 }
83
84
85 int MathXArray::width(int pos) const
86 {
87         MathAtom const * t = data_.at(pos);
88         return t ? t->width() : 0;
89 }
90
91
92 std::ostream & operator<<(std::ostream & os, MathXArray const & ar)
93 {
94         os << ar.data_;
95         return os;
96 }