]> git.lyx.org Git - lyx.git/blob - src/mathed/xarray.C
fix pullArg when pressing <Delete> at the end of an cell
[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, int, int)
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         // keep last values for scriptInset's need to look back
35         int asc = 0;
36         int des = 0;
37         int wid = 0;
38         mathed_char_height(LM_TC_VAR, st, 'I', asc, des);
39
40         for (int pos = 0; pos < data_.size(); data_.next(pos)) {
41                 MathInset * p = data_.nextInset(pos);
42                 if (p) {
43                         // only MathUpDownInsets will use the asc/des information...
44                         p->Metrics(st, asc, des);
45                         asc = p->ascent();
46                         des = p->descent();
47                         wid = p->width();
48                 } else {
49                         char cx = data_.GetChar(pos); 
50                         MathTextCodes fc = data_.GetCode(pos); 
51                         mathed_char_dim(fc, style_, cx, asc, des, wid);
52                 }
53                 ascent_  = max(ascent_, asc);
54                 descent_ = max(descent_, des);
55                 width_   += wid;
56         }
57 }
58
59
60 void MathXArray::draw(Painter & pain, int x, int y)
61 {
62         xo_ = x;
63         yo_ = y;
64
65         if (data_.empty()) {
66                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
67                 return;
68         }
69
70         for (int pos = 0; pos < data_.size(); data_.next(pos)) {
71                 MathInset * p = data_.nextInset(pos);
72                 if (p) {
73                         p->draw(pain, x, y);
74                         x += p->width();
75                 } else {
76                         char cx = data_.GetChar(pos);
77                         MathTextCodes fc = data_.GetCode(pos);
78                         string s;
79                         s += cx;
80                         drawStr(pain, fc, style_, x, y, s);
81                         x += mathed_char_width(fc, style_, cx);
82                 }
83         }
84 }
85
86
87 int MathXArray::pos2x(int targetpos) const
88 {
89         int x = 0;
90         targetpos = min(targetpos, data_.size());
91         for (int pos = 0; pos < targetpos; data_.next(pos)) 
92                 x += width(pos);
93         return x;
94 }
95
96
97 int MathXArray::x2pos(int targetx) const
98 {
99         int pos = 0;
100         for (int x = 0; x < targetx && pos < data_.size(); data_.next(pos))
101                 x += width(pos);
102         return pos;
103 }
104
105 int MathXArray::width(int pos) const
106 {
107         if (pos >= data_.size())
108                 return 0;
109
110         if (data_.isInset(pos)) 
111                 return data_.nextInset(pos)->width();
112         else 
113                 return mathed_char_width(data_.GetCode(pos), style_, data_.GetChar(pos));
114 }
115
116 std::ostream & operator<<(std::ostream & os, MathXArray const & ar)
117 {
118         os << ar.data_;
119         return os;
120 }
121