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"
18 #include "dispatchresult.h"
19 #include "funcrequest.h"
24 #include "metricsinfo.h"
26 #include "frontends/font_metrics.h"
27 #include "frontends/Painter.h"
29 #include "support/std_sstream.h"
31 using std::istringstream;
33 using std::ostringstream;
40 int const ADD_TO_VSPACE_WIDTH = 5;
45 InsetVSpace::InsetVSpace(VSpace const & space)
50 InsetVSpace::~InsetVSpace()
52 InsetVSpaceMailer(*this).hideDialog();
56 std::auto_ptr<InsetBase> InsetVSpace::clone() const
58 return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
63 InsetVSpace::priv_dispatch(FuncRequest const & cmd,
64 idx_type & idx, pos_type & pos)
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(cmd.view());
75 return DispatchResult(true, true);
78 return InsetOld::priv_dispatch(cmd, idx, pos);
83 void InsetVSpace::read(Buffer const &, LyXLex & lex)
87 space_ = VSpace(lex.getString());
92 void InsetVSpace::write(Buffer const &, ostream & os) const
94 os << "VSpace " << space_.asLyXCommand();
98 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
101 int const arrow_size = 4;
102 int const space_size = space_.inPixels(*mi.base.bv);
106 int const min_size = max(3 * arrow_size, font_metrics::maxHeight(font));
108 if (space_.length().len().value() < 0.0)
111 size = max(min_size, space_size);
115 dim.wid = 10 + 2 * ADD_TO_VSPACE_WIDTH;
121 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
123 static std::string const label = _("Vertical Space");
128 x += ADD_TO_VSPACE_WIDTH;
130 int const arrow_size = 4;
131 int const start = y - dim_.asc;
132 int const end = y + dim_.des;
134 // the label to display (if any)
136 // y-values for top arrow
138 // y-values for bottom arrow
141 str = label + " (" + space_.asLyXCommand() + ")";
143 if (space_.kind() == VSpace::VFILL) {
147 // adding or removing space
148 bool const added = space_.kind() != VSpace::LENGTH ||
149 space_.length().len().value() > 0.0;
150 ty1 = added ? (start + arrow_size) : start;
151 ty2 = added ? start : (start + arrow_size);
152 by1 = added ? (end - arrow_size) : end;
153 by2 = added ? end : (end - arrow_size);
156 int const midx = x + arrow_size;
157 int const rightx = midx + arrow_size;
165 font.setColor(LColor::added_space);
168 font_metrics::rectText(str, font, w, a, d);
170 pi.pain.rectText(x + 2 * arrow_size + 5, y + d,
171 str, font, LColor::none, LColor::none);
174 pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
175 pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
178 pi.pain.line(x, by1, midx, by2, LColor::added_space);
179 pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
182 pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
186 int InsetVSpace::latex(Buffer const & buf, ostream & os,
187 OutputParams const &) const
189 os << space_.asLatexCommand(buf.params()) << '\n';
194 int InsetVSpace::plaintext(Buffer const &, ostream & os,
195 OutputParams const &) const
202 int InsetVSpace::linuxdoc(Buffer const &, std::ostream & os,
203 OutputParams const &) const
210 int InsetVSpace::docbook(Buffer const &, std::ostream & os,
211 OutputParams const &) const
218 string const InsetVSpaceMailer::name_ = "vspace";
221 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
226 string const InsetVSpaceMailer::inset2string(Buffer const &) const
228 return params2string(inset_.space_);
232 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
239 istringstream data(in);
245 string const name = lex.getString();
248 // This is part of the inset proper that is usually swallowed
249 // by Buffer::readInset
252 vspace = VSpace(lex.getString());
257 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
260 data << name_ << ' ' << vspace.asLyXCommand();