]> git.lyx.org Git - lyx.git/blob - src/mathed/math_xdata.C
some support for matrix operations with maple ('M-x math-extern maple evalm')
[lyx.git] / src / mathed / math_xdata.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_scriptinset.h"
8 #include "math_support.h"
9 #include "Painter.h"
10 #include "debug.h"
11
12
13 extern MathScriptInset const * asScript(MathArray::const_iterator it);
14
15
16 MathXArray::MathXArray()
17         : width_(0), ascent_(0), descent_(0), xo_(0), yo_(0), size_()
18 {}
19
20
21 void MathXArray::metrics(MathMetricsInfo const & st) const
22 {
23         size_ = 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                 MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
37                 if (q) {
38                         q->metrics(p, st);
39                         ascent_  = std::max(ascent_,  q->ascent(p));
40                         descent_ = std::max(descent_, q->descent(p));
41                         width_  += q->width(p); 
42                         ++it;
43                 } else {
44                         p->metrics(st);
45                         ascent_  = std::max(ascent_,  p->ascent());
46                         descent_ = std::max(descent_, p->descent());
47                         width_  += p->width();  
48                 }
49         }
50         //lyxerr << "MathXArray::metrics(): '" << ascent_ << " " 
51         //      << descent_ << " " << width_ << "'\n";
52 }
53
54
55 void MathXArray::draw(Painter & pain, int x, int y) const
56 {
57         xo_ = x;
58         yo_ = y;
59
60         if (data_.empty()) {
61                 pain.rectangle(x, y - ascent_, width_, height(), LColor::mathline);
62                 return;
63         }
64
65         for (const_iterator it = begin(); it != end(); ++it) {
66                 MathInset const * p = it->nucleus();
67                 MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
68                 if (q) {
69                         q->draw(p, pain, x, y);
70                         x += q->width(p);
71                         ++it;
72                 } else {
73                         p->draw(pain, x, y);
74                         x += p->width();
75                 }
76         }
77 }
78
79
80 int MathXArray::pos2x(size_type targetpos) const
81 {
82         int x = 0;
83         const_iterator target = std::min(begin() + targetpos, end());
84         for (const_iterator it = begin(); it < target; ++it) {
85                 MathInset const * p = it->nucleus();
86                 MathScriptInset const * q = (it + 1 == end()) ? 0 : asScript(it);
87                 if (q) {
88                         ++it;
89                         if (it < target)
90                                 x += q->width(p);
91                         else  // "half" position
92                                 x += q->dxx(p) + q->nwid(p);
93                 } else
94                         x += p->width();
95         }
96         return x;
97 }
98
99
100 MathArray::size_type MathXArray::x2pos(int targetx) const
101 {
102         const_iterator it = begin();
103         int lastx = 0;
104         int currx = 0;
105         for ( ; currx < targetx && it < end(); ++it) {
106                 lastx = currx;
107
108                 int wid = 0;
109                 MathInset const * p = it->nucleus();
110                 MathScriptInset const * q = 0;
111                 if (it + 1 != end())
112                         q = asScript(it);
113                 if (q) {
114                         wid = q->width(p);
115                         ++it;
116                 } else
117                         wid = p->width();
118
119                 currx += wid;
120         }
121         if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
122                 --it;
123         return it - begin();
124 }