2002-02-26 10:50:48 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file VSpace.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2001-11-30 14:57:15 +00:00
|
|
|
*
|
2002-02-26 10:50:48 +00:00
|
|
|
* \author Matthias Ettrich
|
2003-08-23 00:17:00 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-02-26 10:50:48 +00:00
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "VSpace.h"
|
2007-10-24 22:55:02 +00:00
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "BufferView.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/gettext.h"
|
2007-10-24 22:55:02 +00:00
|
|
|
#include "Length.h"
|
2007-04-29 23:33:02 +00:00
|
|
|
#include "Text.h"
|
2007-10-11 09:59:01 +00:00
|
|
|
#include "TextMetrics.h" // for defaultRowHeight()
|
2001-12-02 16:39:57 +00:00
|
|
|
|
2005-01-27 21:05:44 +00:00
|
|
|
#include "support/convert.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
#include "support/lstrings.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2008-03-15 00:22:54 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2008-03-15 00:22:54 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2001-11-30 14:57:15 +00:00
|
|
|
//
|
|
|
|
// VSpace class
|
|
|
|
//
|
|
|
|
|
|
|
|
VSpace::VSpace()
|
2003-12-01 14:16:27 +00:00
|
|
|
: kind_(DEFSKIP), len_(), keep_(false)
|
2001-11-30 14:57:15 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-09-29 21:37:28 +00:00
|
|
|
VSpace::VSpace(VSpaceKind k)
|
2001-12-02 23:47:06 +00:00
|
|
|
: kind_(k), len_(), keep_(false)
|
2001-11-30 14:57:15 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
VSpace::VSpace(Length const & l)
|
2001-11-30 14:57:15 +00:00
|
|
|
: kind_(LENGTH), len_(l), keep_(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
VSpace::VSpace(GlueLength const & l)
|
2001-12-02 23:47:06 +00:00
|
|
|
: kind_(LENGTH), len_(l), keep_(false)
|
2001-11-30 14:57:15 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
VSpace::VSpace(string const & data)
|
2003-12-01 14:16:27 +00:00
|
|
|
: kind_(DEFSKIP), len_(), keep_(false)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
if (data.empty())
|
|
|
|
return;
|
2003-11-13 13:43:44 +00:00
|
|
|
|
|
|
|
string input = rtrim(data);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-12-12 22:43:37 +00:00
|
|
|
size_t const length = input.length();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2002-08-23 11:47:03 +00:00
|
|
|
if (length > 1 && input[length - 1] == '*') {
|
2001-11-30 14:57:15 +00:00
|
|
|
keep_ = true;
|
1999-10-02 16:21:10 +00:00
|
|
|
input.erase(length - 1);
|
|
|
|
}
|
|
|
|
|
2003-11-13 13:43:44 +00:00
|
|
|
if (prefixIs(input, "defskip"))
|
|
|
|
kind_ = DEFSKIP;
|
|
|
|
else if (prefixIs(input, "smallskip"))
|
|
|
|
kind_ = SMALLSKIP;
|
|
|
|
else if (prefixIs(input, "medskip"))
|
|
|
|
kind_ = MEDSKIP;
|
|
|
|
else if (prefixIs(input, "bigskip"))
|
|
|
|
kind_ = BIGSKIP;
|
|
|
|
else if (prefixIs(input, "vfill"))
|
|
|
|
kind_ = VFILL;
|
|
|
|
else if (isValidGlueLength(input, &len_))
|
|
|
|
kind_ = LENGTH;
|
2003-09-05 22:17:02 +00:00
|
|
|
else if (isStrDbl(input)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// This last one is for reading old .lyx files
|
|
|
|
// without units in added_space_top/bottom.
|
|
|
|
// Let unit default to centimeters here.
|
2001-11-30 14:57:15 +00:00
|
|
|
kind_ = LENGTH;
|
2007-04-28 12:58:49 +00:00
|
|
|
len_ = GlueLength(Length(convert<double>(input), Length::CM));
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-15 12:01:38 +00:00
|
|
|
bool VSpace::operator==(VSpace const & other) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-11-30 14:57:15 +00:00
|
|
|
if (kind_ != other.kind_)
|
2001-02-12 14:09:09 +00:00
|
|
|
return false;
|
2001-11-30 14:57:15 +00:00
|
|
|
|
|
|
|
if (kind_ != LENGTH)
|
|
|
|
return this->keep_ == other.keep_;
|
|
|
|
|
|
|
|
if (len_ != other.len_)
|
1999-09-27 18:44:28 +00:00
|
|
|
return false;
|
2001-11-30 14:57:15 +00:00
|
|
|
|
|
|
|
return keep_ == other.keep_;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
string const VSpace::asLyXCommand() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-11-30 14:57:15 +00:00
|
|
|
string result;
|
|
|
|
switch (kind_) {
|
1999-09-27 18:44:28 +00:00
|
|
|
case DEFSKIP: result = "defskip"; break;
|
|
|
|
case SMALLSKIP: result = "smallskip"; break;
|
|
|
|
case MEDSKIP: result = "medskip"; break;
|
|
|
|
case BIGSKIP: result = "bigskip"; break;
|
|
|
|
case VFILL: result = "vfill"; break;
|
2001-11-30 14:57:15 +00:00
|
|
|
case LENGTH: result = len_.asString(); break;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2003-12-01 14:16:27 +00:00
|
|
|
if (keep_)
|
2001-11-30 14:57:15 +00:00
|
|
|
result += '*';
|
|
|
|
return result;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
string const VSpace::asLatexCommand(BufferParams const & params) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-11-30 14:57:15 +00:00
|
|
|
switch (kind_) {
|
|
|
|
case DEFSKIP:
|
2003-11-13 13:43:44 +00:00
|
|
|
return params.getDefSkip().asLatexCommand(params);
|
|
|
|
|
2002-03-01 12:39:22 +00:00
|
|
|
case SMALLSKIP:
|
2003-11-13 13:43:44 +00:00
|
|
|
return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
|
|
|
|
|
2002-03-01 12:39:22 +00:00
|
|
|
case MEDSKIP:
|
2003-11-13 13:43:44 +00:00
|
|
|
return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
|
|
|
|
|
2002-03-01 12:39:22 +00:00
|
|
|
case BIGSKIP:
|
2003-11-13 13:43:44 +00:00
|
|
|
return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2003-11-13 13:43:44 +00:00
|
|
|
case VFILL:
|
|
|
|
return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
|
2002-03-02 13:21:31 +00:00
|
|
|
|
2004-04-03 08:37:12 +00:00
|
|
|
case LENGTH:
|
2003-11-13 13:43:44 +00:00
|
|
|
return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
|
|
|
|
: "\\vspace{" + len_.asLatexString() + '}';
|
2003-11-21 17:31:46 +00:00
|
|
|
|
|
|
|
default:
|
2013-04-25 21:27:10 +00:00
|
|
|
LATTEST(false);
|
|
|
|
// fall through in release mode
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2013-04-25 21:27:10 +00:00
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-11 20:59:04 +00:00
|
|
|
docstring const VSpace::asGUIName() const
|
2005-05-24 10:23:30 +00:00
|
|
|
{
|
2006-11-11 20:59:04 +00:00
|
|
|
docstring result;
|
2005-05-24 10:23:30 +00:00
|
|
|
switch (kind_) {
|
|
|
|
case DEFSKIP:
|
2006-11-11 20:59:04 +00:00
|
|
|
result = _("Default skip");
|
2005-05-24 10:23:30 +00:00
|
|
|
break;
|
|
|
|
case SMALLSKIP:
|
2006-11-11 20:59:04 +00:00
|
|
|
result = _("Small skip");
|
2005-05-24 10:23:30 +00:00
|
|
|
break;
|
|
|
|
case MEDSKIP:
|
2006-11-11 20:59:04 +00:00
|
|
|
result = _("Medium skip");
|
2005-05-24 10:23:30 +00:00
|
|
|
break;
|
|
|
|
case BIGSKIP:
|
2006-11-11 20:59:04 +00:00
|
|
|
result = _("Big skip");
|
2005-05-24 10:23:30 +00:00
|
|
|
break;
|
|
|
|
case VFILL:
|
2006-11-11 20:59:04 +00:00
|
|
|
result = _("Vertical fill");
|
2005-05-24 10:23:30 +00:00
|
|
|
break;
|
|
|
|
case LENGTH:
|
2006-11-11 20:59:04 +00:00
|
|
|
result = from_ascii(len_.asString());
|
2005-05-24 10:23:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (keep_)
|
2006-11-11 20:59:04 +00:00
|
|
|
result += ", " + _("protected");
|
2005-05-24 10:23:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-03-01 12:39:22 +00:00
|
|
|
|
2009-06-12 15:07:04 +00:00
|
|
|
string VSpace::asHTMLLength() const
|
|
|
|
{
|
|
|
|
string result;
|
|
|
|
switch (kind_) {
|
|
|
|
case DEFSKIP: result = "2ex"; break;
|
|
|
|
case SMALLSKIP: result = "1ex"; break;
|
|
|
|
case MEDSKIP: result = "3ex"; break;
|
|
|
|
case BIGSKIP: result = "5ex"; break;
|
|
|
|
case LENGTH: {
|
|
|
|
Length tmp = len_.len();
|
|
|
|
if (tmp.value() > 0)
|
|
|
|
result = tmp.asHTMLString();
|
|
|
|
}
|
|
|
|
case VFILL: break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-10-27 13:45:27 +00:00
|
|
|
|
2003-02-26 17:04:10 +00:00
|
|
|
int VSpace::inPixels(BufferView const & bv) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
// Height of a normal line in pixels (zoom factor considered)
|
2004-04-03 08:37:12 +00:00
|
|
|
int const default_height = defaultRowHeight();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2001-11-30 14:57:15 +00:00
|
|
|
switch (kind_) {
|
2002-08-29 13:05:55 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
case DEFSKIP:
|
2007-08-21 13:03:55 +00:00
|
|
|
return bv.buffer().params().getDefSkip().inPixels(bv);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2017-04-01 17:54:33 +00:00
|
|
|
// This is how the skips are normally defined by LaTeX.
|
2002-08-29 13:05:55 +00:00
|
|
|
// But there should be some way to change this per document.
|
2002-01-19 17:05:24 +00:00
|
|
|
case SMALLSKIP:
|
2017-04-01 17:54:33 +00:00
|
|
|
return int(default_height / 4);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-01-19 17:05:24 +00:00
|
|
|
case MEDSKIP:
|
2017-04-01 17:54:33 +00:00
|
|
|
return int(default_height / 2);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-01-19 17:05:24 +00:00
|
|
|
case BIGSKIP:
|
2003-11-13 13:43:44 +00:00
|
|
|
return default_height;
|
2002-01-19 17:05:24 +00:00
|
|
|
|
|
|
|
case VFILL:
|
1999-12-16 06:43:25 +00:00
|
|
|
// leave space for the vfill symbol
|
2003-11-13 13:43:44 +00:00
|
|
|
return 3 * default_height;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-08-29 13:05:55 +00:00
|
|
|
case LENGTH:
|
2017-04-28 13:06:30 +00:00
|
|
|
return bv.inPixels(len_.len());
|
2003-11-21 17:31:46 +00:00
|
|
|
|
|
|
|
default:
|
2013-04-25 21:27:10 +00:00
|
|
|
LATTEST(false);
|
|
|
|
// fall through in release mode
|
2002-02-16 15:59:55 +00:00
|
|
|
}
|
2013-04-25 21:27:10 +00:00
|
|
|
return 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|