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