2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
|
* \file InsetMathSpace.cpp
|
2003-08-19 13:00:56 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "InsetMathSpace.h"
|
2007-04-26 16:05:57 +00:00
|
|
|
|
#include "MathData.h"
|
2009-01-10 15:54:18 +00:00
|
|
|
|
#include "MathFactory.h"
|
2006-10-22 10:15:23 +00:00
|
|
|
|
#include "MathStream.h"
|
2009-01-10 15:54:18 +00:00
|
|
|
|
#include "MathSupport.h"
|
2004-11-07 10:13:59 +00:00
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
#include "BufferView.h"
|
|
|
|
|
#include "Cursor.h"
|
|
|
|
|
#include "FuncRequest.h"
|
|
|
|
|
#include "FuncStatus.h"
|
2002-09-20 12:36:36 +00:00
|
|
|
|
#include "LaTeXFeatures.h"
|
2001-02-13 13:28:32 +00:00
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
#include "insets/InsetSpace.h"
|
|
|
|
|
|
|
|
|
|
#include "frontends/Application.h"
|
2004-11-07 10:13:59 +00:00
|
|
|
|
#include "frontends/Painter.h"
|
2003-10-06 15:43:21 +00:00
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
#include "support/lassert.h"
|
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
|
using namespace std;
|
2002-06-25 13:19:50 +00:00
|
|
|
|
|
2007-04-22 08:41:50 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
struct SpaceInfo {
|
|
|
|
|
string name;
|
|
|
|
|
int width;
|
|
|
|
|
InsetSpaceParams::Kind kind;
|
|
|
|
|
bool negative;
|
|
|
|
|
bool visible;
|
|
|
|
|
bool custom;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SpaceInfo space_info[] = {
|
|
|
|
|
// name width kind negative visible custom
|
|
|
|
|
{"!", 6, InsetSpaceParams::NEGTHIN, true, true, false},
|
|
|
|
|
{"negthinspace", 6, InsetSpaceParams::NEGTHIN, true, true, false},
|
|
|
|
|
{"negmedspace", 8, InsetSpaceParams::NEGMEDIUM, true, true, false},
|
|
|
|
|
{"negthickspace", 10, InsetSpaceParams::NEGTHICK, true, true, false},
|
|
|
|
|
{",", 6, InsetSpaceParams::THIN, false, true, false},
|
|
|
|
|
{"thinspace", 6, InsetSpaceParams::THIN, false, true, false},
|
|
|
|
|
{":", 8, InsetSpaceParams::MEDIUM, false, true, false},
|
|
|
|
|
{"medspace", 8, InsetSpaceParams::MEDIUM, false, true, false},
|
|
|
|
|
{";", 10, InsetSpaceParams::THICK, false, true, false},
|
|
|
|
|
{"thickspace", 10, InsetSpaceParams::THICK, false, true, false},
|
|
|
|
|
{"enskip", 10, InsetSpaceParams::ENSKIP, false, true, false},
|
|
|
|
|
{"quad", 20, InsetSpaceParams::QUAD, false, true, false},
|
|
|
|
|
{"qquad", 40, InsetSpaceParams::QQUAD, false, true, false},
|
|
|
|
|
{"lyxnegspace", -2, InsetSpaceParams::NEGTHIN, true, false, false},
|
|
|
|
|
{"lyxposspace", 2, InsetSpaceParams::THIN, false, false, false},
|
|
|
|
|
{"hspace", 0, InsetSpaceParams::CUSTOM, false, true, true},
|
2002-03-25 12:11:25 +00:00
|
|
|
|
};
|
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
int const nSpace = sizeof(space_info)/sizeof(SpaceInfo);
|
|
|
|
|
int const defaultSpace = 4;
|
2001-08-09 10:48:50 +00:00
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
} // anon namespace
|
2002-06-25 13:19:50 +00:00
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
InsetMathSpace::InsetMathSpace()
|
|
|
|
|
: space_(defaultSpace)
|
|
|
|
|
{
|
2001-02-28 11:56:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-04-22 08:26:06 +00:00
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
InsetMathSpace::InsetMathSpace(string const & name, string const & length)
|
|
|
|
|
: space_(defaultSpace)
|
2007-04-22 08:26:06 +00:00
|
|
|
|
{
|
2009-01-10 15:54:18 +00:00
|
|
|
|
for (int i = 0; i < nSpace; ++i)
|
|
|
|
|
if (space_info[i].name == name) {
|
|
|
|
|
space_ = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (space_info[space_].custom) {
|
|
|
|
|
length_ = Length(length);
|
|
|
|
|
if (length_.zero() || length_.empty()) {
|
|
|
|
|
length_.value(1.0);
|
|
|
|
|
length_.unit(Length::EM);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-04-22 08:26:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-02-28 11:56:36 +00:00
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
InsetMathSpace::InsetMathSpace(Length const & length)
|
|
|
|
|
: space_(defaultSpace), length_(length)
|
2004-04-08 15:55:50 +00:00
|
|
|
|
{
|
2007-04-22 08:26:06 +00:00
|
|
|
|
for (int i = 0; i < nSpace; ++i)
|
2009-01-10 15:54:18 +00:00
|
|
|
|
if (space_info[i].name == "hspace") {
|
2007-04-22 08:26:06 +00:00
|
|
|
|
space_ = i;
|
2009-01-10 15:54:18 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InsetMathSpace::~InsetMathSpace()
|
|
|
|
|
{
|
|
|
|
|
hideDialogs("mathspace", this);
|
2004-04-08 15:55:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
|
Inset * InsetMathSpace::clone() const
|
2002-03-21 17:42:56 +00:00
|
|
|
|
{
|
2007-08-30 18:03:17 +00:00
|
|
|
|
return new InsetMathSpace(*this);
|
2004-04-08 15:55:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-21 17:42:56 +00:00
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
void InsetMathSpace::metrics(MetricsInfo & mi, Dimension & dim) const
|
2004-04-08 15:55:50 +00:00
|
|
|
|
{
|
2009-01-10 15:54:18 +00:00
|
|
|
|
dim.asc = 4;
|
|
|
|
|
dim.des = 0;
|
|
|
|
|
if (space_info[space_].custom)
|
|
|
|
|
dim.wid = abs(length_.inPixels(
|
|
|
|
|
mi.base.textwidth,
|
|
|
|
|
mathed_char_width(mi.base.font, 'M')));
|
|
|
|
|
else
|
|
|
|
|
dim.wid = space_info[space_].width;
|
2004-04-08 15:55:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathSpace::draw(PainterInfo & pi, int x, int y) const
|
2004-04-08 15:55:50 +00:00
|
|
|
|
{
|
|
|
|
|
// Sadly, HP-UX CC can't handle that kind of initialization.
|
|
|
|
|
// XPoint p[4] = {{++x, y-3}, {x, y}, {x+width-2, y}, {x+width-2, y-3}};
|
2009-01-10 15:54:18 +00:00
|
|
|
|
if (!space_info[space_].visible)
|
2002-03-25 12:11:25 +00:00
|
|
|
|
return;
|
2002-03-21 17:42:56 +00:00
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
Dimension const dim = dimension(*pi.base.bv);
|
2001-10-22 15:37:49 +00:00
|
|
|
|
int xp[4];
|
|
|
|
|
int yp[4];
|
2009-01-10 15:54:18 +00:00
|
|
|
|
int w = dim.wid;
|
2002-03-21 17:42:56 +00:00
|
|
|
|
|
2004-04-08 15:55:50 +00:00
|
|
|
|
xp[0] = ++x; yp[0] = y - 3;
|
|
|
|
|
xp[1] = x; yp[1] = y;
|
|
|
|
|
xp[2] = x + w - 2; yp[2] = y;
|
|
|
|
|
xp[3] = x + w - 2; yp[3] = y - 3;
|
2002-03-21 17:42:56 +00:00
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
pi.pain.lines(xp, yp, 4,
|
|
|
|
|
space_info[space_].custom ?
|
|
|
|
|
Color_special :
|
|
|
|
|
(isNegative() ? Color_latex : Color_math));
|
2001-10-22 15:37:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathSpace::incSpace()
|
2001-02-13 19:10:18 +00:00
|
|
|
|
{
|
2009-01-10 15:54:18 +00:00
|
|
|
|
int const oldwidth = space_info[space_].width;
|
|
|
|
|
do
|
|
|
|
|
space_ = (space_ + 1) % nSpace;
|
|
|
|
|
while ((space_info[space_].width == oldwidth && !space_info[space_].custom) ||
|
|
|
|
|
!space_info[space_].visible);
|
|
|
|
|
if (space_info[space_].custom && (length_.zero() || length_.empty())) {
|
|
|
|
|
length_.value(1.0);
|
|
|
|
|
length_.unit(Length::EM);
|
|
|
|
|
}
|
2001-02-13 19:10:18 +00:00
|
|
|
|
}
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
2003-03-11 09:25:47 +00:00
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathSpace::validate(LaTeXFeatures & features) const
|
2002-09-20 12:36:36 +00:00
|
|
|
|
{
|
2009-01-10 15:54:18 +00:00
|
|
|
|
if (space_info[space_].name == "negmedspace" ||
|
|
|
|
|
space_info[space_].name == "negthickspace")
|
|
|
|
|
features.require("amsmath");
|
2002-09-20 12:36:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathSpace::maple(MapleStream & os) const
|
2001-11-15 14:14:37 +00:00
|
|
|
|
{
|
|
|
|
|
os << ' ';
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathSpace::mathematica(MathematicaStream & os) const
|
2002-07-01 11:17:14 +00:00
|
|
|
|
{
|
|
|
|
|
os << ' ';
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathSpace::octave(OctaveStream & os) const
|
2001-11-15 14:14:37 +00:00
|
|
|
|
{
|
|
|
|
|
os << ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathSpace::normalize(NormalStream & os) const
|
2001-11-15 14:14:37 +00:00
|
|
|
|
{
|
2002-08-22 13:02:14 +00:00
|
|
|
|
os << "[space " << int(space_) << "] ";
|
2001-11-15 14:14:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathSpace::write(WriteStream & os) const
|
2001-11-15 14:14:37 +00:00
|
|
|
|
{
|
2009-01-10 15:54:18 +00:00
|
|
|
|
// no MathEnsurer - all kinds work in text and math mode
|
|
|
|
|
os << '\\' << space_info[space_].name.c_str();
|
|
|
|
|
if (space_info[space_].custom)
|
|
|
|
|
os << '{' << length_.asLatexString().c_str() << '}';
|
|
|
|
|
else
|
2002-08-22 10:04:11 +00:00
|
|
|
|
os.pendingSpace(true);
|
2009-01-10 15:54:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const InsetMathSpace::createDialogStr() const
|
|
|
|
|
{
|
|
|
|
|
LASSERT(space_info[space_].visible, /**/);
|
|
|
|
|
InsetSpaceParams isp(true);
|
|
|
|
|
isp.kind = space_info[space_].kind;
|
|
|
|
|
isp.length = length_;
|
|
|
|
|
return InsetSpace::params2string(isp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
docstring InsetMathSpace::contextMenu(BufferView const &, int, int) const
|
|
|
|
|
{
|
|
|
|
|
return from_ascii("context-mathspace");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetMathSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
|
|
|
|
|
FuncStatus & status) const
|
|
|
|
|
{
|
|
|
|
|
switch (cmd.action) {
|
|
|
|
|
// we handle these
|
|
|
|
|
case LFUN_INSET_MODIFY:
|
|
|
|
|
case LFUN_INSET_DIALOG_UPDATE:
|
|
|
|
|
case LFUN_MOUSE_RELEASE:
|
|
|
|
|
case LFUN_MOUSE_PRESS:
|
|
|
|
|
case LFUN_MOUSE_MOTION:
|
|
|
|
|
status.setEnabled(true);
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
bool retval = InsetMath::getStatus(cur, cmd, status);
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InsetMathSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
|
|
|
|
|
{
|
|
|
|
|
switch (cmd.action) {
|
|
|
|
|
case LFUN_INSET_MODIFY:
|
|
|
|
|
if (cmd.getArg(0) == "mathspace") {
|
|
|
|
|
MathData ar;
|
|
|
|
|
if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
|
|
|
|
|
*this = *ar[0].nucleus()->asSpaceInset();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cur.undispatched();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LFUN_INSET_DIALOG_UPDATE:
|
|
|
|
|
cur.bv().updateDialog("mathspace", createDialogStr());
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LFUN_MOUSE_RELEASE:
|
|
|
|
|
if (cmd.button() == mouse_button::button1) {
|
|
|
|
|
string const data = createDialogStr();
|
|
|
|
|
cur.bv().showDialog("mathspace", data, this);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
cur.undispatched();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LFUN_MOUSE_PRESS:
|
|
|
|
|
case LFUN_MOUSE_MOTION:
|
|
|
|
|
// eat other mouse commands
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
InsetMath::doDispatch(cur, cmd);
|
|
|
|
|
break;
|
2002-08-22 10:04:11 +00:00
|
|
|
|
}
|
2001-11-15 14:14:37 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
2009-01-10 15:54:18 +00:00
|
|
|
|
bool InsetMathSpace::isNegative() const
|
|
|
|
|
{
|
|
|
|
|
if (space_info[space_].custom)
|
|
|
|
|
return length_.value() < 0;
|
|
|
|
|
return space_info[space_].negative;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
} // namespace lyx
|