]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathXYArrow.cpp
use bald pointers in clone()
[lyx.git] / src / mathed / InsetMathXYArrow.cpp
1 /**
2  * \file InsetMathXYArrow.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathXYArrow.h"
14 #include "MathStream.h"
15 #include "MathSupport.h"
16 #include "frontends/Painter.h"
17 #include "debug.h"
18
19
20 namespace lyx {
21
22
23 using std::max;
24
25
26 InsetMathXYArrow::InsetMathXYArrow()
27         : InsetMathNest(2)
28 {}
29
30
31 Inset * InsetMathXYArrow::clone() const
32 {
33         return new InsetMathXYArrow(*this);
34 }
35
36
37 InsetMathXYMatrix const * InsetMathXYArrow::targetMatrix() const
38 {
39         return target_;
40 }
41
42
43 MathData const & InsetMathXYArrow::targetCell() const
44 {
45 #if 0
46         InsetMathXYMatrix const * p = targetMatrix();
47         int x = 0;
48         int y = 0;
49         MathData const & t = cell(0);
50         for (MathData::const_iterator it = t.begin(); it != t.end(); ++it) {
51                 switch ((*it)->getChar()) {
52                         case 'l': --x; break;
53                         case 'r': ++x; break;
54                         case 'u': --y; break;
55                         case 'd': ++y; break;
56                 }
57         }
58         //lyxerr << "target: x: " << x << " y: " << y << endl;
59         InsetMath::idx_type n = mi_.idx + p->ncols() * y + x;
60         if (n >= p->nargs()) {
61                 lyxerr << "source: n: " << mi_.idx << "\n"
62                        << "target: n: " << n << " out of range" << endl;
63                 n = 0;
64         }
65         return p->cell(n);
66 #else
67         static MathData dummy;
68         return dummy;
69 #endif
70 }
71
72
73 MathData const & InsetMathXYArrow::sourceCell() const
74 {
75 #if 0
76         return targetMatrix()->cell(mi_.idx);
77 #else
78         static MathData dummy;
79         return dummy;
80 #endif
81 }
82
83
84 bool InsetMathXYArrow::metrics(MetricsInfo & mi) const
85 {
86         InsetMathNest::metrics(mi);
87         mi_   = mi;
88         FontSetChanger dummy(mi.base, "textrm");
89 #if 0
90         target_ = mi.inset ? mi.inset->asXYMatrixInset() : 0;
91
92         if (editing()) {
93                 int w    = mathed_string_width(mi.base.font, from_ascii("target: "));
94                 width_   = w + max(cell(0).width(), cell(1).width());
95                 ascent_  = cell(0).ascent();
96                 descent_ = cell(0).descent() + cell(1).height() + 10;
97         } else {
98                 width_   = 0;
99                 ascent_  = 0;
100                 descent_ = 0;
101                 //mathed_string_dim(font_, "X", ascent_, descent_, width_);
102         }
103 #endif
104 }
105
106
107 void InsetMathXYArrow::draw(PainterInfo & pi, int x, int y) const
108 {
109         metrics(mi_);
110         FontSetChanger dummy(pi.base, "textrm");
111
112         if (editing()) {
113
114 #if 0
115
116                 int lasc;
117                 int ldes;
118                 int lwid;
119                 mathed_string_dim(pi.base.font, "target: ", lasc, ldes, lwid);
120
121                 cell(0).draw(pi, x + lwid, y);
122                 pi.base.text(x + 3, y, "target");
123                 y += max(cell(0).descent(), ldes) + 5;
124
125                 y += max(cell(1).ascent(), lasc) + 5;
126                 cell(1).draw(pi, x + lwid, y);
127                 pi.base.text(x + 3, y, "label");
128
129 #endif
130
131         } else {
132
133                 pi.pain.text(x, y, "X");
134                 MathData const & s = sourceCell();
135                 MathData const & t = targetCell();
136                 pi.pain.line(s.xm(), s.ym(), t.xm(), t.ym(), Color::math);
137                 cell(1).draw(pi, (s.xm() + t.xm())/2, (s.ym() + t.ym())/2);
138
139         }
140 }
141
142
143 void InsetMathXYArrow::write(WriteStream & os) const
144 {
145         os << "\\ar";
146         if (cell(0).size())
147                 os << '[' << cell(0) << ']';
148         if (cell(1).size())
149                 os << (up_ ? '^' : '_') << '{' << cell(1) << '}';
150         os << " ";
151 }
152
153
154 void InsetMathXYArrow::normalize(NormalStream & os) const
155 {
156         os << "[xyarrow ";
157         InsetMathNest::normalize(os);
158         os << ']';
159 }
160
161
162 } // namespace lyx