2 * \file InsetVSpace.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "InsetVSpace.h"
17 #include "BufferView.h"
19 #include "Dimension.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
24 #include "MetricsInfo.h"
26 #include "texstream.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lassert.h"
34 #include "frontends/Application.h"
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
46 int const ADD_TO_VSPACE_WIDTH = 5;
51 InsetVSpace::InsetVSpace(VSpace const & space)
52 : Inset(nullptr), space_(space)
56 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
58 switch (cmd.action()) {
60 case LFUN_INSET_MODIFY: {
62 string arg = to_utf8(cmd.argument());
63 if (arg == "vspace custom")
64 arg = (space_.kind() == VSpace::LENGTH)
65 ? "vspace " + space_.length().asString()
66 : "vspace 1" + string(stringFromUnit(Length::defaultUnit()));
67 InsetVSpace::string2params(arg, space_);
72 Inset::doDispatch(cur, cmd);
78 bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
79 FuncStatus & status) const
81 switch (cmd.action()) {
83 case LFUN_INSET_MODIFY:
84 if (cmd.getArg(0) == "vspace") {
86 string arg = to_utf8(cmd.argument());
87 if (arg == "vspace custom")
88 arg = (space_.kind() == VSpace::LENGTH)
89 ? "vspace " + space_.length().asString()
90 : "vspace 1" + string(stringFromUnit(Length::defaultUnit()));
91 InsetVSpace::string2params(arg, vspace);
92 status.setOnOff(vspace == space_);
94 status.setEnabled(true);
98 return Inset::getStatus(cur, cmd, status);
103 void InsetVSpace::read(Lexer & lex)
105 LASSERT(lex.isOK(), return);
109 space_ = VSpace(vsp);
110 lex >> "\\end_inset";
114 void InsetVSpace::write(ostream & os) const
116 os << "VSpace " << space_.asLyXCommand();
120 docstring const InsetVSpace::label() const
122 static docstring const label = _("Vertical Space");
123 return label + " (" + space_.asGUIName() + ')';
128 int const vspace_arrow_size = 4;
132 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
134 int height = 3 * vspace_arrow_size;
135 if (space_.length().len().value() >= 0.0)
136 height = max(height, space_.inPixels(*mi.base.bv));
145 theFontMetrics(font).rectText(label(), w, a, d);
147 height = max(height, a + d);
149 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
150 dim.des = height - dim.asc; // label text
151 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * vspace_arrow_size + 5 + w;
155 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
157 Dimension const dim = dimension(*pi.base.bv);
158 x += ADD_TO_VSPACE_WIDTH;
159 int const start = y - dim.asc;
160 int const end = y + dim.des;
162 // y-values for top arrow
164 // y-values for bottom arrow
167 if (space_.kind() == VSpace::VFILL) {
171 // adding or removing space
172 bool const added = space_.kind() != VSpace::LENGTH ||
173 space_.length().len().value() >= 0.0;
174 ty1 = added ? (start + vspace_arrow_size) : start;
175 ty2 = added ? start : (start + vspace_arrow_size);
176 by1 = added ? (end - vspace_arrow_size) : end;
177 by2 = added ? end : (end - vspace_arrow_size);
180 int const midx = x + vspace_arrow_size;
181 int const rightx = midx + vspace_arrow_size;
189 font.setColor(Color_added_space);
192 docstring const lab = label();
193 theFontMetrics(font).rectText(lab, w, a, d);
195 pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
196 start + (end - start) / 2 + (a - d) / 2,
197 lab, font, Color_none, Color_none);
200 pi.pain.line(x, ty1, midx, ty2, Color_added_space);
201 pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
204 pi.pain.line(x, by1, midx, by2, Color_added_space);
205 pi.pain.line(midx, by2, rightx, by1, Color_added_space);
208 pi.pain.line(midx, ty2, midx, by2, Color_added_space);
212 void InsetVSpace::latex(otexstream & os, OutputParams const &) const
214 os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
218 int InsetVSpace::plaintext(odocstringstream & os,
219 OutputParams const &, size_t) const
222 return PLAINTEXT_NEWLINE;
226 void InsetVSpace::docbook(XMLStream & xs, OutputParams const &) const
232 docstring InsetVSpace::xhtml(XMLStream & os, OutputParams const &) const
234 string const len = space_.asHTMLLength();
235 string const attr = "style='height:" + (len.empty() ? "1em" : len) + "'";
236 os << xml::StartTag("div", attr, true) << xml::EndTag("div");
241 string InsetVSpace::contextMenuName() const
243 return "context-vspace";
247 void InsetVSpace::string2params(string const & in, VSpace & vspace)
253 istringstream data(in);
256 lex.setContext("InsetVSpace::string2params");
257 lex >> "vspace" >> vspace;
261 string InsetVSpace::params2string(VSpace const & vspace)
264 data << "vspace" << ' ' << vspace.asLyXCommand();