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"
28 #include "support/debug.h"
29 #include "support/gettext.h"
30 #include "support/lassert.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_);
70 Inset::doDispatch(cur, cmd);
76 bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
77 FuncStatus & status) const
81 case LFUN_INSET_MODIFY:
82 if (cmd.getArg(0) == "vspace") {
84 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
85 status.setOnOff(vspace == space_);
87 status.setEnabled(true);
91 return Inset::getStatus(cur, cmd, status);
96 bool InsetVSpace::showInsetDialog(BufferView * bv) const
98 bv->showDialog("vspace", params2string(space()),
99 const_cast<InsetVSpace *>(this));
104 void InsetVSpace::read(Lexer & lex)
106 LASSERT(lex.isOK(), /**/);
110 space_ = VSpace(vsp);
111 lex >> "\\end_inset";
115 void InsetVSpace::write(ostream & os) const
117 os << "VSpace " << space_.asLyXCommand();
121 docstring const InsetVSpace::label() const
123 static docstring const label = _("Vertical Space");
124 return label + " (" + space_.asGUIName() + ')';
129 int const vspace_arrow_size = 4;
133 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
135 int height = 3 * vspace_arrow_size;
136 if (space_.length().len().value() >= 0.0)
137 height = max(height, space_.inPixels(*mi.base.bv));
146 theFontMetrics(font).rectText(label(), w, a, d);
148 height = max(height, a + d);
150 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
151 dim.des = height - dim.asc; // label text
152 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * vspace_arrow_size + 5 + w;
153 // Cache the inset dimension.
154 setDimCache(mi, dim);
158 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
160 Dimension const dim = dimension(*pi.base.bv);
161 x += ADD_TO_VSPACE_WIDTH;
162 int const start = y - dim.asc;
163 int const end = y + dim.des;
165 // y-values for top arrow
167 // y-values for bottom arrow
170 if (space_.kind() == VSpace::VFILL) {
174 // adding or removing space
175 bool const added = space_.kind() != VSpace::LENGTH ||
176 space_.length().len().value() >= 0.0;
177 ty1 = added ? (start + vspace_arrow_size) : start;
178 ty2 = added ? start : (start + vspace_arrow_size);
179 by1 = added ? (end - vspace_arrow_size) : end;
180 by2 = added ? end : (end - vspace_arrow_size);
183 int const midx = x + vspace_arrow_size;
184 int const rightx = midx + vspace_arrow_size;
192 font.setColor(Color_added_space);
195 docstring const lab = label();
196 theFontMetrics(font).rectText(lab, w, a, d);
198 pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
199 start + (end - start) / 2 + (a - d) / 2,
200 lab, font, Color_none, Color_none);
203 pi.pain.line(x, ty1, midx, ty2, Color_added_space);
204 pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
207 pi.pain.line(x, by1, midx, by2, Color_added_space);
208 pi.pain.line(midx, by2, rightx, by1, Color_added_space);
211 pi.pain.line(midx, ty2, midx, by2, Color_added_space);
215 int InsetVSpace::latex(odocstream & os, OutputParams const &) const
217 os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
222 int InsetVSpace::plaintext(odocstream & os, OutputParams const &) const
225 return PLAINTEXT_NEWLINE;
229 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
236 docstring InsetVSpace::contextMenu(BufferView const &, int, int) const
238 return from_ascii("context-vspace");
242 void InsetVSpace::string2params(string const & in, VSpace & vspace)
248 istringstream data(in);
251 lex.setContext("InsetVSpace::string2params");
252 lex >> "vspace" >> vspace;
256 string InsetVSpace::params2string(VSpace const & vspace)
259 data << "vspace" << ' ' << vspace.asLyXCommand();