]> git.lyx.org Git - lyx.git/blob - src/mathed/math_xyarrowinset.C
whichFont down to 5.3%
[lyx.git] / src / mathed / math_xyarrowinset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_xyarrowinset.h"
8 #include "math_xymatrixinset.h"
9 #include "math_mathmlstream.h"
10 #include "math_streamstr.h"
11 #include "math_support.h"
12 #include "Painter.h"
13 #include "debug.h"
14
15
16 MathXYArrowInset::MathXYArrowInset()
17         : MathNestInset(2)
18 {}
19
20
21 MathInset * MathXYArrowInset::clone() const
22 {
23         return new MathXYArrowInset(*this);
24 }
25
26
27 MathXYMatrixInset const * MathXYArrowInset::targetMatrix() const
28 {
29         return mi_.inset ? mi_.inset->asXYMatrixInset() : 0;
30 }
31
32
33 MathXArray const & MathXYArrowInset::targetCell() const
34 {
35         MathXYMatrixInset const * p = targetMatrix();
36         int x = 0;
37         int y = 0;
38         MathArray const & t = cell(0);
39         for (MathArray::const_iterator it = t.begin(); it != t.end(); ++it) {
40                 switch ((*it)->getChar()) {
41                         case 'l': --x; break;
42                         case 'r': ++x; break;
43                         case 'u': --y; break;
44                         case 'd': ++y; break;
45                 }
46         }
47         //lyxerr << "target: x: " << x << " y: " << y << "\n";
48         int n = mi_.idx + p->ncols() * y + x;
49         if (n < 0 || n >= int(p->nargs())) {
50                 lyxerr << "source: n: " << mi_.idx << "\n";
51                 lyxerr << "target: n: " << n << " out of range\n";
52                 n = 0;
53         }
54   return p->xcell(n);
55 }
56
57
58 MathXArray const & MathXYArrowInset::sourceCell() const
59 {
60   return targetMatrix()->xcell(mi_.idx);
61 }
62
63
64 void MathXYArrowInset::metrics(MathMetricsInfo const & mi) const
65 {
66         mi_ = mi;
67         MathNestInset::metrics(mi);
68
69         if (editing()) {
70                 int w    = mathed_string_width(LM_TC_TEXTRM, mi_, "target: ");
71                 width_   = w + std::max(xcell(0).width(), xcell(1).width());
72                 ascent_  = xcell(0).ascent();
73                 descent_ = xcell(0).descent() + xcell(1).height() + 10;
74         } else {
75                 width_   = 0;
76                 ascent_  = 0;
77                 descent_ = 0;
78                 //mathed_string_dim(LM_TC_TEXTRM, mi_, "X", ascent_, descent_, width_);
79         }
80 }
81
82
83 void MathXYArrowInset::draw(Painter & pain, int x, int y) const
84 {
85         metrics(mi_);
86
87         if (editing()) {
88
89                 int lasc;
90                 int ldes;
91                 int lwid;
92                 mathed_string_dim(LM_TC_TEXTRM, mi_, "target: ", lasc, ldes, lwid);
93
94                 xcell(0).draw(pain, x + lwid, y);
95                 drawStr(pain, LM_TC_TEXTRM, mi_, x + 3, y, "target");
96                 y += std::max(xcell(0).descent(), ldes) + 5;
97
98                 y += std::max(xcell(1).ascent(), lasc) + 5;
99                 xcell(1).draw(pain, x + lwid, y);
100                 drawStr(pain, LM_TC_TEXTRM, mi_, x + 3, y, "label");
101
102         } else {
103
104                 //drawStr(pain, LM_TC_TEXTRM, mi_, x, y, "X");
105                 MathXArray const & s = sourceCell();
106                 MathXArray const & t = targetCell();
107                 pain.line(s.xm(), s.ym(), t.xm(), t.ym(), LColor::math);
108                 xcell(1).draw(pain, (s.xm() + t.xm())/2, (s.ym() + t.ym())/2);
109                 
110         }
111 }
112
113
114 void MathXYArrowInset::write(WriteStream & os) const
115 {
116         os << "\\ar";
117         if (cell(0).size())
118                 os << "[" << cell(0) << "]";
119         if (cell(1).size())
120                 os << (up_ ? "^" : "_") << "{" << cell(1) << "}";
121         os << " ";
122 }
123
124
125 void MathXYArrowInset::normalize(NormalStream & os) const
126 {
127         os << "[xyarrow ";
128         MathNestInset::normalize(os);
129         os << "]";
130 }