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