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"
25 #include "MetricsInfo.h"
26 #include "OutputParams.h"
28 #include "support/debug.h"
29 #include "support/gettext.h"
31 #include "frontends/Application.h"
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
43 int const ADD_TO_VSPACE_WIDTH = 5;
48 InsetVSpace::InsetVSpace(VSpace const & space)
53 InsetVSpace::~InsetVSpace()
55 hideDialogs("vspace", this);
59 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
63 case LFUN_INSET_MODIFY: {
64 InsetVSpace::string2params(to_utf8(cmd.argument()), space_);
68 case LFUN_MOUSE_RELEASE:
69 if (!cur.selection() && cmd.button() == mouse_button::button1)
70 cur.bv().showDialog("vspace", params2string(space()),
71 const_cast<InsetVSpace *>(this));
75 Inset::doDispatch(cur, cmd);
81 bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
82 FuncStatus & status) const
86 case LFUN_INSET_MODIFY:
87 if (cmd.getArg(0) == "vspace") {
89 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
90 status.setOnOff(vspace == space_);
96 return Inset::getStatus(cur, cmd, status);
101 void InsetVSpace::edit(Cursor & cur, bool, EntryDirection)
103 cur.bv().showDialog("vspace", params2string(space()),
104 const_cast<InsetVSpace *>(this));
108 void InsetVSpace::read(Lexer & lex)
110 BOOST_ASSERT(lex.isOK());
114 space_ = VSpace(vsp);
118 if (end_token != "\\end_inset")
119 lex.printError("Missing \\end_inset at this point. "
124 void InsetVSpace::write(ostream & os) const
126 os << "VSpace " << space_.asLyXCommand();
130 docstring const InsetVSpace::label() const
132 static docstring const label = _("Vertical Space");
133 return label + " (" + space_.asGUIName() + ')';
138 int const arrow_size = 4;
142 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
144 int height = 3 * arrow_size;
145 if (space_.length().len().value() >= 0.0)
146 height = max(height, space_.inPixels(*mi.base.bv));
155 theFontMetrics(font).rectText(label(), w, a, d);
157 height = max(height, a + d);
159 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
160 dim.des = height - dim.asc; // label text
161 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
162 // Cache the inset dimension.
163 setDimCache(mi, dim);
167 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
169 Dimension const dim = dimension(*pi.base.bv);
170 x += ADD_TO_VSPACE_WIDTH;
171 int const start = y - dim.asc;
172 int const end = y + dim.des;
174 // y-values for top arrow
176 // y-values for bottom arrow
179 if (space_.kind() == VSpace::VFILL) {
183 // adding or removing space
184 bool const added = space_.kind() != VSpace::LENGTH ||
185 space_.length().len().value() >= 0.0;
186 ty1 = added ? (start + arrow_size) : start;
187 ty2 = added ? start : (start + arrow_size);
188 by1 = added ? (end - arrow_size) : end;
189 by2 = added ? end : (end - arrow_size);
192 int const midx = x + arrow_size;
193 int const rightx = midx + arrow_size;
201 font.setColor(Color_added_space);
204 docstring const lab = label();
205 theFontMetrics(font).rectText(lab, w, a, d);
207 pi.pain.rectText(x + 2 * arrow_size + 5,
208 start + (end - start) / 2 + (a - d) / 2,
209 lab, font, Color_none, Color_none);
212 pi.pain.line(x, ty1, midx, ty2, Color_added_space);
213 pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
216 pi.pain.line(x, by1, midx, by2, Color_added_space);
217 pi.pain.line(midx, by2, rightx, by1, Color_added_space);
220 pi.pain.line(midx, ty2, midx, by2, Color_added_space);
224 int InsetVSpace::latex(odocstream & os, OutputParams const &) const
226 os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
231 int InsetVSpace::plaintext(odocstream & os, OutputParams const &) const
234 return PLAINTEXT_NEWLINE;
238 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
245 docstring InsetVSpace::contextMenu(BufferView const &, int, int) const
247 return from_ascii("context-vspace");
251 void InsetVSpace::string2params(string const & in, VSpace & vspace)
257 istringstream data(in);
263 if (!lex || name != "vspace") {
264 LYXERR0("Expected arg 1 to be \"vspace\" in " << in);
271 vspace = VSpace(vsp);
275 string InsetVSpace::params2string(VSpace const & vspace)
278 data << "vspace" << ' ' << vspace.asLyXCommand();