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"
18 #include "Dimension.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "support/gettext.h"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
38 int const ADD_TO_VSPACE_WIDTH = 5;
43 InsetVSpace::InsetVSpace(VSpace const & space)
48 InsetVSpace::~InsetVSpace()
50 InsetVSpaceMailer(*this).hideDialog();
54 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
58 case LFUN_INSET_MODIFY: {
59 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), space_);
63 case LFUN_MOUSE_RELEASE:
65 InsetVSpaceMailer(*this).showDialog(&cur.bv());
69 Inset::doDispatch(cur, cmd);
75 void InsetVSpace::read(Lexer & lex)
77 BOOST_ASSERT(lex.isOK());
85 if (end_token != "\\end_inset")
86 lex.printError("Missing \\end_inset at this point. "
91 void InsetVSpace::write(ostream & os) const
93 os << "VSpace " << space_.asLyXCommand();
97 docstring const InsetVSpace::label() const
99 static docstring const label = _("Vertical Space");
100 return label + " (" + space_.asGUIName() + ')';
105 int const arrow_size = 4;
109 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
111 int height = 3 * arrow_size;
112 if (space_.length().len().value() >= 0.0)
113 height = max(height, space_.inPixels(*mi.base.bv));
122 theFontMetrics(font).rectText(label(), w, a, d);
124 height = max(height, a + d);
126 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
127 dim.des = height - dim.asc; // label text
128 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
129 // Cache the inset dimension.
130 setDimCache(mi, dim);
134 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
136 Dimension const dim = dimension(*pi.base.bv);
137 x += ADD_TO_VSPACE_WIDTH;
138 int const start = y - dim.asc;
139 int const end = y + dim.des;
141 // y-values for top arrow
143 // y-values for bottom arrow
146 if (space_.kind() == VSpace::VFILL) {
150 // adding or removing space
151 bool const added = space_.kind() != VSpace::LENGTH ||
152 space_.length().len().value() >= 0.0;
153 ty1 = added ? (start + arrow_size) : start;
154 ty2 = added ? start : (start + arrow_size);
155 by1 = added ? (end - arrow_size) : end;
156 by2 = added ? end : (end - arrow_size);
159 int const midx = x + arrow_size;
160 int const rightx = midx + arrow_size;
168 font.setColor(Color_added_space);
171 docstring const lab = label();
172 theFontMetrics(font).rectText(lab, w, a, d);
174 pi.pain.rectText(x + 2 * arrow_size + 5,
175 start + (end - start) / 2 + (a - d) / 2,
176 lab, font, Color_none, Color_none);
179 pi.pain.line(x, ty1, midx, ty2, Color_added_space);
180 pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
183 pi.pain.line(x, by1, midx, by2, Color_added_space);
184 pi.pain.line(midx, by2, rightx, by1, Color_added_space);
187 pi.pain.line(midx, ty2, midx, by2, Color_added_space);
191 int InsetVSpace::latex(odocstream & os, OutputParams const &) const
193 os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
198 int InsetVSpace::plaintext(odocstream & os, OutputParams const &) const
201 return PLAINTEXT_NEWLINE;
205 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
212 string const InsetVSpaceMailer::name_ = "vspace";
215 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
220 string const InsetVSpaceMailer::inset2string(Buffer const &) const
222 return params2string(inset_.space());
226 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
232 istringstream data(in);
238 if (!lex || name != name_)
239 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
244 vspace = VSpace(vsp);
248 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
251 data << name_ << ' ' << vspace.asLyXCommand();