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"
23 #include "MetricsInfo.h"
25 #include "texstream.h"
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/gettext.h"
31 #include "support/Lexer.h"
32 #include "support/lassert.h"
34 #include "frontends/Application.h"
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
48 int const ADD_TO_VSPACE_WIDTH = 5;
53 InsetVSpace::InsetVSpace(VSpace const & space)
54 : Inset(nullptr), space_(space)
58 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
60 switch (cmd.action()) {
62 case LFUN_INSET_MODIFY: {
64 string arg = to_utf8(cmd.argument());
65 if (arg == "vspace custom")
66 arg = (space_.kind() == VSpace::LENGTH)
67 ? "vspace " + space_.length().asString()
68 : "vspace 1" + string(stringFromUnit(Length::defaultUnit()));
69 InsetVSpace::string2params(arg, space_);
74 Inset::doDispatch(cur, cmd);
80 bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
81 FuncStatus & status) const
83 switch (cmd.action()) {
85 case LFUN_INSET_MODIFY:
86 if (cmd.getArg(0) == "vspace") {
88 string arg = to_utf8(cmd.argument());
89 if (arg == "vspace custom")
90 arg = (space_.kind() == VSpace::LENGTH)
91 ? "vspace " + space_.length().asString()
92 : "vspace 1" + string(stringFromUnit(Length::defaultUnit()));
93 InsetVSpace::string2params(arg, vspace);
94 status.setOnOff(vspace == space_);
96 status.setEnabled(true);
100 return Inset::getStatus(cur, cmd, status);
105 void InsetVSpace::read(Lexer & lex)
107 LASSERT(lex.isOK(), return);
111 space_ = VSpace(vsp);
112 lex >> "\\end_inset";
116 void InsetVSpace::write(ostream & os) const
118 os << "VSpace " << space_.asLyXCommand();
122 docstring const InsetVSpace::label() const
124 static docstring const label = _("Vertical Space");
125 return label + " (" + space_.asGUIName() + ')';
130 int const vspace_arrow_size = 4;
134 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
136 int height = 3 * vspace_arrow_size;
137 if (space_.length().len().value() >= 0.0)
138 height = max(height, space_.inPixels(*mi.base.bv));
147 theFontMetrics(font).rectText(label(), w, a, d);
149 height = max(height, a + d);
151 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
152 dim.des = height - dim.asc; // label text
153 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * vspace_arrow_size + 5 + w;
157 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
159 Dimension const dim = dimension(*pi.base.bv);
160 x += ADD_TO_VSPACE_WIDTH;
161 int const start = y - dim.asc;
162 int const end = y + dim.des;
164 // y-values for top arrow
166 // y-values for bottom arrow
169 if (space_.kind() == VSpace::VFILL) {
173 // adding or removing space
174 bool const added = space_.kind() != VSpace::LENGTH ||
175 space_.length().len().value() >= 0.0;
176 ty1 = added ? (start + vspace_arrow_size) : start;
177 ty2 = added ? start : (start + vspace_arrow_size);
178 by1 = added ? (end - vspace_arrow_size) : end;
179 by2 = added ? end : (end - vspace_arrow_size);
182 int const midx = x + vspace_arrow_size;
183 int const rightx = midx + vspace_arrow_size;
191 font.setColor(Color_added_space);
194 docstring const lab = label();
195 theFontMetrics(font).rectText(lab, w, a, d);
197 pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
198 start + (end - start) / 2 + (a - d) / 2,
199 lab, font, Color_none, Color_none);
202 pi.pain.line(x, ty1, midx, ty2, Color_added_space);
203 pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
206 pi.pain.line(x, by1, midx, by2, Color_added_space);
207 pi.pain.line(midx, by2, rightx, by1, Color_added_space);
210 pi.pain.line(midx, ty2, midx, by2, Color_added_space);
214 void InsetVSpace::latex(otexstream & os, OutputParams const & rp) const
216 os << from_ascii(space_.asLatexCommand(buffer().params())) << breakln;
217 if (rp.need_noindent) {
218 // If the paragraph starts with a vspace and has more than that
219 // content, the \\noindent needs to come after that
220 // (as \\noindent leaves vmode).
221 os << "\\noindent" << termcmd;
226 int InsetVSpace::plaintext(odocstringstream & os,
227 OutputParams const &, size_t) const
230 return PLAINTEXT_NEWLINE;
234 void InsetVSpace::docbook(XMLStream & xs, OutputParams const &) const
240 docstring InsetVSpace::xhtml(XMLStream & os, OutputParams const &) const
242 string const len = space_.asHTMLLength();
243 string const attr = "style='height:" + (len.empty() ? "1em" : len) + "'";
244 os << xml::StartTag("div", attr, true) << xml::EndTag("div");
249 string InsetVSpace::contextMenuName() const
251 return "context-vspace";
255 void InsetVSpace::string2params(string const & in, VSpace & vspace)
261 istringstream data(in);
264 lex.setContext("InsetVSpace::string2params");
265 lex >> "vspace" >> vspace;
269 string InsetVSpace::params2string(VSpace const & vspace)
272 data << "vspace" << ' ' << vspace.asLyXCommand();