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 "InsetWrap.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
22 #include "DispatchResult.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
30 #include "OutputParams.h"
31 #include "TocBackend.h"
33 #include "support/convert.h"
34 #include "support/docstream.h"
41 using std::istringstream;
43 using std::ostringstream;
46 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
47 : InsetCollapsable(bp), name_(from_utf8(type))
49 setLabel(_("wrap: ") + floatName(type, bp));
52 params_.placement = "o";
53 params_.overhang = Length(0, Length::PCW);
54 params_.width = Length(50, Length::PCW);
58 InsetWrap::~InsetWrap()
60 InsetWrapMailer(*this).hideDialog();
64 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
67 case LFUN_INSET_MODIFY: {
68 InsetWrapParams params;
69 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
70 params_.lines = params.lines;
71 params_.placement = params.placement;
72 params_.overhang = params.overhang;
73 params_.width = params.width;
77 case LFUN_INSET_DIALOG_UPDATE:
78 InsetWrapMailer(*this).updateDialog(&cur.bv());
81 case LFUN_MOUSE_RELEASE: {
82 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
83 InsetWrapMailer(*this).showDialog(&cur.bv());
86 InsetCollapsable::doDispatch(cur, cmd);
91 InsetCollapsable::doDispatch(cur, cmd);
97 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
98 FuncStatus & flag) const
100 switch (cmd.action) {
101 case LFUN_INSET_MODIFY:
102 case LFUN_INSET_DIALOG_UPDATE:
107 return InsetCollapsable::getStatus(cur, cmd, flag);
112 void InsetWrap::updateLabels(Buffer const & buf, ParIterator const & it)
114 Counters & cnts = buf.params().getTextClass().counters();
115 string const saveflt = cnts.current_float();
117 // Tell to captions what the current float is
118 cnts.current_float(params().type);
120 InsetCollapsable::updateLabels(buf, it);
123 cnts.current_float(saveflt);
127 void InsetWrapParams::write(ostream & os) const
129 os << "Wrap " << type << '\n';
130 os << "lines " << lines << "\n";
131 os << "placement " << placement << "\n";
132 os << "overhang " << overhang.asString() << "\n";
133 os << "width \"" << width.asString() << "\"\n";
137 void InsetWrapParams::read(Lexer & lex)
142 if (token == "lines")
145 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
147 // take countermeasures
148 lex.pushToken(token);
153 if (token == "placement")
156 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
158 lex.pushToken(token);
163 if (token == "overhang") {
165 overhang = Length(lex.getString());
167 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
169 lex.pushToken(token);
174 if (token == "width") {
176 width = Length(lex.getString());
178 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
180 lex.pushToken(token);
185 void InsetWrap::write(Buffer const & buf, ostream & os) const
188 InsetCollapsable::write(buf, os);
192 void InsetWrap::read(Buffer const & buf, Lexer & lex)
195 InsetCollapsable::read(buf, lex);
199 void InsetWrap::validate(LaTeXFeatures & features) const
201 features.require("wrapfig");
202 InsetCollapsable::validate(features);
206 Inset * InsetWrap::clone() const
208 return new InsetWrap(*this);
212 docstring const InsetWrap::editMessage() const
214 return _("Opened Wrap Inset");
218 int InsetWrap::latex(Buffer const & buf, odocstream & os,
219 OutputParams const & runparams) const
221 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
222 // no optional argument when lines are zero
223 if (params_.lines != 0)
224 os << '[' << params_.lines << ']';
225 os << '{' << from_ascii(params_.placement) << '}';
226 Length over(params_.overhang);
227 // no optional argument when the value is zero
228 if (over.value() != 0)
229 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
230 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
231 int const i = InsetText::latex(buf, os, runparams);
232 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
237 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
238 OutputParams const & runparams) const
240 os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
241 InsetText::plaintext(buf, os, runparams);
244 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
248 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
249 OutputParams const & runparams) const
252 os << '<' << from_ascii(params_.type) << '>';
253 int const i = InsetText::docbook(buf, os, runparams);
254 os << "</" << from_ascii(params_.type) << '>';
259 bool InsetWrap::insetAllowed(InsetCode code) const
267 return InsetCollapsable::insetAllowed(code);
272 bool InsetWrap::showInsetDialog(BufferView * bv) const
274 if (!InsetText::showInsetDialog(bv))
275 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
280 string const InsetWrapMailer::name_("wrap");
282 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
287 string const InsetWrapMailer::inset2string(Buffer const &) const
289 return params2string(inset_.params());
293 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
295 params = InsetWrapParams();
299 istringstream data(in);
305 if (!lex || name != name_)
306 return print_mailer_error("InsetWrapMailer", in, 1, name_);
308 // This is part of the inset proper that is usually swallowed
309 // by Text::readInset
312 if (!lex || id != "Wrap")
313 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
315 // We have to read the type here!
321 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
324 data << name_ << ' ';