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;
47 SpaceInfo space_info[] = {
48 // name width kind negative visible custom
49 {"!", 6, InsetSpaceParams::NEGTHIN, true, true, false},
50 {"negthinspace", 6, InsetSpaceParams::NEGTHIN, true, true, false},
51 {"negmedspace", 8, InsetSpaceParams::NEGMEDIUM, true, true, false},
52 {"negthickspace", 10, InsetSpaceParams::NEGTHICK, true, true, false},
53 {",", 6, InsetSpaceParams::THIN, false, true, false},
54 {"thinspace", 6, InsetSpaceParams::THIN, false, true, false},
55 {":", 8, InsetSpaceParams::MEDIUM, false, true, false},
56 {"medspace", 8, InsetSpaceParams::MEDIUM, false, true, false},
57 {";", 10, InsetSpaceParams::THICK, false, true, false},
58 {"thickspace", 10, InsetSpaceParams::THICK, false, true, false},
59 {"enskip", 10, InsetSpaceParams::ENSKIP, false, true, false},
60 {"quad", 20, InsetSpaceParams::QUAD, false, true, false},
61 {"qquad", 40, InsetSpaceParams::QQUAD, false, true, false},
62 {"lyxnegspace", -2, InsetSpaceParams::NEGTHIN, true, false, false},
63 {"lyxposspace", 2, InsetSpaceParams::THIN, false, false, false},
64 {"hspace", 0, InsetSpaceParams::CUSTOM, false, true, true},
67 int const nSpace = sizeof(space_info)/sizeof(SpaceInfo);
68 int const defaultSpace = 4;
72 InsetMathSpace::InsetMathSpace()
73 : space_(defaultSpace)
78 InsetMathSpace::InsetMathSpace(string const & name, string const & length)
79 : space_(defaultSpace)
81 for (int i = 0; i < nSpace; ++i)
82 if (space_info[i].name == name) {
86 if (space_info[space_].custom) {
87 length_ = Length(length);
88 if (length_.zero() || length_.empty()) {
90 length_.unit(Length::EM);
96 InsetMathSpace::InsetMathSpace(Length const & length)
97 : space_(defaultSpace), length_(length)
99 for (int i = 0; i < nSpace; ++i)
100 if (space_info[i].name == "hspace") {
107 Inset * InsetMathSpace::clone() const
109 return new InsetMathSpace(*this);
113 void InsetMathSpace::metrics(MetricsInfo & mi, Dimension & dim) const
117 if (space_info[space_].custom)
118 dim.wid = abs(length_.inPixels(
120 mathed_char_width(mi.base.font, 'M')));
122 dim.wid = space_info[space_].width;
126 void InsetMathSpace::draw(PainterInfo & pi, int x, int y) const
128 // Sadly, HP-UX CC can't handle that kind of initialization.
129 // XPoint p[4] = {{++x, y-3}, {x, y}, {x+width-2, y}, {x+width-2, y-3}};
130 if (!space_info[space_].visible)
133 Dimension const dim = dimension(*pi.base.bv);
138 xp[0] = ++x; yp[0] = y - 3;
139 xp[1] = x; yp[1] = y;
140 xp[2] = x + w - 2; yp[2] = y;
141 xp[3] = x + w - 2; yp[3] = y - 3;
143 pi.pain.lines(xp, yp, 4,
144 space_info[space_].custom ?
146 (isNegative() ? Color_latex : Color_math));
150 void InsetMathSpace::incSpace()
152 int const oldwidth = space_info[space_].width;
154 space_ = (space_ + 1) % nSpace;
155 while ((space_info[space_].width == oldwidth && !space_info[space_].custom) ||
156 !space_info[space_].visible);
157 if (space_info[space_].custom && (length_.zero() || length_.empty())) {
159 length_.unit(Length::EM);
164 void InsetMathSpace::validate(LaTeXFeatures & features) const
166 if (space_info[space_].name == "negmedspace" ||
167 space_info[space_].name == "negthickspace")
168 features.require("amsmath");
172 void InsetMathSpace::maple(MapleStream & os) const
177 void InsetMathSpace::mathematica(MathematicaStream & os) const
183 void InsetMathSpace::octave(OctaveStream & os) const
189 void InsetMathSpace::mathmlize(MathStream & ms) const
191 SpaceInfo const & si = space_info[space_];
192 if (si.negative || !si.visible)
196 l = length_.asHTMLString();
197 else if (si.kind != InsetSpaceParams::MEDIUM) {
205 ms << " width=\"" << from_ascii(l) << "\"";
210 void InsetMathSpace::normalize(NormalStream & os) const
212 os << "[space " << int(space_) << "] ";
216 void InsetMathSpace::write(WriteStream & os) const
218 // no MathEnsurer - all kinds work in text and math mode
219 os << '\\' << space_info[space_].name.c_str();
220 if (space_info[space_].custom)
221 os << '{' << length_.asLatexString().c_str() << '}';
223 os.pendingSpace(true);
227 InsetSpaceParams InsetMathSpace::params() const
229 LASSERT(space_info[space_].visible, /**/);
230 InsetSpaceParams isp(true);
231 isp.kind = space_info[space_].kind;
232 isp.length = GlueLength(length_);
237 docstring InsetMathSpace::contextMenu(BufferView const &, int, int) const
239 return from_ascii("context-mathspace");
243 bool InsetMathSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
244 FuncStatus & status) const
246 switch (cmd.action) {
248 case LFUN_INSET_MODIFY:
249 case LFUN_INSET_DIALOG_UPDATE:
250 case LFUN_MOUSE_RELEASE:
251 case LFUN_MOUSE_PRESS:
252 case LFUN_MOUSE_MOTION:
253 status.setEnabled(true);
256 bool retval = InsetMath::getStatus(cur, cmd, status);
262 void InsetMathSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
264 switch (cmd.action) {
265 case LFUN_INSET_MODIFY:
266 if (cmd.getArg(0) == "mathspace") {
268 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
269 *this = *ar[0].nucleus()->asSpaceInset();
276 case LFUN_MOUSE_RELEASE:
277 if (cmd.button() == mouse_button::button1) {
278 showInsetDialog(&cur.bv());
284 case LFUN_MOUSE_PRESS:
285 case LFUN_MOUSE_MOTION:
286 // eat other mouse commands
290 InsetMath::doDispatch(cur, cmd);
296 bool InsetMathSpace::isNegative() const
298 if (space_info[space_].custom)
299 return length_.value() < 0;
300 return space_info[space_].negative;