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/Application.h"
27 #include "frontends/FontLoader.h"
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
35 using std::istringstream;
37 using std::ostringstream;
44 int const ADD_TO_VSPACE_WIDTH = 5;
49 InsetVSpace::InsetVSpace(VSpace const & space)
54 InsetVSpace::~InsetVSpace()
56 InsetVSpaceMailer(*this).hideDialog();
60 std::auto_ptr<InsetBase> InsetVSpace::doClone() const
62 return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
66 void InsetVSpace::doDispatch(LCursor & cur, FuncRequest & cmd)
70 case LFUN_INSET_MODIFY: {
71 InsetVSpaceMailer::string2params(lyx::to_utf8(cmd.argument()), space_);
75 case LFUN_MOUSE_RELEASE:
76 InsetVSpaceMailer(*this).showDialog(&cur.bv());
80 InsetBase::doDispatch(cur, cmd);
86 void InsetVSpace::read(Buffer const &, LyXLex & lex)
88 BOOST_ASSERT(lex.isOK());
96 if (end_token != "\\end_inset")
97 lex.printError("Missing \\end_inset at this point. "
102 void InsetVSpace::write(Buffer const &, ostream & os) const
104 os << "VSpace " << space_.asLyXCommand();
108 string const InsetVSpace::label() const
111 static string const label = lyx::to_utf8(_("Vertical Space"));
112 return label + " (" + space_.asGUIName() + ')';
117 int const arrow_size = 4;
121 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
123 int height = 3 * arrow_size;
124 if (space_.length().len().value() >= 0.0)
125 height = max(height, space_.inPixels(*mi.base.bv));
134 string lab = label();
135 docstring dlab(lab.begin(), lab.end());
136 theApp->fontLoader().metrics(font).rectText(dlab, w, a, d);
138 height = max(height, a + d);
140 dim.asc = height / 2 + (a - d) / 2; // align cursor with the
141 dim.des = height - dim.asc; // label text
142 dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
147 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
149 setPosCache(pi, x, y);
151 x += ADD_TO_VSPACE_WIDTH;
153 int const start = y - dim_.asc;
154 int const end = y + dim_.des;
156 // y-values for top arrow
158 // y-values for bottom arrow
161 if (space_.kind() == VSpace::VFILL) {
165 // adding or removing space
166 bool const added = space_.kind() != VSpace::LENGTH ||
167 space_.length().len().value() >= 0.0;
168 ty1 = added ? (start + arrow_size) : start;
169 ty2 = added ? start : (start + arrow_size);
170 by1 = added ? (end - arrow_size) : end;
171 by2 = added ? end : (end - arrow_size);
174 int const midx = x + arrow_size;
175 int const rightx = midx + arrow_size;
183 font.setColor(LColor::added_space);
186 string lab = label();
187 docstring dlab(lab.begin(), lab.end());
188 theApp->fontLoader().metrics(font).rectText(dlab, w, a, d);
190 pi.pain.rectText(x + 2 * arrow_size + 5,
191 start + (end - start) / 2 + (a - d) / 2,
192 dlab, font, LColor::none, LColor::none);
195 pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
196 pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
199 pi.pain.line(x, by1, midx, by2, LColor::added_space);
200 pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
203 pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
207 int InsetVSpace::latex(Buffer const & buf, ostream & os,
208 OutputParams const &) const
210 os << space_.asLatexCommand(buf.params()) << '\n';
215 int InsetVSpace::plaintext(Buffer const &, 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();