]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathCases.cpp
Please Coverity (code should be equivalent)
[lyx.git] / src / mathed / InsetMathCases.cpp
1 /**
2  * \file InsetMathCases.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 "InsetMathCases.h"
14
15 #include "Cursor.h"
16 #include "FuncRequest.h"
17 #include "FuncStatus.h"
18 #include "support/gettext.h"
19 #include "LaTeXFeatures.h"
20 #include "MathData.h"
21 #include "MathStream.h"
22 #include "MathSupport.h"
23 #include "MetricsInfo.h"
24
25 #include "support/lstrings.h"
26
27 #include <ostream>
28
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33
34
35 InsetMathCases::InsetMathCases(Buffer * buf, row_type n)
36         : InsetMathGrid(buf, 2, n, 'c', from_ascii("ll"))
37 {}
38
39
40 Inset * InsetMathCases::clone() const
41 {
42         return new InsetMathCases(*this);
43 }
44
45
46 void InsetMathCases::metrics(MetricsInfo & mi, Dimension & dim) const
47 {
48         InsetMathGrid::metrics(mi, dim);
49         dim.wid += 8;
50 }
51
52
53 Dimension const InsetMathCases::dimension(BufferView const & bv) const
54 {
55         Dimension dim = InsetMathGrid::dimension(bv);
56         dim.wid += 8;
57         return dim;
58 }
59
60
61 void InsetMathCases::draw(PainterInfo & pi, int x, int y) const
62 {
63         Dimension const dim = dimension(*pi.base.bv);
64         mathed_draw_deco(pi, x + 1, y - dim.ascent(), 6, dim.height(), from_ascii("{"));
65         InsetMathGrid::drawWithMargin(pi, x, y, 8, 0);
66         setPosCache(pi, x, y);
67 }
68
69
70 void InsetMathCases::doDispatch(Cursor & cur, FuncRequest & cmd)
71 {
72         //lyxerr << "*** InsetMathCases: request: " << cmd << endl;
73         switch (cmd.action()) {
74         case LFUN_TABULAR_FEATURE: {
75                 string s = cmd.getArg(0);
76                 // vertical lines and adding/deleting columns is not allowed for \cases
77                 // FIXME: "I suspect that the break after cur.undispatched() should be a
78                 // return; the recordUndo seems bogus too." (lasgouttes)
79                 if (s == "append-column" || s == "delete-column"
80                     || s == "add-vline-left" || s == "add-vline-right") {
81                         cur.undispatched();
82                         break;
83                 }
84                 cur.recordUndo();
85         }
86         default:
87                 break;
88         }
89         InsetMathGrid::doDispatch(cur, cmd);
90 }
91
92
93 bool InsetMathCases::getStatus(Cursor & cur, FuncRequest const & cmd,
94                 FuncStatus & flag) const
95 {
96         switch (cmd.action()) {
97         case LFUN_TABULAR_FEATURE: {
98                 string s = cmd.getArg(0);
99                 if (s == "add-vline-left" || s == "add-vline-right") {
100                         flag.setEnabled(false);
101                         flag.message(bformat(
102                                 from_utf8(N_("No vertical grid lines in 'cases': feature %1$s")),
103                                 from_utf8(s)));
104                         return true;
105                 }
106                 if (s == "append-column" || s == "delete-column") {
107                         flag.setEnabled(false);
108                         flag.message(bformat(
109                                 from_utf8(N_("Changing number of columns not allowed in "
110                                              "'cases': feature %1$s")), from_utf8(s)));
111                         return true;
112                 }
113                 break;
114         }
115         default:
116                 break;
117         }
118         return InsetMathGrid::getStatus(cur, cmd, flag);
119 }
120
121
122 void InsetMathCases::write(WriteStream & os) const
123 {
124         MathEnsurer ensurer(os);
125         if (os.fragile())
126                 os << "\\protect";
127         bool open = os.startOuterRow();
128         os << "\\begin{cases}\n";
129         InsetMathGrid::write(os);
130         if (os.fragile())
131                 os << "\\protect";
132         os << "\\end{cases}";
133         if (open)
134                 os.startOuterRow();
135 }
136
137
138 void InsetMathCases::normalize(NormalStream & os) const
139 {
140         os << "[cases ";
141         InsetMathGrid::normalize(os);
142         os << ']';
143 }
144
145
146 void InsetMathCases::maple(MapleStream & os) const
147 {
148         os << "cases(";
149         InsetMathGrid::maple(os);
150         os << ')';
151 }
152
153
154 void InsetMathCases::mathmlize(MathStream & ms) const
155 {
156         ms << "<mo form='prefix' fence='true' stretchy='true' symmetric='true'>{</mo>";
157         InsetMathGrid::mathmlize(ms);
158 }
159
160
161 // FIXME XHTML
162 // We need a brace here, somehow.
163 void InsetMathCases::htmlize(HtmlStream & ms) const
164 {
165         InsetMathGrid::htmlize(ms, "class='cases'");
166 }
167
168
169 void InsetMathCases::infoize(odocstream & os) const
170 {
171         os << "Cases ";
172 }
173
174
175 void InsetMathCases::validate(LaTeXFeatures & features) const
176 {
177         features.require("amsmath");
178         InsetMathGrid::validate(features);
179         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
180                 // CSS based on eLyXer's
181                 features.addCSSSnippet(
182                         "table.cases{display: inline-block; text-align: center;"
183                         "border-left: thin solid black; vertical-align: middle; padding-left: 0.5ex;}\n"
184                         "table.cases td {text-align: left;}");
185 }
186
187
188 int InsetMathCases::displayColSpace(col_type) const
189 {
190         return 20;
191 }
192
193
194 } // namespace lyx