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 "DispatchResult.h"
19 #include "FuncRequest.h"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
35 using std::istringstream;
37 using std::ostringstream;
44 int const ADD_TO_VSPACE_WIDTH = 5;
49 InsetVSpace::InsetVSpace(VSpace const & space)
54 InsetVSpace::~InsetVSpace()
56 InsetVSpaceMailer(*this).hideDialog();
60 Inset * InsetVSpace::clone() const
62 return new InsetVSpace(*this);
66 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
70 case LFUN_INSET_MODIFY: {
71 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), space_);
75 case LFUN_MOUSE_RELEASE:
77 InsetVSpaceMailer(*this).showDialog(&cur.bv());
81 Inset::doDispatch(cur, cmd);
87 void InsetVSpace::read(Buffer const &, Lexer & lex)
89 BOOST_ASSERT(lex.isOK());
97 if (end_token != "\\end_inset")
98 lex.printError("Missing \\end_inset at this point. "
103 void InsetVSpace::write(Buffer const &, ostream & os) const
105 os << "VSpace " << space_.asLyXCommand();
109 docstring const InsetVSpace::label() const
111 static docstring const label = _("Vertical Space");
112 return label + " (" + space_.asGUIName() + ')';
117 int const arrow_size = 4;
121 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
123 int height = 3 * arrow_size;
124 if (space_.length().len().value() >= 0.0)
125 height = max(height, space_.inPixels(*mi.base.bv));
134 theFontMetrics(font).rectText(label(), w, a, d);
136 height = max(height, a + d);
138 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
139 dim.des = height - dim.asc; // label text
140 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
141 // Cache the inset dimension.
142 setDimCache(mi, dim);
146 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
148 Dimension const dim = dimension(*pi.base.bv);
149 x += ADD_TO_VSPACE_WIDTH;
150 int const start = y - dim.asc;
151 int const end = y + dim.des;
153 // y-values for top arrow
155 // y-values for bottom arrow
158 if (space_.kind() == VSpace::VFILL) {
162 // adding or removing space
163 bool const added = space_.kind() != VSpace::LENGTH ||
164 space_.length().len().value() >= 0.0;
165 ty1 = added ? (start + arrow_size) : start;
166 ty2 = added ? start : (start + arrow_size);
167 by1 = added ? (end - arrow_size) : end;
168 by2 = added ? end : (end - arrow_size);
171 int const midx = x + arrow_size;
172 int const rightx = midx + arrow_size;
180 font.setColor(Color::added_space);
183 docstring const lab = label();
184 theFontMetrics(font).rectText(lab, w, a, d);
186 pi.pain.rectText(x + 2 * arrow_size + 5,
187 start + (end - start) / 2 + (a - d) / 2,
188 lab, font, Color::none, Color::none);
191 pi.pain.line(x, ty1, midx, ty2, Color::added_space);
192 pi.pain.line(midx, ty2, rightx, ty1, Color::added_space);
195 pi.pain.line(x, by1, midx, by2, Color::added_space);
196 pi.pain.line(midx, by2, rightx, by1, Color::added_space);
199 pi.pain.line(midx, ty2, midx, by2, Color::added_space);
203 int InsetVSpace::latex(Buffer const & buf, odocstream & os,
204 OutputParams const &) const
206 os << from_ascii(space_.asLatexCommand(buf.params())) << '\n';
211 int InsetVSpace::plaintext(Buffer const &, odocstream & os,
212 OutputParams const &) const
215 return PLAINTEXT_NEWLINE;
219 int InsetVSpace::docbook(Buffer const &, odocstream & os,
220 OutputParams const &) const
227 string const InsetVSpaceMailer::name_ = "vspace";
230 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
235 string const InsetVSpaceMailer::inset2string(Buffer const &) const
237 return params2string(inset_.space());
241 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
247 istringstream data(in);
253 if (!lex || name != name_)
254 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
259 vspace = VSpace(vsp);
263 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
266 data << name_ << ' ' << vspace.asLyXCommand();