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 Inset * InsetVSpace::clone() const
56 return new InsetVSpace(*this);
60 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
64 case LFUN_INSET_MODIFY: {
65 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), space_);
69 case LFUN_MOUSE_RELEASE:
71 InsetVSpaceMailer(*this).showDialog(&cur.bv());
75 Inset::doDispatch(cur, cmd);
81 void InsetVSpace::read(Buffer const &, Lexer & lex)
83 BOOST_ASSERT(lex.isOK());
91 if (end_token != "\\end_inset")
92 lex.printError("Missing \\end_inset at this point. "
97 void InsetVSpace::write(Buffer const &, ostream & os) const
99 os << "VSpace " << space_.asLyXCommand();
103 docstring const InsetVSpace::label() const
105 static docstring const label = _("Vertical Space");
106 return label + " (" + space_.asGUIName() + ')';
111 int const arrow_size = 4;
115 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
117 int height = 3 * arrow_size;
118 if (space_.length().len().value() >= 0.0)
119 height = max(height, space_.inPixels(*mi.base.bv));
128 theFontMetrics(font).rectText(label(), w, a, d);
130 height = max(height, a + d);
132 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
133 dim.des = height - dim.asc; // label text
134 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
135 // Cache the inset dimension.
136 setDimCache(mi, dim);
140 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
142 Dimension const dim = dimension(*pi.base.bv);
143 x += ADD_TO_VSPACE_WIDTH;
144 int const start = y - dim.asc;
145 int const end = y + dim.des;
147 // y-values for top arrow
149 // y-values for bottom arrow
152 if (space_.kind() == VSpace::VFILL) {
156 // adding or removing space
157 bool const added = space_.kind() != VSpace::LENGTH ||
158 space_.length().len().value() >= 0.0;
159 ty1 = added ? (start + arrow_size) : start;
160 ty2 = added ? start : (start + arrow_size);
161 by1 = added ? (end - arrow_size) : end;
162 by2 = added ? end : (end - arrow_size);
165 int const midx = x + arrow_size;
166 int const rightx = midx + arrow_size;
174 font.setColor(Color_added_space);
177 docstring const lab = label();
178 theFontMetrics(font).rectText(lab, w, a, d);
180 pi.pain.rectText(x + 2 * arrow_size + 5,
181 start + (end - start) / 2 + (a - d) / 2,
182 lab, font, Color_none, Color_none);
185 pi.pain.line(x, ty1, midx, ty2, Color_added_space);
186 pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
189 pi.pain.line(x, by1, midx, by2, Color_added_space);
190 pi.pain.line(midx, by2, rightx, by1, Color_added_space);
193 pi.pain.line(midx, ty2, midx, by2, Color_added_space);
197 int InsetVSpace::latex(Buffer const & buf, odocstream & os,
198 OutputParams const &) const
200 os << from_ascii(space_.asLatexCommand(buf.params())) << '\n';
205 int InsetVSpace::plaintext(Buffer const &, odocstream & os,
206 OutputParams const &) const
209 return PLAINTEXT_NEWLINE;
213 int InsetVSpace::docbook(Buffer const &, odocstream & os,
214 OutputParams const &) const
221 string const InsetVSpaceMailer::name_ = "vspace";
224 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
229 string const InsetVSpaceMailer::inset2string(Buffer const &) const
231 return params2string(inset_.space());
235 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
241 istringstream data(in);
247 if (!lex || name != name_)
248 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
253 vspace = VSpace(vsp);
257 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
260 data << name_ << ' ' << vspace.asLyXCommand();