]> git.lyx.org Git - lyx.git/blob - src/mathed/xarray.C
Fix working of the spellchecker dialog with ispell when there are no
[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, st, ascent_, descent_); 
29         width_   = 0;
30
31         //lyxerr << "MathXArray::metrics(): '" << data_ << "'\n";
32         for (size_type 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         //lyxerr << "MathXArray::metrics(): '" << ascent_ << " " 
40         //      << descent_ << " " << width_ << "'\n";
41 }
42
43
44 void MathXArray::draw(Painter & pain, int x, int y) const
45 {
46         xo_ = x;
47         yo_ = y;
48
49         if (data_.empty()) {
50                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
51                 return;
52         }
53
54         for (size_type pos = 0; pos < data_.size(); ++pos) {
55                 MathAtom const * p = data_.at(pos);
56                 p->draw(pain, x, y);
57                 x += p->width();
58         }
59 }
60
61
62 int MathXArray::pos2x(size_type targetpos) const
63 {
64         int x = 0;
65         targetpos = std::min(targetpos, data_.size());
66         for (size_type pos = 0; pos < targetpos; ++pos) 
67                 x += width(pos);
68         return x;
69 }
70
71
72 MathArray::size_type MathXArray::x2pos(int targetx) const
73 {
74         size_type pos  = 0;
75         int lastx      = 0;
76         int currx      = 0;
77         for ( ; currx < targetx && pos < data_.size(); ++pos) {
78                 lastx = currx;
79                 currx += width(pos);
80         }
81         if (abs(lastx - targetx) < abs(currx - targetx) && pos > 0)
82                 --pos;
83         return pos;
84 }
85
86
87 int MathXArray::width(size_type pos) const
88 {
89         MathAtom const * t = data_.at(pos);
90         return t ? t->width() : 0;
91 }
92
93
94 std::ostream & operator<<(std::ostream & os, MathXArray const & ar)
95 {
96         os << ar.data_;
97         return os;
98 }