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 "dispatchresult.h"
20 #include "funcrequest.h"
25 #include "metricsinfo.h"
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
30 #include "support/std_sstream.h"
32 using std::istringstream;
34 using std::ostringstream;
41 int const ADD_TO_VSPACE_WIDTH = 5;
46 InsetVSpace::InsetVSpace(VSpace const & space)
51 InsetVSpace::~InsetVSpace()
53 InsetVSpaceMailer(*this).hideDialog();
57 std::auto_ptr<InsetBase> InsetVSpace::clone() const
59 return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
64 InsetVSpace::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
68 case LFUN_INSET_MODIFY: {
69 InsetVSpaceMailer::string2params(cmd.argument, space_);
70 return DispatchResult(true, true);
73 case LFUN_MOUSE_PRESS:
74 InsetVSpaceMailer(*this).showDialog(&cur.bv());
75 return DispatchResult(true, true);
78 return InsetOld::priv_dispatch(cur, cmd);
83 void InsetVSpace::read(Buffer const &, LyXLex & lex)
85 BOOST_ASSERT(lex.isOK());
93 if (end_token != "\\end_inset")
94 lex.printError("Missing \\end_inset at this point. "
99 void InsetVSpace::write(Buffer const &, ostream & os) const
101 os << "VSpace " << space_.asLyXCommand();
105 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
108 int const arrow_size = 4;
109 int const space_size = space_.inPixels(*mi.base.bv);
113 int const min_size = max(3 * arrow_size, font_metrics::maxHeight(font));
115 if (space_.length().len().value() < 0.0)
118 size = max(min_size, space_size);
122 dim.wid = 10 + 2 * ADD_TO_VSPACE_WIDTH;
128 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
130 static std::string const label = _("Vertical Space");
135 x += ADD_TO_VSPACE_WIDTH;
137 int const arrow_size = 4;
138 int const start = y - dim_.asc;
139 int const end = y + dim_.des;
141 // the label to display (if any)
143 // y-values for top arrow
145 // y-values for bottom arrow
148 str = label + " (" + space_.asLyXCommand() + ")";
150 if (space_.kind() == VSpace::VFILL) {
154 // adding or removing space
155 bool const added = space_.kind() != VSpace::LENGTH ||
156 space_.length().len().value() > 0.0;
157 ty1 = added ? (start + arrow_size) : start;
158 ty2 = added ? start : (start + arrow_size);
159 by1 = added ? (end - arrow_size) : end;
160 by2 = added ? end : (end - arrow_size);
163 int const midx = x + arrow_size;
164 int const rightx = midx + arrow_size;
172 font.setColor(LColor::added_space);
175 font_metrics::rectText(str, font, w, a, d);
177 pi.pain.rectText(x + 2 * arrow_size + 5, y + d,
178 str, font, LColor::none, LColor::none);
181 pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
182 pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
185 pi.pain.line(x, by1, midx, by2, LColor::added_space);
186 pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
189 pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
193 int InsetVSpace::latex(Buffer const & buf, ostream & os,
194 OutputParams const &) const
196 os << space_.asLatexCommand(buf.params()) << '\n';
201 int InsetVSpace::plaintext(Buffer const &, ostream & os,
202 OutputParams const &) const
209 int InsetVSpace::linuxdoc(Buffer const &, std::ostream & os,
210 OutputParams const &) const
217 int InsetVSpace::docbook(Buffer const &, std::ostream & os,
218 OutputParams const &) const
225 string const InsetVSpaceMailer::name_ = "vspace";
228 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
233 string const InsetVSpaceMailer::inset2string(Buffer const &) const
235 return params2string(inset_.space());
239 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
245 istringstream data(in);
251 if (!lex || name != name_)
252 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
257 vspace = VSpace(vsp);
261 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
264 data << name_ << ' ' << vspace.asLyXCommand();