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