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 case LFUN_INSET_SETTINGS:
97 status.setEnabled(true);
101 return Inset::getStatus(cur, cmd, status);
106 void InsetVSpace::edit(Cursor & cur, bool, EntryDirection)
108 showInsetDialog(&cur.bv());
112 bool InsetVSpace::showInsetDialog(BufferView * bv) const
114 bv->showDialog("vspace", params2string(space()),
115 const_cast<InsetVSpace *>(this));
120 void InsetVSpace::read(Lexer & lex)
122 LASSERT(lex.isOK(), /**/);
126 space_ = VSpace(vsp);
127 lex >> "\\end_inset";
131 void InsetVSpace::write(ostream & os) const
133 os << "VSpace " << space_.asLyXCommand();
137 docstring const InsetVSpace::label() const
139 static docstring const label = _("Vertical Space");
140 return label + " (" + space_.asGUIName() + ')';
145 int const vspace_arrow_size = 4;
149 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
151 int height = 3 * vspace_arrow_size;
152 if (space_.length().len().value() >= 0.0)
153 height = max(height, space_.inPixels(*mi.base.bv));
162 theFontMetrics(font).rectText(label(), w, a, d);
164 height = max(height, a + d);
166 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
167 dim.des = height - dim.asc; // label text
168 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * vspace_arrow_size + 5 + w;
169 // Cache the inset dimension.
170 setDimCache(mi, dim);
174 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
176 Dimension const dim = dimension(*pi.base.bv);
177 x += ADD_TO_VSPACE_WIDTH;
178 int const start = y - dim.asc;
179 int const end = y + dim.des;
181 // y-values for top arrow
183 // y-values for bottom arrow
186 if (space_.kind() == VSpace::VFILL) {
190 // adding or removing space
191 bool const added = space_.kind() != VSpace::LENGTH ||
192 space_.length().len().value() >= 0.0;
193 ty1 = added ? (start + vspace_arrow_size) : start;
194 ty2 = added ? start : (start + vspace_arrow_size);
195 by1 = added ? (end - vspace_arrow_size) : end;
196 by2 = added ? end : (end - vspace_arrow_size);
199 int const midx = x + vspace_arrow_size;
200 int const rightx = midx + vspace_arrow_size;
208 font.setColor(Color_added_space);
211 docstring const lab = label();
212 theFontMetrics(font).rectText(lab, w, a, d);
214 pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
215 start + (end - start) / 2 + (a - d) / 2,
216 lab, font, Color_none, Color_none);
219 pi.pain.line(x, ty1, midx, ty2, Color_added_space);
220 pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
223 pi.pain.line(x, by1, midx, by2, Color_added_space);
224 pi.pain.line(midx, by2, rightx, by1, Color_added_space);
227 pi.pain.line(midx, ty2, midx, by2, Color_added_space);
231 int InsetVSpace::latex(odocstream & os, OutputParams const &) const
233 os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
238 int InsetVSpace::plaintext(odocstream & os, OutputParams const &) const
241 return PLAINTEXT_NEWLINE;
245 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
252 docstring InsetVSpace::contextMenu(BufferView const &, int, int) const
254 return from_ascii("context-vspace");
258 void InsetVSpace::string2params(string const & in, VSpace & vspace)
264 istringstream data(in);
267 lex.setContext("InsetVSpace::string2params");
268 lex >> "vspace" >> vspace;
272 string InsetVSpace::params2string(VSpace const & vspace)
275 data << "vspace" << ' ' << vspace.asLyXCommand();