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