]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathCancel.cpp
Use context-sensitive command termination
[lyx.git] / src / mathed / InsetMathCancel.cpp
1 /**
2  * \file InsetMathCancel.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Uwe Stöhr
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathCancel.h"
14
15 #include "MathStream.h"
16
17 #include "LaTeXFeatures.h"
18 #include "MetricsInfo.h"
19
20 #include "frontends/Painter.h"
21
22 #include <ostream>
23
24 namespace lyx {
25
26
27 InsetMathCancel::InsetMathCancel(Buffer * buf, Kind k)
28         : InsetMathNest(buf, 1), kind_(k)
29 {}
30
31
32 Inset * InsetMathCancel::clone() const
33 {
34         return new InsetMathCancel(*this);
35 }
36
37
38 void InsetMathCancel::metrics(MetricsInfo & mi, Dimension & dim) const
39 {
40         Changer dummy = mi.base.changeEnsureMath();
41         cell(0).metrics(mi, dim);
42         metricsMarkers(mi, dim);
43 }
44
45
46 void InsetMathCancel::draw(PainterInfo & pi, int x, int y) const
47 {
48         Changer dummy = pi.base.changeEnsureMath();
49         // We first draw the text and then an arrow
50         ColorCode const origcol = pi.base.font.color();
51         cell(0).draw(pi, x + 1, y);
52         Dimension const dim = dimension(*pi.base.bv);
53
54         /*
55          * y1 \    /
56          *     \  /
57          *      \/
58          *      /\
59          *     /  \
60          * y2 /    \
61          *    x1  x2
62          */
63
64         int const x2 = x + dim.wid;
65         int const x1 = x;
66         int const y1 = y - dim.asc;
67         int const y2 = y + dim.des;
68
69         if (kind_ == cancel)
70                 pi.pain.line(x2, y1, x1, y2, origcol);
71         else if (kind_ == bcancel)
72                 pi.pain.line(x2, y2, x1, y1, origcol);
73         else if (kind_ == xcancel) {
74                 pi.pain.line(x2, y1, x1, y2, origcol);
75                 pi.pain.line(x2, y2, x1, y1, origcol);
76         }
77
78         drawMarkers(pi, x, y);
79 }
80
81
82 void InsetMathCancel::write(WriteStream & os) const
83 {
84         MathEnsurer ensurer(os);
85         switch (kind_) {
86         case cancel:
87                 os << "\\cancel{";
88                 break;
89         case bcancel:
90                 os << "\\bcancel{";
91                 break;
92         case xcancel:
93                 os << "\\xcancel{";
94                 break;
95         }
96         os << cell(0) << '}';
97 }
98
99
100 void InsetMathCancel::normalize(NormalStream & os) const
101 {
102         switch (kind_) {
103         case cancel:
104                 os << "[cancel ";
105                 break;
106         case bcancel:
107                 os << "[bcancel ";
108                 break;
109         case xcancel:
110                 os << "[xcancel ";
111                 break;
112         }
113         os << cell(0) << ']';
114 }
115
116
117 void InsetMathCancel::infoize(odocstream & os) const
118 {
119         switch (kind_) {
120         case cancel:
121                 os << "Cancel";
122                 break;
123         case bcancel:
124                 os << "Bcancel";
125                 break;
126         case xcancel:
127                 os << "Xcancel";
128                 break;
129         }
130 }
131
132
133 // unfortunately, we do not have many options here, so we have to treat
134 // them all the same way.
135 void InsetMathCancel::htmlize(HtmlStream & os) const
136 {
137         os << MTag("span", "style='text-decoration: line-through;'")
138            << cell(0)
139            << ETag("span");
140 }
141
142
143 void InsetMathCancel::mathmlize(MathStream & os) const
144 {
145         switch (kind_) {
146         case cancel:
147                 os << MTag("menclose", "notation='updiagonalstrike'")
148                    << cell(0)
149                    << ETag("menclose"); 
150                 break;
151         case bcancel:
152                 os << MTag("menclose", "notation='downdiagonalstrike'")
153                    << cell(0)
154                    << ETag("menclose"); 
155                 break;
156         case xcancel:
157                 os << MTag("menclose", "notation='updiagonalstrike'")
158                    << MTag("menclose", "notation='downdiagonalstrike'")
159                    << cell(0)
160                    << ETag("menclose")
161                    << ETag("menclose");
162                 break;
163         }
164 }
165
166
167 void InsetMathCancel::validate(LaTeXFeatures & features) const
168 {
169         InsetMathNest::validate(features);
170         if (features.runparams().isLaTeX())
171                 features.require("cancel");
172         InsetMathNest::validate(features);
173 }
174
175 } // namespace lyx