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