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