]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSplit.cpp
07e78f41a2472713b92225d576e4b459fc79171f
[lyx.git] / src / mathed / InsetMathSplit.cpp
1 /**
2  * \file InsetMathSplit.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 "InsetMathSplit.h"
14
15 #include "MathData.h"
16 #include "MathStream.h"
17 #include "MathStream.h"
18
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "support/gettext.h"
22 #include "LaTeXFeatures.h"
23
24 #include "support/lstrings.h"
25
26 #include <ostream>
27
28 using namespace std;
29
30 namespace lyx {
31
32 using support::bformat;
33
34
35 // FIXME: handle numbers in gui, currently they are only read and written
36
37 InsetMathSplit::InsetMathSplit(Buffer * buf, docstring const & name,
38         char valign, bool numbered)
39         : InsetMathGrid(buf, 1, 1, valign, docstring()), name_(name),
40           numbered_(numbered)
41 {
42 }
43
44
45 Inset * InsetMathSplit::clone() const
46 {
47         return new InsetMathSplit(*this);
48 }
49
50
51 //FIXME: This has probably no effect and can be removed.\r
52 char InsetMathSplit::defaultColAlign(col_type col)
53 {
54         if (name_ == "gathered")
55                 return 'c';
56         if (name_ == "lgathered")\r
57                 return 'l';\r
58         if (name_ == "rgathered")\r
59                 return 'r';\r
60         if (name_ == "split"\r
61             || name_ == "aligned"\r
62             || name_ == "align"\r
63             || name_ == "alignedat")\r
64                 return colAlign(hullAlign, col);\r
65         return 'l';
66 }
67
68
69 char InsetMathSplit::displayColAlign(idx_type idx) const\r
70 {\r
71         if (name_ == "gathered")\r
72                 return 'c';\r
73         if (name_ == "lgathered")\r
74                 return 'l';\r
75         if (name_ == "rgathered")\r
76                 return 'r';\r
77         if (name_ == "split"\r
78             || name_ == "aligned"\r
79             || name_ == "align"\r
80             || name_ == "alignedat")\r
81                 return colAlign(hullAlign, col(idx));\r
82         return InsetMathGrid::displayColAlign(idx);\r
83 }\r
84 \r
85 \r
86 int InsetMathSplit::displayColSpace(col_type col) const\r
87 {\r
88         if (name_ == "split" || name_ == "aligned" || name_ == "align")\r
89                 return colSpace(hullAlign, col);\r
90         if (name_ == "alignedat")\r
91                 return colSpace(hullAlignAt, col);\r
92         return 0;\r
93 }\r
94 \r
95 \r
96 \r
97 void InsetMathSplit::draw(PainterInfo & pi, int x, int y) const
98 {
99         InsetMathGrid::draw(pi, x, y);
100         setPosCache(pi, x, y);
101 }
102
103
104 bool InsetMathSplit::getStatus(Cursor & cur, FuncRequest const & cmd,
105                 FuncStatus & flag) const
106 {
107         switch (cmd.action()) {
108         case LFUN_INSET_MODIFY: {
109                 istringstream is(to_utf8(cmd.argument()));
110                 string s;
111                 is >> s;
112                 if (s != "tabular")
113                         break;
114                 is >> s;
115                 if (s == "add-vline-left" || s == "add-vline-right") {
116                         flag.message(bformat(
117                                 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),       name_));
118                         flag.setEnabled(false);
119                         return true;
120                 }
121                 if (s == "align-left" || s == "align-center" || s == "align-right") {\r
122                         flag.setEnabled(false);\r
123                         return true;\r
124                 }\r
125                 break;
126         }
127         default:
128                 break;
129         }
130         return InsetMathGrid::getStatus(cur, cmd, flag);
131 }
132
133
134 void InsetMathSplit::write(WriteStream & ws) const
135 {
136         MathEnsurer ensurer(ws);
137         if (ws.fragile())
138                 ws << "\\protect";
139         docstring suffix;
140         if (!numbered_ && name_ == "align")
141                 suffix = from_ascii("*");
142         ws << "\\begin{" << name_ << suffix << '}';
143         bool open = ws.startOuterRow();
144         if (name_ != "split" && name_ != "align" && verticalAlignment() != 'c')
145                 ws << '[' << verticalAlignment() << ']';
146         if (name_ == "alignedat")
147                 ws << '{' << static_cast<unsigned int>((ncols() + 1)/2) << '}';
148         InsetMathGrid::write(ws);
149         if (ws.fragile())
150                 ws << "\\protect";
151         ws << "\\end{" << name_ << suffix << "}\n";
152         if (open)
153                 ws.startOuterRow();
154 }
155
156
157 void InsetMathSplit::infoize(odocstream & os) const
158 {
159         docstring name = name_;
160         name[0] = support::uppercase(name[0]);
161         if (name_ == "align" && !numbered_)
162                 os << name << "* ";
163         else
164                 os << name << ' ';
165 }
166
167
168 void InsetMathSplit::mathmlize(MathStream & ms) const
169 {
170         // split, gathered, aligned, alignedat
171         // At the moment, those seem to display just fine without any
172         // special treatment.
173         // FIXME
174         // lgathered and rgathered could use the proper alignment, but
175         // it's not clear how to do that without copying a lot of code.
176         // One idea would be to wrap the table in an <mrow>, and set the
177         // alignment there via CSS.
178         // FIXME how to handle numbered and unnumbered align?
179         InsetMathGrid::mathmlize(ms);
180 }
181
182
183 void InsetMathSplit::htmlize(HtmlStream & ms) const
184 {
185         // split, gathered, aligned, alignedat
186         // At the moment, those seem to display just fine without any
187         // special treatment.
188         // FIXME
189         // lgathered and rgathered could use the proper alignment.
190         // FIXME how to handle numbered and unnumbered align?
191         InsetMathGrid::htmlize(ms);
192 }
193
194
195 void InsetMathSplit::validate(LaTeXFeatures & features) const
196 {
197         if (name_ == "split" || name_ == "gathered" || name_ == "aligned" ||
198             name_ == "alignedat" || name_ == "align")
199                 features.require("amsmath");
200         else if (name_ == "lgathered" || name_ == "rgathered")
201                 features.require("mathtools");
202         InsetMathGrid::validate(features);
203 }
204
205
206 } // namespace lyx