]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSplit.cpp
Display the correct horizontal alignment in AMS environments
[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 void InsetMathSplit::draw(PainterInfo & pi, int x, int y) const
87 {
88         InsetMathGrid::draw(pi, x, y);
89         setPosCache(pi, x, y);
90 }
91
92
93 bool InsetMathSplit::getStatus(Cursor & cur, FuncRequest const & cmd,
94                 FuncStatus & flag) const
95 {
96         switch (cmd.action()) {
97         case LFUN_INSET_MODIFY: {
98                 istringstream is(to_utf8(cmd.argument()));
99                 string s;
100                 is >> s;
101                 if (s != "tabular")
102                         break;
103                 is >> s;
104                 if (s == "add-vline-left" || s == "add-vline-right") {
105                         flag.message(bformat(
106                                 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),       name_));
107                         flag.setEnabled(false);
108                         return true;
109                 }
110                 if (s == "align-left" || s == "align-center" || s == "align-right") {\r
111                         flag.setEnabled(false);\r
112                         return true;\r
113                 }\r
114                 break;
115         }
116         default:
117                 break;
118         }
119         return InsetMathGrid::getStatus(cur, cmd, flag);
120 }
121
122
123 void InsetMathSplit::write(WriteStream & ws) const
124 {
125         MathEnsurer ensurer(ws);
126         if (ws.fragile())
127                 ws << "\\protect";
128         docstring suffix;
129         if (!numbered_ && name_ == "align")
130                 suffix = from_ascii("*");
131         ws << "\\begin{" << name_ << suffix << '}';
132         bool open = ws.startOuterRow();
133         if (name_ != "split" && name_ != "align" && verticalAlignment() != 'c')
134                 ws << '[' << verticalAlignment() << ']';
135         if (name_ == "alignedat")
136                 ws << '{' << static_cast<unsigned int>((ncols() + 1)/2) << '}';
137         InsetMathGrid::write(ws);
138         if (ws.fragile())
139                 ws << "\\protect";
140         ws << "\\end{" << name_ << suffix << "}\n";
141         if (open)
142                 ws.startOuterRow();
143 }
144
145
146 void InsetMathSplit::infoize(odocstream & os) const
147 {
148         docstring name = name_;
149         name[0] = support::uppercase(name[0]);
150         if (name_ == "align" && !numbered_)
151                 os << name << "* ";
152         else
153                 os << name << ' ';
154 }
155
156
157 void InsetMathSplit::mathmlize(MathStream & ms) const
158 {
159         // split, gathered, aligned, alignedat
160         // At the moment, those seem to display just fine without any
161         // special treatment.
162         // FIXME
163         // lgathered and rgathered could use the proper alignment, but
164         // it's not clear how to do that without copying a lot of code.
165         // One idea would be to wrap the table in an <mrow>, and set the
166         // alignment there via CSS.
167         // FIXME how to handle numbered and unnumbered align?
168         InsetMathGrid::mathmlize(ms);
169 }
170
171
172 void InsetMathSplit::htmlize(HtmlStream & ms) const
173 {
174         // split, gathered, aligned, alignedat
175         // At the moment, those seem to display just fine without any
176         // special treatment.
177         // FIXME
178         // lgathered and rgathered could use the proper alignment.
179         // FIXME how to handle numbered and unnumbered align?
180         InsetMathGrid::htmlize(ms);
181 }
182
183
184 void InsetMathSplit::validate(LaTeXFeatures & features) const
185 {
186         if (name_ == "split" || name_ == "gathered" || name_ == "aligned" ||
187             name_ == "alignedat" || name_ == "align")
188                 features.require("amsmath");
189         else if (name_ == "lgathered" || name_ == "rgathered")
190                 features.require("mathtools");
191         InsetMathGrid::validate(features);
192 }
193
194
195 } // namespace lyx