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