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 string const InsetVSpace::label() const
107 static string const label = _("Vertical Space");
108 return label + " (" + space_.asGUIName() + ')';
113 int const arrow_size = 4;
117 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
120 int const space_size = space_.inPixels(*mi.base.bv);
124 int const min_size = max(3 * arrow_size, font_metrics::maxHeight(font));
126 if (space_.length().len().value() < 0.0)
129 size = max(min_size, space_size);
137 font_metrics::rectText(label(), font, w, a, d);
138 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
144 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
146 setPosCache(pi, x, y);
148 x += ADD_TO_VSPACE_WIDTH;
150 int const start = y - dim_.asc;
151 int const end = y + dim_.des;
153 // the label to display (if any)
154 string const str = label();
155 // y-values for top arrow
157 // y-values for bottom arrow
160 if (space_.kind() == VSpace::VFILL) {
164 // adding or removing space
165 bool const added = space_.kind() != VSpace::LENGTH ||
166 space_.length().len().value() > 0.0;
167 ty1 = added ? (start + arrow_size) : start;
168 ty2 = added ? start : (start + arrow_size);
169 by1 = added ? (end - arrow_size) : end;
170 by2 = added ? end : (end - arrow_size);
173 int const midx = x + arrow_size;
174 int const rightx = midx + arrow_size;
182 font.setColor(LColor::added_space);
185 font_metrics::rectText(label(), font, w, a, d);
187 pi.pain.rectText(x + 2 * arrow_size + 5, y + d,
188 str, font, LColor::none, LColor::none);
191 pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
192 pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
195 pi.pain.line(x, by1, midx, by2, LColor::added_space);
196 pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
199 pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
203 int InsetVSpace::latex(Buffer const & buf, ostream & os,
204 OutputParams const &) const
206 os << space_.asLatexCommand(buf.params()) << '\n';
211 int InsetVSpace::plaintext(Buffer const &, ostream & os,
212 OutputParams const &) const
219 int InsetVSpace::linuxdoc(Buffer const &, std::ostream & os,
220 OutputParams const &) const
227 int InsetVSpace::docbook(Buffer const &, std::ostream & os,
228 OutputParams const &) const
235 string const InsetVSpaceMailer::name_ = "vspace";
238 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
243 string const InsetVSpaceMailer::inset2string(Buffer const &) const
245 return params2string(inset_.space());
249 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
255 istringstream data(in);
261 if (!lex || name != name_)
262 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
267 vspace = VSpace(vsp);
271 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
274 data << name_ << ' ' << vspace.asLyXCommand();