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