3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Matthias Ettrich
8 * Full author contact details are available in file CREDITS.
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "support/gettext.h"
21 #include "TextMetrics.h" // for defaultRowHeight()
23 #include "support/convert.h"
24 #include "support/lstrings.h"
26 #include "support/lassert.h"
29 using namespace lyx::support;
39 : kind_(DEFSKIP), len_(), keep_(false)
43 VSpace::VSpace(VSpaceKind k)
44 : kind_(k), len_(), keep_(false)
48 VSpace::VSpace(Length const & l)
49 : kind_(LENGTH), len_(l), keep_(false)
53 VSpace::VSpace(GlueLength const & l)
54 : kind_(LENGTH), len_(l), keep_(false)
58 VSpace::VSpace(string const & data)
59 : kind_(DEFSKIP), len_(), keep_(false)
64 string input = rtrim(data);
66 size_t const length = input.length();
68 if (length > 1 && input[length - 1] == '*') {
70 input.erase(length - 1);
73 if (prefixIs(input, "defskip"))
75 else if (prefixIs(input, "smallskip"))
77 else if (prefixIs(input, "medskip"))
79 else if (prefixIs(input, "bigskip"))
81 else if (prefixIs(input, "vfill"))
83 else if (isValidGlueLength(input, &len_))
85 else if (isStrDbl(input)) {
86 // This last one is for reading old .lyx files
87 // without units in added_space_top/bottom.
88 // Let unit default to centimeters here.
90 len_ = GlueLength(Length(convert<double>(input), Length::CM));
95 bool VSpace::operator==(VSpace const & other) const
97 if (kind_ != other.kind_)
101 return this->keep_ == other.keep_;
103 if (len_ != other.len_)
106 return keep_ == other.keep_;
110 string const VSpace::asLyXCommand() const
114 case DEFSKIP: result = "defskip"; break;
115 case SMALLSKIP: result = "smallskip"; break;
116 case MEDSKIP: result = "medskip"; break;
117 case BIGSKIP: result = "bigskip"; break;
118 case VFILL: result = "vfill"; break;
119 case LENGTH: result = len_.asString(); break;
127 string const VSpace::asLatexCommand(BufferParams const & params) const
131 return params.getDefSkip().asLatexCommand(params);
134 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
137 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
140 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
143 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
146 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
147 : "\\vspace{" + len_.asLatexString() + '}';
150 LASSERT(false, /**/);
156 docstring const VSpace::asGUIName() const
161 result = _("Default skip");
164 result = _("Small skip");
167 result = _("Medium skip");
170 result = _("Big skip");
173 result = _("Vertical fill");
176 result = from_ascii(len_.asString());
180 result += ", " + _("protected");
185 string VSpace::asHTMLLength() const
189 case DEFSKIP: result = "2ex"; break;
190 case SMALLSKIP: result = "1ex"; break;
191 case MEDSKIP: result = "3ex"; break;
192 case BIGSKIP: result = "5ex"; break;
194 Length tmp = len_.len();
196 result = tmp.asHTMLString();
204 int VSpace::inPixels(BufferView const & bv) const
206 // Height of a normal line in pixels (zoom factor considered)
207 int const default_height = defaultRowHeight();
212 return bv.buffer().params().getDefSkip().inPixels(bv);
214 // This is how the skips are normally defined by LateX.
215 // But there should be some way to change this per document.
217 return default_height / 4;
220 return default_height / 2;
223 return default_height;
226 // leave space for the vfill symbol
227 return 3 * default_height;
230 return len_.len().inPixels(bv.workWidth());
233 LASSERT(false, /**/);