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
119 int height = 3 * arrow_size;
120 if (space_.length().len().value() >= 0.0)
121 height = max(height, space_.inPixels(*mi.base.bv));
130 font_metrics::rectText(label(), font, w, a, d);
132 height = max(height, a + d);
134 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
135 dim.des = height - dim.asc; // label text
136 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
141 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
143 setPosCache(pi, x, y);
145 x += ADD_TO_VSPACE_WIDTH;
147 int const start = y - dim_.asc;
148 int const end = y + dim_.des;
150 // y-values for top arrow
152 // y-values for bottom arrow
155 if (space_.kind() == VSpace::VFILL) {
159 // adding or removing space
160 bool const added = space_.kind() != VSpace::LENGTH ||
161 space_.length().len().value() >= 0.0;
162 ty1 = added ? (start + arrow_size) : start;
163 ty2 = added ? start : (start + arrow_size);
164 by1 = added ? (end - arrow_size) : end;
165 by2 = added ? end : (end - arrow_size);
168 int const midx = x + arrow_size;
169 int const rightx = midx + arrow_size;
177 font.setColor(LColor::added_space);
180 font_metrics::rectText(label(), font, w, a, d);
182 pi.pain.rectText(x + 2 * arrow_size + 5,
183 start + (end - start) / 2 + (a - d) / 2,
184 label(), font, LColor::none, LColor::none);
187 pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
188 pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
191 pi.pain.line(x, by1, midx, by2, LColor::added_space);
192 pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
195 pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
199 int InsetVSpace::latex(Buffer const & buf, ostream & os,
200 OutputParams const &) const
202 os << space_.asLatexCommand(buf.params()) << '\n';
207 int InsetVSpace::plaintext(Buffer const &, ostream & os,
208 OutputParams const &) const
215 int InsetVSpace::linuxdoc(Buffer const &, std::ostream & os,
216 OutputParams const &) const
223 int InsetVSpace::docbook(Buffer const &, std::ostream & os,
224 OutputParams const &) const
231 string const InsetVSpaceMailer::name_ = "vspace";
234 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
239 string const InsetVSpaceMailer::inset2string(Buffer const &) const
241 return params2string(inset_.space());
245 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
251 istringstream data(in);
257 if (!lex || name != name_)
258 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
263 vspace = VSpace(vsp);
267 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
270 data << name_ << ' ' << vspace.asLyXCommand();