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_);
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_);
93 status.setEnabled(true);
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 LASSERT(lex.isOK(), /**/);
114 space_ = VSpace(vsp);
115 lex >> "\\end_inset";
119 void InsetVSpace::write(ostream & os) const
121 os << "VSpace " << space_.asLyXCommand();
125 docstring const InsetVSpace::label() const
127 static docstring const label = _("Vertical Space");
128 return label + " (" + space_.asGUIName() + ')';
133 int const vspace_arrow_size = 4;
137 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
139 int height = 3 * vspace_arrow_size;
140 if (space_.length().len().value() >= 0.0)
141 height = max(height, space_.inPixels(*mi.base.bv));
150 theFontMetrics(font).rectText(label(), w, a, d);
152 height = max(height, a + d);
154 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
155 dim.des = height - dim.asc; // label text
156 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * vspace_arrow_size + 5 + w;
157 // Cache the inset dimension.
158 setDimCache(mi, dim);
162 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
164 Dimension const dim = dimension(*pi.base.bv);
165 x += ADD_TO_VSPACE_WIDTH;
166 int const start = y - dim.asc;
167 int const end = y + dim.des;
169 // y-values for top arrow
171 // y-values for bottom arrow
174 if (space_.kind() == VSpace::VFILL) {
178 // adding or removing space
179 bool const added = space_.kind() != VSpace::LENGTH ||
180 space_.length().len().value() >= 0.0;
181 ty1 = added ? (start + vspace_arrow_size) : start;
182 ty2 = added ? start : (start + vspace_arrow_size);
183 by1 = added ? (end - vspace_arrow_size) : end;
184 by2 = added ? end : (end - vspace_arrow_size);
187 int const midx = x + vspace_arrow_size;
188 int const rightx = midx + vspace_arrow_size;
196 font.setColor(Color_added_space);
199 docstring const lab = label();
200 theFontMetrics(font).rectText(lab, w, a, d);
202 pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
203 start + (end - start) / 2 + (a - d) / 2,
204 lab, font, Color_none, Color_none);
207 pi.pain.line(x, ty1, midx, ty2, Color_added_space);
208 pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
211 pi.pain.line(x, by1, midx, by2, Color_added_space);
212 pi.pain.line(midx, by2, rightx, by1, Color_added_space);
215 pi.pain.line(midx, ty2, midx, by2, Color_added_space);
219 int InsetVSpace::latex(odocstream & os, OutputParams const &) const
221 os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
226 int InsetVSpace::plaintext(odocstream & os, OutputParams const &) const
229 return PLAINTEXT_NEWLINE;
233 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
240 docstring InsetVSpace::contextMenu(BufferView const &, int, int) const
242 return from_ascii("context-vspace");
246 void InsetVSpace::string2params(string const & in, VSpace & vspace)
252 istringstream data(in);
255 lex.setContext("InsetVSpace::string2params");
256 lex >> "vspace" >> vspace;
260 string InsetVSpace::params2string(VSpace const & vspace)
263 data << "vspace" << ' ' << vspace.asLyXCommand();