2 * \file InsetMathSpace.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathSpace.h"
15 #include "MathFactory.h"
16 #include "MathStream.h"
17 #include "MathSupport.h"
19 #include "BufferView.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "LaTeXFeatures.h"
25 #include "insets/InsetSpace.h"
27 #include "frontends/Application.h"
28 #include "frontends/Painter.h"
30 #include "support/lassert.h"
41 InsetSpaceParams::Kind kind;
45 bool escape; ///< whether a backslash needs to be added for writing
48 SpaceInfo space_info[] = {
49 // name width kind negative visible custom escape
50 {"!", 6, InsetSpaceParams::NEGTHIN, true, true, false, true},
51 {"negthinspace", 6, InsetSpaceParams::NEGTHIN, true, true, false, true},
52 {"negmedspace", 8, InsetSpaceParams::NEGMEDIUM, true, true, false, true},
53 {"negthickspace", 10, InsetSpaceParams::NEGTHICK, true, true, false, true},
54 {",", 6, InsetSpaceParams::THIN, false, true, false, true},
55 {"thinspace", 6, InsetSpaceParams::THIN, false, true, false, true},
56 {":", 8, InsetSpaceParams::MEDIUM, false, true, false, true},
57 {"medspace", 8, InsetSpaceParams::MEDIUM, false, true, false, true},
58 {";", 10, InsetSpaceParams::THICK, false, true, false, true},
59 {"thickspace", 10, InsetSpaceParams::THICK, false, true, false, true},
60 {"enskip", 10, InsetSpaceParams::ENSKIP, false, true, false, true},
61 {"enspace", 10, InsetSpaceParams::ENSPACE, false, true, false, true},
62 {"quad", 20, InsetSpaceParams::QUAD, false, true, false, true},
63 {"qquad", 40, InsetSpaceParams::QQUAD, false, true, false, true},
64 {"lyxnegspace", -2, InsetSpaceParams::NEGTHIN, true, false, false, true},
65 {"lyxposspace", 2, InsetSpaceParams::THIN, false, false, false, true},
66 {"hfill", 80, InsetSpaceParams::HFILL, false, true, false, true},
67 {"hspace*{\\fill}", 80, InsetSpaceParams::HFILL_PROTECTED, false, true, false, true},
68 {"hspace*", 0, InsetSpaceParams::CUSTOM_PROTECTED,false, true, true, true},
69 {"hspace", 0, InsetSpaceParams::CUSTOM, false, true, true, true},
70 {" ", 10, InsetSpaceParams::NORMAL, false, true, false, true},
71 {"~", 10, InsetSpaceParams::PROTECTED, false, true, false, false},
74 int const nSpace = sizeof(space_info)/sizeof(SpaceInfo);
75 int const defaultSpace = 4;
79 InsetMathSpace::InsetMathSpace()
80 : space_(defaultSpace)
85 InsetMathSpace::InsetMathSpace(string const & name, string const & length)
86 : space_(defaultSpace)
88 for (int i = 0; i < nSpace; ++i)
89 if (space_info[i].name == name) {
93 if (space_info[space_].custom) {
94 length_ = Length(length);
95 if (length_.zero() || length_.empty()) {
97 length_.unit(Length::EM);
103 InsetMathSpace::InsetMathSpace(Length const & length, bool const prot)
104 : space_(defaultSpace), length_(length)
106 for (int i = 0; i < nSpace; ++i)
107 if ((prot && space_info[i].name == "hspace*")
108 || (!prot && space_info[i].name == "hspace")) {
115 Inset * InsetMathSpace::clone() const
117 return new InsetMathSpace(*this);
121 void InsetMathSpace::metrics(MetricsInfo & mi, Dimension & dim) const
125 if (space_info[space_].custom)
126 dim.wid = abs(length_.inPixels(mi.base));
128 dim.wid = space_info[space_].width;
132 void InsetMathSpace::draw(PainterInfo & pi, int x, int y) const
134 // Sadly, HP-UX CC can't handle that kind of initialization.
135 // XPoint p[4] = {{++x, y-3}, {x, y}, {x+width-2, y}, {x+width-2, y-3}};
136 if (!space_info[space_].visible)
139 Dimension const dim = dimension(*pi.base.bv);
144 xp[0] = ++x; yp[0] = y - 3;
145 xp[1] = x; yp[1] = y;
146 xp[2] = x + w - 2; yp[2] = y;
147 xp[3] = x + w - 2; yp[3] = y - 3;
149 pi.pain.lines(xp, yp, 4,
150 space_info[space_].custom ?
152 (isNegative() ? Color_latex : Color_math));
156 void InsetMathSpace::incSpace()
158 int const oldwidth = space_info[space_].width;
160 space_ = (space_ + 1) % nSpace;
161 while ((space_info[space_].width == oldwidth && !space_info[space_].custom) ||
162 !space_info[space_].visible);
163 if (space_info[space_].custom && (length_.zero() || length_.empty())) {
165 length_.unit(Length::EM);
170 void InsetMathSpace::validate(LaTeXFeatures & features) const
172 if (space_info[space_].name == "negmedspace" ||
173 space_info[space_].name == "negthickspace")
174 features.require("amsmath");
178 void InsetMathSpace::maple(MapleStream & os) const
183 void InsetMathSpace::mathematica(MathematicaStream & os) const
189 void InsetMathSpace::octave(OctaveStream & os) const
195 void InsetMathSpace::mathmlize(MathStream & ms) const
197 SpaceInfo const & si = space_info[space_];
198 if (si.negative || !si.visible)
202 l = length_.asHTMLString();
203 else if (si.kind != InsetSpaceParams::MEDIUM) {
211 ms << " width=\"" << from_ascii(l) << "\"";
216 void InsetMathSpace::htmlize(HtmlStream & ms) const
218 SpaceInfo const & si = space_info[space_];
220 case InsetSpaceParams::THIN:
221 ms << from_ascii(" ");
223 case InsetSpaceParams::MEDIUM:
224 ms << from_ascii(" ");
226 case InsetSpaceParams::THICK:
227 ms << from_ascii(" ");
229 case InsetSpaceParams::ENSKIP:
230 case InsetSpaceParams::ENSPACE:
231 ms << from_ascii(" ");
233 case InsetSpaceParams::QUAD:
234 ms << from_ascii(" ");
236 case InsetSpaceParams::QQUAD:
237 ms << from_ascii("  ");
239 case InsetSpaceParams::HFILL:
240 case InsetSpaceParams::HFILL_PROTECTED:
241 // FIXME: is there a useful HTML entity?
243 case InsetSpaceParams::CUSTOM:
244 case InsetSpaceParams::CUSTOM_PROTECTED: {
245 string l = length_.asHTMLString();
246 ms << MTag("span", "width='" + l + "'")
247 << from_ascii(" ") << ETag("span");
250 case InsetSpaceParams::NORMAL:
251 case InsetSpaceParams::PROTECTED:
252 ms << from_ascii(" ");
260 void InsetMathSpace::normalize(NormalStream & os) const
262 os << "[space " << int(space_) << "] ";
266 void InsetMathSpace::write(WriteStream & os) const
268 // no MathEnsurer - all kinds work in text and math mode
269 if (space_info[space_].escape)
271 os << space_info[space_].name.c_str();
272 if (space_info[space_].custom)
273 os << '{' << length_.asLatexString().c_str() << '}';
274 else if (space_info[space_].escape && space_info[space_].name.length() > 1)
275 os.pendingSpace(true);
279 InsetSpaceParams InsetMathSpace::params() const
281 InsetSpaceParams isp(true);
282 LASSERT(space_info[space_].visible, return isp);
283 isp.kind = space_info[space_].kind;
284 isp.length = GlueLength(length_);
289 string InsetMathSpace::contextMenuName() const
291 return "context-mathspace";
295 bool InsetMathSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
296 FuncStatus & status) const
298 switch (cmd.action()) {
300 case LFUN_INSET_MODIFY:
301 case LFUN_INSET_DIALOG_UPDATE:
302 case LFUN_MOUSE_RELEASE:
303 case LFUN_MOUSE_PRESS:
304 case LFUN_MOUSE_MOTION:
305 status.setEnabled(true);
308 bool retval = InsetMath::getStatus(cur, cmd, status);
314 void InsetMathSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
316 switch (cmd.action()) {
317 case LFUN_INSET_MODIFY:
318 if (cmd.getArg(0) == "mathspace") {
320 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
322 *this = *ar[0].nucleus()->asSpaceInset();
329 case LFUN_MOUSE_RELEASE:
330 if (cmd.button() == mouse_button::button1) {
331 showInsetDialog(&cur.bv());
337 case LFUN_MOUSE_PRESS:
338 case LFUN_MOUSE_MOTION:
339 // eat other mouse commands
343 InsetMath::doDispatch(cur, cmd);
349 bool InsetMathSpace::isNegative() const
351 if (space_info[space_].custom)
352 return length_.value() < 0;
353 return space_info[space_].negative;