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"
25 #include "OutputParams.h"
26 #include "output_xhtml.h"
29 #include "support/debug.h"
30 #include "support/gettext.h"
31 #include "support/lassert.h"
33 #include "frontends/Application.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
45 int const ADD_TO_VSPACE_WIDTH = 5;
50 InsetVSpace::InsetVSpace(VSpace const & space)
51 : Inset(0), space_(space)
55 InsetVSpace::~InsetVSpace()
57 hideDialogs("vspace", this);
61 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
65 case LFUN_INSET_MODIFY: {
66 InsetVSpace::string2params(to_utf8(cmd.argument()), space_);
71 Inset::doDispatch(cur, cmd);
77 bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
78 FuncStatus & status) const
82 case LFUN_INSET_MODIFY:
83 if (cmd.getArg(0) == "vspace") {
85 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
86 status.setOnOff(vspace == space_);
88 status.setEnabled(true);
92 return Inset::getStatus(cur, cmd, status);
97 bool InsetVSpace::showInsetDialog(BufferView * bv) const
99 bv->showDialog("vspace", params2string(space()),
100 const_cast<InsetVSpace *>(this));
105 void InsetVSpace::read(Lexer & lex)
107 LASSERT(lex.isOK(), /**/);
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;
154 // Cache the inset dimension.
155 setDimCache(mi, dim);
159 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
161 Dimension const dim = dimension(*pi.base.bv);
162 x += ADD_TO_VSPACE_WIDTH;
163 int const start = y - dim.asc;
164 int const end = y + dim.des;
166 // y-values for top arrow
168 // y-values for bottom arrow
171 if (space_.kind() == VSpace::VFILL) {
175 // adding or removing space
176 bool const added = space_.kind() != VSpace::LENGTH ||
177 space_.length().len().value() >= 0.0;
178 ty1 = added ? (start + vspace_arrow_size) : start;
179 ty2 = added ? start : (start + vspace_arrow_size);
180 by1 = added ? (end - vspace_arrow_size) : end;
181 by2 = added ? end : (end - vspace_arrow_size);
184 int const midx = x + vspace_arrow_size;
185 int const rightx = midx + vspace_arrow_size;
193 font.setColor(Color_added_space);
196 docstring const lab = label();
197 theFontMetrics(font).rectText(lab, w, a, d);
199 pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
200 start + (end - start) / 2 + (a - d) / 2,
201 lab, font, Color_none, Color_none);
204 pi.pain.line(x, ty1, midx, ty2, Color_added_space);
205 pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
208 pi.pain.line(x, by1, midx, by2, Color_added_space);
209 pi.pain.line(midx, by2, rightx, by1, Color_added_space);
212 pi.pain.line(midx, ty2, midx, by2, Color_added_space);
216 int InsetVSpace::latex(odocstream & os, OutputParams const &) const
218 os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
223 int InsetVSpace::plaintext(odocstream & os, OutputParams const &) const
226 return PLAINTEXT_NEWLINE;
230 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
237 docstring InsetVSpace::xhtml(XHTMLStream & xs, OutputParams const &) const
239 string len = space_.asHTMLLength();
241 // we didn't understand it
244 string const attr = "style='height:" + len + "'";
245 xs << StartTag("div", attr, true) << EndTag("div");
251 docstring InsetVSpace::contextMenu(BufferView const &, int, int) const
253 return from_ascii("context-vspace");
257 void InsetVSpace::string2params(string const & in, VSpace & vspace)
263 istringstream data(in);
266 lex.setContext("InsetVSpace::string2params");
267 lex >> "vspace" >> vspace;
271 string InsetVSpace::params2string(VSpace const & vspace)
274 data << "vspace" << ' ' << vspace.asLyXCommand();