]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathXArrow.cpp
Move several common types to support/types.h
[lyx.git] / src / mathed / InsetMathXArrow.cpp
1 /**
2  * \file InsetMathXArrow.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 "InsetMathXArrow.h"
14
15 #include "MathData.h"
16 #include "MathStream.h"
17 #include "MathSupport.h"
18
19 #include "LaTeXFeatures.h"
20 #include "MetricsInfo.h"
21
22 #include "support/debug.h"
23 #include "support/lassert.h"
24
25 #include <algorithm>
26
27 using namespace std;
28
29 namespace lyx {
30
31
32 InsetMathXArrow::InsetMathXArrow(Buffer * buf, docstring const & name)
33         : InsetMathFracBase(buf), name_(name)
34 {}
35
36
37 Inset * InsetMathXArrow::clone() const
38 {
39         return new InsetMathXArrow(*this);
40 }
41
42
43 void InsetMathXArrow::metrics(MetricsInfo & mi, Dimension & dim) const
44 {
45         Changer dummy2 = mi.base.changeEnsureMath();
46         Changer dummy = mi.base.changeScript();
47         Dimension dim0;
48         cell(0).metrics(mi, dim0);
49         Dimension dim1;
50         cell(1).metrics(mi, dim1);
51         dim.wid = max(dim0.width(), dim1.width()) + 10;
52         dim.asc = dim0.height() + 10;
53         dim.des = dim1.height();
54 }
55
56
57 void InsetMathXArrow::draw(PainterInfo & pi, int x, int y) const
58 {
59         Changer dummy2 = pi.base.changeEnsureMath();
60         Changer dummy = pi.base.changeScript();
61         Dimension const dim = dimension(*pi.base.bv);
62         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
63         // center the cells with the decoration
64         cell(0).draw(pi, x + dim.width()/2 - dim0.width()/2, y - 10);
65         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
66         cell(1).draw(pi, x + dim.width()/2 - dim1.width()/2, y + dim1.height());
67         mathed_draw_deco(pi, x, y - 7, dim.wid, 5, name_);
68 }
69
70
71 void InsetMathXArrow::write(WriteStream & os) const
72 {
73         MathEnsurer ensurer(os);
74         os << '\\' << name_;
75         if (!cell(1).empty())
76                 os << '[' << cell(1) << ']';
77         os << '{' << cell(0) << '}';
78 }
79
80
81 void InsetMathXArrow::normalize(NormalStream & os) const
82 {
83         os << "[xarrow " << name_ << ' ' <<  cell(0) << ' ' << cell(1) << ']';
84 }
85
86
87 void InsetMathXArrow::mathmlize(MathStream & ms) const
88 {
89         char const * arrow;
90
91         if (!ms.xmlMode()) { // Use HTML entities.
92                 if (name_ == "xleftarrow")
93                         arrow = "&larr;";
94                 else if (name_ == "xrightarrow")
95                         arrow = "&rarr;";
96                 else if (name_ == "xhookleftarrow")
97                         arrow = "&larrhk;";
98                 else if (name_ == "xhookrightarrow")
99                         arrow = "&rarrhk;";
100                 else if (name_ == "xLeftarrow")
101                         arrow = "&lArr;";
102                 else if (name_ == "xRightarrow")
103                         arrow = "&rArr;";
104                 else if (name_ == "xleftrightarrow")
105                         arrow = "&leftrightarrow;";
106                 else if (name_ == "xLeftrightarrow")
107                         arrow = "&Leftrightarrow;";
108                 else if (name_ == "xleftharpoondown")
109                         arrow = "&leftharpoondown;";
110                 else if (name_ == "xleftharpoonup")
111                         arrow = "&leftharpoonup;";
112                 else if (name_ == "xleftrightharpoons")
113                         arrow = "&leftrightharpoons;";
114                 else if (name_ == "xrightharpoondown")
115                         arrow = "&rightharpoondown;";
116                 else if (name_ == "xrightharpoonup")
117                         arrow = "&rightharpoonup;";
118                 else if (name_ == "xrightleftharpoons")
119                         arrow = "&rightleftharpoons;";
120                 else if (name_ == "xmapsto")
121                         arrow = "&mapsto;";
122                 else {
123                         lyxerr << "mathmlize conversion for '" << name_ << "' not implemented" << endl;
124                         LASSERT(false, arrow = "&rarr;");
125                 }
126         } else { // Use XML entities.
127                 if (name_ == "xleftarrow")
128                         arrow = "&#x2190;";
129                 else if (name_ == "xrightarrow")
130                         arrow = "&#x2192;";
131                 else if (name_ == "xhookleftarrow")
132                         arrow = "&#x21a9;";
133                 else if (name_ == "xhookrightarrow")
134                         arrow = "&#x21aa;";
135                 else if (name_ == "xLeftarrow")
136                         arrow = "&#x21d0;";
137                 else if (name_ == "xRightarrow")
138                         arrow = "&#x21d2;";
139                 else if (name_ == "xleftrightarrow")
140                         arrow = "&#x2194;";
141                 else if (name_ == "xLeftrightarrow")
142                         arrow = "&#x21d4;";
143                 else if (name_ == "xleftharpoondown")
144                         arrow = "&#x21bd;";
145                 else if (name_ == "xleftharpoonup")
146                         arrow = "&#x21bc;";
147                 else if (name_ == "xleftrightharpoons")
148                         arrow = "&#x21cb;";
149                 else if (name_ == "xrightharpoondown")
150                         arrow = "&#x21c1;";
151                 else if (name_ == "xrightharpoonup")
152                         arrow = "&#x21c0;";
153                 else if (name_ == "xrightleftharpoons")
154                         arrow = "&#x21cc;";
155                 else if (name_ == "xmapsto")
156                         arrow = "&#x21a6;";
157                 else {
158                         lyxerr << "mathmlize XML conversion for '" << name_ << "' not implemented" << endl;
159                         LASSERT(false, arrow = "&#x2192;");
160                 }
161         }
162
163         ms << "<" << from_ascii(ms.namespacedTag("munderover")) << " accent='false' accentunder='false'>"
164            << arrow << cell(1) << cell(0)
165            << "</" << from_ascii(ms.namespacedTag("munderover"))<< ">";
166 }
167
168
169 void InsetMathXArrow::htmlize(HtmlStream & os) const
170 {
171         char const * arrow;
172
173         if (name_ == "xleftarrow")
174                 arrow = "&larr;";
175         else if (name_ == "xrightarrow")
176                 arrow = "&rarr;";
177         else if (name_ == "xhookleftarrow")
178                 arrow = "&larrhk;";
179         else if (name_ == "xhookrightarrow")
180                 arrow = "&rarrhk;";
181         else if (name_ == "xLeftarrow")
182                 arrow = "&lArr;";
183         else if (name_ == "xRightarrow")
184                 arrow = "&rArr;";
185         else if (name_ == "xleftrightarrow")
186                 arrow = "&leftrightarrow;";
187         else if (name_ == "xLeftrightarrow")
188                 arrow = "&Leftrightarrow;";
189         else if (name_ == "xleftharpoondown")
190                 arrow = "&leftharpoondown;";
191         else if (name_ == "xleftharpoonup")
192                 arrow = "&leftharpoonup;";
193         else if (name_ == "xleftrightharpoons")
194                 arrow = "&leftrightharpoons;";
195         else if (name_ == "xrightharpoondown")
196                 arrow = "&rightharpoondown;";
197         else if (name_ == "xrightharpoonup")
198                 arrow = "&rightharpoonup;";
199         else if (name_ == "xrightleftharpoons")
200                 arrow = "&rightleftharpoons;";
201         else if (name_ == "xmapsto")
202                 arrow = "&mapsto;";
203         else {
204                 lyxerr << "htmlize conversion for '" << name_ << "' not implemented" << endl;
205                 LASSERT(false, arrow = "&rarr;");
206         }
207         os << MTag("span", "class='xarrow'")
208                  << MTag("span", "class='xatop'") << cell(0) << ETag("span")
209                  << MTag("span", "class='xabottom'") << arrow << ETag("span")
210                  << ETag("span");
211 }
212
213
214 void InsetMathXArrow::validate(LaTeXFeatures & features) const
215 {
216         if (name_ == "xleftarrow" || name_ == "xrightarrow")
217                 features.require("amsmath");
218         else
219                 features.require("mathtools");
220         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
221                 // CSS adapted from eLyXer
222                 features.addCSSSnippet(
223                         "span.xarrow{display: inline-block; vertical-align: middle; text-align:center;}\n"
224                         "span.xatop{display: block;}\n"
225                         "span.xabottom{display: block;}");
226         InsetMathNest::validate(features);
227 }
228
229
230 } // namespace lyx