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