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