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"
18 #include "dispatchresult.h"
19 #include "funcrequest.h"
24 #include "metricsinfo.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
34 using std::istringstream;
36 using std::ostringstream;
43 int const ADD_TO_VSPACE_WIDTH = 5;
48 InsetVSpace::InsetVSpace(VSpace const & space)
53 InsetVSpace::~InsetVSpace()
55 InsetVSpaceMailer(*this).hideDialog();
59 std::auto_ptr<InsetBase> InsetVSpace::doClone() const
61 return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
65 void InsetVSpace::doDispatch(LCursor & cur, FuncRequest & cmd)
69 case LFUN_INSET_MODIFY: {
70 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), space_);
74 case LFUN_MOUSE_RELEASE:
75 InsetVSpaceMailer(*this).showDialog(&cur.bv());
79 InsetBase::doDispatch(cur, cmd);
85 void InsetVSpace::read(Buffer const &, LyXLex & lex)
87 BOOST_ASSERT(lex.isOK());
95 if (end_token != "\\end_inset")
96 lex.printError("Missing \\end_inset at this point. "
101 void InsetVSpace::write(Buffer const &, ostream & os) const
103 os << "VSpace " << space_.asLyXCommand();
107 docstring const InsetVSpace::label() const
109 static docstring const label = _("Vertical Space");
110 return label + " (" + space_.asGUIName() + ')';
115 int const arrow_size = 4;
119 bool InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
121 int height = 3 * arrow_size;
122 if (space_.length().len().value() >= 0.0)
123 height = max(height, space_.inPixels(*mi.base.bv));
132 theFontMetrics(font).rectText(label(), w, a, d);
134 height = max(height, a + d);
136 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
137 dim.des = height - dim.asc; // label text
138 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
139 bool const changed = dim_ != dim;
145 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
147 setPosCache(pi, x, y);
149 x += ADD_TO_VSPACE_WIDTH;
151 int const start = y - dim_.asc;
152 int const end = y + dim_.des;
154 // y-values for top arrow
156 // y-values for bottom arrow
159 if (space_.kind() == VSpace::VFILL) {
163 // adding or removing space
164 bool const added = space_.kind() != VSpace::LENGTH ||
165 space_.length().len().value() >= 0.0;
166 ty1 = added ? (start + arrow_size) : start;
167 ty2 = added ? start : (start + arrow_size);
168 by1 = added ? (end - arrow_size) : end;
169 by2 = added ? end : (end - arrow_size);
172 int const midx = x + arrow_size;
173 int const rightx = midx + arrow_size;
181 font.setColor(LColor::added_space);
184 docstring const lab = label();
185 theFontMetrics(font).rectText(lab, w, a, d);
187 pi.pain.rectText(x + 2 * arrow_size + 5,
188 start + (end - start) / 2 + (a - d) / 2,
189 lab, font, LColor::none, LColor::none);
192 pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
193 pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
196 pi.pain.line(x, by1, midx, by2, LColor::added_space);
197 pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
200 pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
204 int InsetVSpace::latex(Buffer const & buf, odocstream & os,
205 OutputParams const &) const
207 os << from_ascii(space_.asLatexCommand(buf.params())) << '\n';
212 int InsetVSpace::plaintext(Buffer const &, odocstream & os,
213 OutputParams const &) const
220 int InsetVSpace::docbook(Buffer const &, odocstream & os,
221 OutputParams const &) const
228 string const InsetVSpaceMailer::name_ = "vspace";
231 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
236 string const InsetVSpaceMailer::inset2string(Buffer const &) const
238 return params2string(inset_.space());
242 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
248 istringstream data(in);
254 if (!lex || name != name_)
255 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
260 vspace = VSpace(vsp);
264 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
267 data << name_ << ' ' << vspace.asLyXCommand();