]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathCases.cpp
Merge branch 'master' of git.lyx.org:lyx
[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_INSET_MODIFY: {
75                 istringstream is(to_utf8(cmd.argument()));
76                 string s;
77                 is >> s;
78                 if (s != "tabular")
79                         break;
80                 is >> s;
81                 // vertical lines and adding/deleting columns is not allowed for \cases
82                 if (s == "append-column" || s == "delete-column"
83                     || s == "add-vline-left" || s == "add-vline-right") {
84                         cur.undispatched();
85                         break;
86                 }
87                 cur.recordUndo();
88         }
89         default:
90                 break;
91         }
92         InsetMathGrid::doDispatch(cur, cmd);
93 }
94
95
96 bool InsetMathCases::getStatus(Cursor & cur, FuncRequest const & cmd,
97                 FuncStatus & flag) const
98 {
99         switch (cmd.action()) {
100         case LFUN_INSET_MODIFY: {
101                 istringstream is(to_utf8(cmd.argument()));
102                 string s;
103                 is >> s;
104                 if (s != "tabular")
105                         break;
106                 is >> s;
107                 if (s == "add-vline-left" || s == "add-vline-right") {
108                         flag.setEnabled(false);
109                         flag.message(bformat(
110                                 from_utf8(N_("No vertical grid lines in 'cases': feature %1$s")),
111                                 from_utf8(s)));
112                         return true;
113                 }
114                 if (s == "append-column" || s == "delete-column") {
115                         flag.setEnabled(false);
116                         flag.message(bformat(
117                                 from_utf8(N_("Changing number of columns not allowed in "
118                                              "'cases': feature %1$s")), from_utf8(s)));
119                         return true;
120                 }
121                 break;
122         }
123         default:
124                 break;
125         }
126         return InsetMathGrid::getStatus(cur, cmd, flag);
127 }
128
129
130 void InsetMathCases::write(WriteStream & os) const
131 {
132         MathEnsurer ensurer(os);
133         if (os.fragile())
134                 os << "\\protect";
135         os << "\\begin{cases}\n";
136         InsetMathGrid::write(os);
137         if (os.fragile())
138                 os << "\\protect";
139         os << "\\end{cases}";
140 }
141
142
143 void InsetMathCases::normalize(NormalStream & os) const
144 {
145         os << "[cases ";
146         InsetMathGrid::normalize(os);
147         os << ']';
148 }
149
150
151 void InsetMathCases::maple(MapleStream & os) const
152 {
153         os << "cases(";
154         InsetMathGrid::maple(os);
155         os << ')';
156 }
157
158
159 void InsetMathCases::mathmlize(MathStream & ms) const
160 {
161         ms << "<mo form='prefix' fence='true' stretchy='true' symmetric='true'>{</mo>";
162         InsetMathGrid::mathmlize(ms);
163 }
164
165
166 // FIXME XHTML
167 // We need a brace here, somehow.
168 void InsetMathCases::htmlize(HtmlStream & ms) const
169 {
170         InsetMathGrid::htmlize(ms, "class='cases'");
171 }
172
173
174 void InsetMathCases::infoize(odocstream & os) const
175 {
176         os << "Cases ";
177 }
178
179
180 void InsetMathCases::validate(LaTeXFeatures & features) const
181 {
182         features.require("amsmath");
183         InsetMathGrid::validate(features);
184         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
185                 // CSS based on eLyXer's
186                 features.addCSSSnippet(
187                         "table.cases{display: inline-block; text-align: center;"
188                         "border-left: thin solid black; vertical-align: middle; padding-left: 0.5ex;}\n"
189                         "table.cases td {text-align: left;}");
190 }
191
192
193 } // namespace lyx