]> git.lyx.org Git - lyx.git/blob - src/mathed/xarray.C
some visual support for \lefteqn
[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 "math_scriptinset.h"
10 #include "mathed/support.h"
11 #include "math_defs.h"
12 #include "Painter.h"
13 #include "debug.h"
14
15
16 MathXArray::MathXArray()
17         : width_(0), ascent_(0), descent_(0), xo_(0), yo_(0), style_(LM_ST_TEXT)
18 {}
19
20
21 void MathXArray::metrics(MathStyles st) const
22 {
23         style_   = st;
24         mathed_char_dim(LM_TC_VAR, st, 'I', ascent_, descent_, width_);
25
26         if (data_.empty()) 
27                 return;
28
29         math_font_max_dim(LM_TC_TEXTRM, st, ascent_, descent_); 
30         width_   = 0;
31
32         //lyxerr << "MathXArray::metrics(): '" << data_ << "'\n";
33         
34         for (const_iterator it = begin(); it != end(); ++it) {
35                 MathInset const * p = it->nucleus();
36                 if (MathScriptInset const * q = data_.asScript(it)) {
37                         q->metrics(p, st);
38                         ascent_  = std::max(ascent_,  q->ascent(p));
39                         descent_ = std::max(descent_, q->descent(p));
40                         width_  += q->width(p); 
41                         ++it;
42                 } else {
43                         p->metrics(st);
44                         ascent_  = std::max(ascent_,  p->ascent());
45                         descent_ = std::max(descent_, p->descent());
46                         width_  += p->width();  
47                 }
48         }
49         //lyxerr << "MathXArray::metrics(): '" << ascent_ << " " 
50         //      << descent_ << " " << width_ << "'\n";
51 }
52
53
54 void MathXArray::draw(Painter & pain, int x, int y) const
55 {
56         xo_ = x;
57         yo_ = y;
58
59         if (data_.empty()) {
60                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
61                 return;
62         }
63
64         for (const_iterator it = begin(); it != end(); ++it) {
65                 MathInset const * p = it->nucleus();
66                 if (MathScriptInset const * q = data_.asScript(it)) {
67                         q->draw(p, pain, x, y);
68                         x += q->width(p);
69                         ++it;
70                 } else {
71                         p->draw(pain, x, y);
72                         x += p->width();
73                 }
74         }
75 }
76
77
78 int MathXArray::pos2x(size_type targetpos) const
79 {
80         int x = 0;
81         const_iterator target = std::min(begin() + targetpos, end());
82         for (const_iterator it = begin(); it < target; ++it) {
83                 MathInset const * p = it->nucleus();
84                 if (MathScriptInset const * q = data_.asScript(it)) {
85                         ++it;
86                         if (it < target)
87                                 x += q->width(p);
88                         else  // "half" position
89                                 x += q->dxx(p) + q->nwid(p);
90                 } else
91                         x += p->width();
92         }
93         return x;
94 }
95
96
97 MathArray::size_type MathXArray::x2pos(int targetx) const
98 {
99         const_iterator it = begin();
100         int lastx = 0;
101         int currx = 0;
102         for ( ; currx < targetx && it < end(); ++it) {
103                 lastx = currx;
104
105                 int wid = 0;
106                 MathInset const * p = it->nucleus();
107                 if (MathScriptInset const * q = data_.asScript(it)) {
108                         wid = q->width(p);
109                         ++it;
110                 } else
111                         wid = p->width();
112
113                 currx += wid;
114         }
115         if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
116                 --it;
117         return it - begin();
118 }
119
120
121 std::ostream & operator<<(std::ostream & os, MathXArray const & ar)
122 {
123         os << ar.data_;
124         return os;
125 }