]> git.lyx.org Git - lyx.git/blob - src/mathed/math_xyarrowinset.C
using previews might be a good alternative to hard coding such diagrams...
[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 "frontends/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 #if 0   
39         MathXYMatrixInset const * p = targetMatrix();
40         int x = 0;
41         int y = 0;
42         MathArray const & t = cell(0);
43         for (MathArray::const_iterator it = t.begin(); it != t.end(); ++it) {
44                 switch ((*it)->getChar()) {
45                         case 'l': --x; break;
46                         case 'r': ++x; break;
47                         case 'u': --y; break;
48                         case 'd': ++y; break;
49                 }
50         }
51         //lyxerr << "target: x: " << x << " y: " << y << "\n";
52         MathInset::idx_type n = mi_.idx + p->ncols() * y + x;
53         if (n >= p->nargs()) {
54                 lyxerr << "source: n: " << mi_.idx << "\n";
55                 lyxerr << "target: n: " << n << " out of range\n";
56                 n = 0;
57         }
58   return p->xcell(n);
59 #else
60         static MathXArray dummy;
61         return dummy;
62 #endif
63 }
64
65
66 MathXArray const & MathXYArrowInset::sourceCell() const
67 {
68 #if 0
69   return targetMatrix()->xcell(mi_.idx);
70 #else
71         static MathXArray dummy;
72         return dummy;
73 #endif
74 }
75
76
77 void MathXYArrowInset::metrics(MathMetricsInfo & mi) const
78 {
79         MathNestInset::metrics(mi);
80         mi_   = mi;
81         MathFontSetChanger dummy(mi.base, "textrm");
82 #if 0
83         target_ = mi.inset ? mi.inset->asXYMatrixInset() : 0;
84
85         if (editing()) {
86                 int w    = mathed_string_width(mi.base.font, "target: ");
87                 width_   = w + max(xcell(0).width(), xcell(1).width());
88                 ascent_  = xcell(0).ascent();
89                 descent_ = xcell(0).descent() + xcell(1).height() + 10;
90         } else {
91                 width_   = 0;
92                 ascent_  = 0;
93                 descent_ = 0;
94                 //mathed_string_dim(font_, "X", ascent_, descent_, width_);
95         }
96 #endif
97 }
98
99
100 void MathXYArrowInset::draw(MathPainterInfo & pi, int x, int y) const
101 {
102         metrics(mi_);
103         MathFontSetChanger dummy(pi.base, "textrm");
104
105         if (editing()) {
106
107 #if 0
108
109                 int lasc;
110                 int ldes;
111                 int lwid;
112                 mathed_string_dim(pi.base.font, "target: ", lasc, ldes, lwid);
113
114                 xcell(0).draw(pi, x + lwid, y);
115                 drawStr(pi, pi.base.font, x + 3, y, "target");
116                 y += max(xcell(0).descent(), ldes) + 5;
117
118                 y += max(xcell(1).ascent(), lasc) + 5;
119                 xcell(1).draw(pi, x + lwid, y);
120                 drawStr(pi, pi.base.font, x + 3, y, "label");
121
122 #endif
123
124         } else {
125
126                 drawStr(pi, font_, x, y, "X");
127                 MathXArray const & s = sourceCell();
128                 MathXArray const & t = targetCell();
129                 pi.pain.line(s.xm(), s.ym(), t.xm(), t.ym(), LColor::math);
130                 xcell(1).draw(pi, (s.xm() + t.xm())/2, (s.ym() + t.ym())/2);
131
132         }
133 }
134
135
136 void MathXYArrowInset::write(WriteStream & os) const
137 {
138         os << "\\ar";
139         if (cell(0).size())
140                 os << "[" << cell(0) << "]";
141         if (cell(1).size())
142                 os << (up_ ? "^" : "_") << "{" << cell(1) << "}";
143         os << " ";
144 }
145
146
147 void MathXYArrowInset::normalize(NormalStream & os) const
148 {
149         os << "[xyarrow ";
150         MathNestInset::normalize(os);
151         os << "]";
152 }