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"
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::doClone() const
59 return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
63 void InsetVSpace::doDispatch(LCursor & cur, FuncRequest & cmd)
67 case LFUN_INSET_MODIFY: {
68 InsetVSpaceMailer::string2params(cmd.argument, space_);
72 case LFUN_MOUSE_PRESS:
73 InsetVSpaceMailer(*this).showDialog(&cur.bv());
77 InsetBase::doDispatch(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");
132 setPosCache(pi, x, y);
134 x += ADD_TO_VSPACE_WIDTH;
136 int const arrow_size = 4;
137 int const start = y - dim_.asc;
138 int const end = y + dim_.des;
140 // the label to display (if any)
142 // y-values for top arrow
144 // y-values for bottom arrow
147 str = label + " (" + space_.asGUIName() + ")";
149 if (space_.kind() == VSpace::VFILL) {
153 // adding or removing space
154 bool const added = space_.kind() != VSpace::LENGTH ||
155 space_.length().len().value() > 0.0;
156 ty1 = added ? (start + arrow_size) : start;
157 ty2 = added ? start : (start + arrow_size);
158 by1 = added ? (end - arrow_size) : end;
159 by2 = added ? end : (end - arrow_size);
162 int const midx = x + arrow_size;
163 int const rightx = midx + arrow_size;
171 font.setColor(LColor::added_space);
174 font_metrics::rectText(str, font, w, a, d);
176 pi.pain.rectText(x + 2 * arrow_size + 5, y + d,
177 str, font, LColor::none, LColor::none);
180 pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
181 pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
184 pi.pain.line(x, by1, midx, by2, LColor::added_space);
185 pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
188 pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
192 int InsetVSpace::latex(Buffer const & buf, ostream & os,
193 OutputParams const &) const
195 os << space_.asLatexCommand(buf.params()) << '\n';
200 int InsetVSpace::plaintext(Buffer const &, ostream & os,
201 OutputParams const &) const
208 int InsetVSpace::linuxdoc(Buffer const &, std::ostream & os,
209 OutputParams const &) const
216 int InsetVSpace::docbook(Buffer const &, std::ostream & os,
217 OutputParams const &) const
224 string const InsetVSpaceMailer::name_ = "vspace";
227 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
232 string const InsetVSpaceMailer::inset2string(Buffer const &) const
234 return params2string(inset_.space());
238 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
244 istringstream data(in);
250 if (!lex || name != name_)
251 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
256 vspace = VSpace(vsp);
260 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
263 data << name_ << ' ' << vspace.asLyXCommand();