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/lassert.h"
29 #include "support/debug.h"
30 #include "support/gettext.h"
32 #include "frontends/Application.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
44 int const ADD_TO_VSPACE_WIDTH = 5;
49 InsetVSpace::InsetVSpace(VSpace const & space)
54 InsetVSpace::~InsetVSpace()
56 hideDialogs("vspace", this);
60 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
64 case LFUN_INSET_MODIFY: {
65 InsetVSpace::string2params(to_utf8(cmd.argument()), space_);
69 case LFUN_MOUSE_RELEASE:
70 if (!cur.selection() && cmd.button() == mouse_button::button1)
71 cur.bv().showDialog("vspace", params2string(space()),
72 const_cast<InsetVSpace *>(this));
76 Inset::doDispatch(cur, cmd);
82 bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
83 FuncStatus & status) const
87 case LFUN_INSET_MODIFY:
88 if (cmd.getArg(0) == "vspace") {
90 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
91 status.setOnOff(vspace == space_);
97 return Inset::getStatus(cur, cmd, status);
102 void InsetVSpace::edit(Cursor & cur, bool, EntryDirection)
104 cur.bv().showDialog("vspace", params2string(space()),
105 const_cast<InsetVSpace *>(this));
109 void InsetVSpace::read(Lexer & lex)
111 LASSERT(lex.isOK(), /**/);
115 space_ = VSpace(vsp);
116 lex >> "\\end_inset";
120 void InsetVSpace::write(ostream & os) const
122 os << "VSpace " << space_.asLyXCommand();
126 docstring const InsetVSpace::label() const
128 static docstring const label = _("Vertical Space");
129 return label + " (" + space_.asGUIName() + ')';
134 int const arrow_size = 4;
138 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
140 int height = 3 * arrow_size;
141 if (space_.length().len().value() >= 0.0)
142 height = max(height, space_.inPixels(*mi.base.bv));
151 theFontMetrics(font).rectText(label(), w, a, d);
153 height = max(height, a + d);
155 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
156 dim.des = height - dim.asc; // label text
157 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
158 // Cache the inset dimension.
159 setDimCache(mi, dim);
163 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
165 Dimension const dim = dimension(*pi.base.bv);
166 x += ADD_TO_VSPACE_WIDTH;
167 int const start = y - dim.asc;
168 int const end = y + dim.des;
170 // y-values for top arrow
172 // y-values for bottom arrow
175 if (space_.kind() == VSpace::VFILL) {
179 // adding or removing space
180 bool const added = space_.kind() != VSpace::LENGTH ||
181 space_.length().len().value() >= 0.0;
182 ty1 = added ? (start + arrow_size) : start;
183 ty2 = added ? start : (start + arrow_size);
184 by1 = added ? (end - arrow_size) : end;
185 by2 = added ? end : (end - arrow_size);
188 int const midx = x + arrow_size;
189 int const rightx = midx + arrow_size;
197 font.setColor(Color_added_space);
200 docstring const lab = label();
201 theFontMetrics(font).rectText(lab, w, a, d);
203 pi.pain.rectText(x + 2 * arrow_size + 5,
204 start + (end - start) / 2 + (a - d) / 2,
205 lab, font, Color_none, Color_none);
208 pi.pain.line(x, ty1, midx, ty2, Color_added_space);
209 pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
212 pi.pain.line(x, by1, midx, by2, Color_added_space);
213 pi.pain.line(midx, by2, rightx, by1, Color_added_space);
216 pi.pain.line(midx, ty2, midx, by2, Color_added_space);
220 int InsetVSpace::latex(odocstream & os, OutputParams const &) const
222 os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
227 int InsetVSpace::plaintext(odocstream & os, OutputParams const &) const
230 return PLAINTEXT_NEWLINE;
234 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
241 docstring InsetVSpace::contextMenu(BufferView const &, int, int) const
243 return from_ascii("context-vspace");
247 void InsetVSpace::string2params(string const & in, VSpace & vspace)
253 istringstream data(in);
256 lex.setContext("InsetVSpace::string2params");
257 lex >> "vspace" >> vspace;
261 string InsetVSpace::params2string(VSpace const & vspace)
264 data << "vspace" << ' ' << vspace.asLyXCommand();