]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathXArrow.cpp
When selecting math with the mouse, selection happens in the middle (#9748)
[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 }
56
57
58 void InsetMathXArrow::draw(PainterInfo & pi, int x, int y) const
59 {
60         Changer dummy2 = pi.base.changeEnsureMath();
61         Changer dummy = pi.base.changeScript();
62         Dimension const dim = dimension(*pi.base.bv);
63         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
64         // center the cells with the decoration
65         cell(0).draw(pi, x + dim.width()/2 - dim0.width()/2, y - 10);
66         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
67         cell(1).draw(pi, x + dim.width()/2 - dim1.width()/2, y + dim1.height());
68         mathed_draw_deco(pi, x, y - 7, dim.wid, 5, name_);
69 }
70
71
72 void InsetMathXArrow::write(WriteStream & os) const
73 {
74         MathEnsurer ensurer(os);
75         os << '\\' << name_;
76         if (!cell(1).empty())
77                 os << '[' << cell(1) << ']';
78         os << '{' << cell(0) << '}';
79 }
80
81
82 void InsetMathXArrow::normalize(NormalStream & os) const
83 {
84         os << "[xarrow " << name_ << ' ' <<  cell(0) << ' ' << cell(1) << ']';
85 }
86
87
88 void InsetMathXArrow::mathmlize(MathStream & ms) const
89 {
90         char const * arrow;
91         
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         ms << "<munderover accent='false' accentunder='false'>"
127            << arrow << cell(1) << cell(0)
128            << "</munderover>";
129 }
130
131
132 void InsetMathXArrow::htmlize(HtmlStream & os) const
133 {
134         char const * arrow;
135
136         if (name_ == "xleftarrow")
137                 arrow = "&larr;";
138         else if (name_ == "xrightarrow")
139                 arrow = "&rarr;";
140         else if (name_ == "xhookleftarrow")
141                 arrow = "&larrhk;";
142         else if (name_ == "xhookrightarrow")
143                 arrow = "&rarrhk;";
144         else if (name_ == "xLeftarrow")
145                 arrow = "&lArr;";
146         else if (name_ == "xRightarrow")
147                 arrow = "&rArr;";
148         else if (name_ == "xleftrightarrow")
149                 arrow = "&leftrightarrow;";
150         else if (name_ == "xLeftrightarrow")
151                 arrow = "&Leftrightarrow;";
152         else if (name_ == "xleftharpoondown")
153                 arrow = "&leftharpoondown;";
154         else if (name_ == "xleftharpoonup")
155                 arrow = "&leftharpoonup;";
156         else if (name_ == "xleftrightharpoons")
157                 arrow = "&leftrightharpoons;";
158         else if (name_ == "xrightharpoondown")
159                 arrow = "&rightharpoondown;";
160         else if (name_ == "xrightharpoonup")
161                 arrow = "&rightharpoonup;";
162         else if (name_ == "xrightleftharpoons")
163                 arrow = "&rightleftharpoons;";
164         else if (name_ == "xmapsto")
165                 arrow = "&mapsto;";
166         else {
167                 lyxerr << "htmlize conversion for '" << name_ << "' not implemented" << endl;
168                 LASSERT(false, arrow = "&rarr;");
169         }
170         os << MTag("span", "class='xarrow'")
171                  << MTag("span", "class='xatop'") << cell(0) << ETag("span")
172                  << MTag("span", "class='xabottom'") << arrow << ETag("span")
173                  << ETag("span");
174 }
175
176
177 void InsetMathXArrow::validate(LaTeXFeatures & features) const
178 {
179         if (name_ == "xleftarrow" || name_ == "xrightarrow")
180                 features.require("amsmath");
181         else
182                 features.require("mathtools");
183         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
184                 // CSS adapted from eLyXer
185                 features.addCSSSnippet(
186                         "span.xarrow{display: inline-block; vertical-align: middle; text-align:center;}\n"
187                         "span.xatop{display: block;}\n"
188                         "span.xabottom{display: block;}");
189         InsetMathNest::validate(features);
190 }
191
192
193 } // namespace lyx