]> git.lyx.org Git - lyx.git/blob - src/mathed/xarray.C
remove more forms.h cruft
[lyx.git] / src / mathed / xarray.C
1
2 #include <config.h>
3
4 #ifdef __GNUG__
5 #pragma implementation
6 #endif
7
8 #include "xarray.h"
9 #include "math_inset.h"
10 #include "mathed/support.h"
11 #include "math_defs.h"
12 #include "Painter.h"
13
14 using std::max;
15 using std::min;
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(); data_.next(pos)) {
35                 MathInset * p = data_.GetInset(pos);
36                 if (p) {
37                         p->Metrics(st);
38                         ascent_  = max(ascent_,  p->ascent());
39                         descent_ = max(descent_, p->descent());
40                         width_   += p->width();
41                 } else {
42                         char cx = data_.GetChar(pos); 
43                         MathTextCodes fc = data_.GetCode(pos); 
44                         int asc;
45                         int des;
46                         int wid;
47                         mathed_char_dim(fc, style_, cx, asc, des, wid);
48                         ascent_  = max(ascent_, asc);
49                         descent_ = max(descent_, des);
50                         width_   += wid;
51                 }
52         }
53 }
54
55
56 void MathXArray::draw(Painter & pain, int x, int y)
57 {
58         xo_ = x;
59         yo_ = y;
60
61         if (data_.empty()) {
62                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
63                 return;
64         }
65
66         for (int pos = 0; pos < data_.size(); data_.next(pos)) {
67                 MathInset * p = data_.GetInset(pos);
68                 if (p) {
69                         p->draw(pain, x, y);
70                         x += p->width();
71                 } else {
72                         char cx = data_.GetChar(pos);
73                         MathTextCodes fc = data_.GetCode(pos);
74                         string s;
75                         s += cx;
76                         drawStr(pain, fc, style_, x, y, s);
77                         x += mathed_char_width(fc, style_, cx);
78                 }
79         }
80 }
81
82
83 int MathXArray::pos2x(int targetpos) const
84 {
85         int x = 0;
86         targetpos = min(targetpos, data_.size());
87         for (int pos = 0; pos < targetpos; data_.next(pos)) 
88                 x += width(pos);
89         return x;
90 }
91
92
93 int MathXArray::x2pos(int targetx) const
94 {
95         int pos = 0;
96         for (int x = 0; x < targetx && pos < data_.size(); data_.next(pos))
97                 x += width(pos);
98         return pos;
99 }
100
101 int MathXArray::width(int pos) const
102 {
103         if (pos >= data_.size())
104                 return 0;
105
106         if (data_.isInset(pos)) 
107                 return data_.GetInset(pos)->width();
108         else 
109                 return mathed_char_width(data_.GetCode(pos), style_, data_.GetChar(pos));
110 }