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"
21 #include "support/debug.h"
22 #include "DispatchResult.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "support/gettext.h"
28 #include "LaTeXFeatures.h"
30 #include "OutputParams.h"
31 #include "TextClass.h"
32 #include "TocBackend.h"
34 #include "support/convert.h"
35 #include "support/docstream.h"
42 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
43 : InsetCollapsable(bp), name_(from_utf8(type))
45 setLabel(_("wrap: ") + floatName(type, bp));
48 params_.placement = "o";
49 params_.overhang = Length(0, Length::PCW);
50 params_.width = Length(50, Length::PCW);
54 InsetWrap::~InsetWrap()
56 InsetWrapMailer(*this).hideDialog();
60 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
63 case LFUN_INSET_MODIFY: {
64 InsetWrapParams params;
65 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
66 params_.lines = params.lines;
67 params_.placement = params.placement;
68 params_.overhang = params.overhang;
69 params_.width = params.width;
73 case LFUN_INSET_DIALOG_UPDATE:
74 InsetWrapMailer(*this).updateDialog(&cur.bv());
77 case LFUN_MOUSE_RELEASE: {
78 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
79 InsetWrapMailer(*this).showDialog(&cur.bv());
82 InsetCollapsable::doDispatch(cur, cmd);
87 InsetCollapsable::doDispatch(cur, cmd);
93 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
94 FuncStatus & flag) const
97 case LFUN_INSET_MODIFY:
98 case LFUN_INSET_DIALOG_UPDATE:
103 return InsetCollapsable::getStatus(cur, cmd, flag);
108 void InsetWrap::updateLabels(Buffer const & buf, ParIterator const & it)
110 Counters & cnts = buf.params().getTextClass().counters();
111 string const saveflt = cnts.current_float();
113 // Tell to captions what the current float is
114 cnts.current_float(params().type);
116 InsetCollapsable::updateLabels(buf, it);
119 cnts.current_float(saveflt);
123 void InsetWrapParams::write(ostream & os) const
125 os << "Wrap " << type << '\n';
126 os << "lines " << lines << "\n";
127 os << "placement " << placement << "\n";
128 os << "overhang " << overhang.asString() << "\n";
129 os << "width \"" << width.asString() << "\"\n";
133 void InsetWrapParams::read(Lexer & lex)
138 if (token == "lines")
141 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
143 // take countermeasures
144 lex.pushToken(token);
149 if (token == "placement")
152 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
154 lex.pushToken(token);
159 if (token == "overhang") {
161 overhang = Length(lex.getString());
163 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
165 lex.pushToken(token);
170 if (token == "width") {
172 width = Length(lex.getString());
174 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
176 lex.pushToken(token);
181 void InsetWrap::write(Buffer const & buf, ostream & os) const
184 InsetCollapsable::write(buf, os);
188 void InsetWrap::read(Buffer const & buf, Lexer & lex)
191 InsetCollapsable::read(buf, lex);
195 void InsetWrap::validate(LaTeXFeatures & features) const
197 features.require("wrapfig");
198 InsetCollapsable::validate(features);
202 Inset * InsetWrap::clone() const
204 return new InsetWrap(*this);
208 docstring const InsetWrap::editMessage() const
210 return _("Opened Wrap Inset");
214 int InsetWrap::latex(Buffer const & buf, odocstream & os,
215 OutputParams const & runparams) const
217 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
218 // no optional argument when lines are zero
219 if (params_.lines != 0)
220 os << '[' << params_.lines << ']';
221 os << '{' << from_ascii(params_.placement) << '}';
222 Length over(params_.overhang);
223 // no optional argument when the value is zero
224 if (over.value() != 0)
225 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
226 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
227 int const i = InsetText::latex(buf, os, runparams);
228 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
233 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
234 OutputParams const & runparams) const
236 os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
237 InsetText::plaintext(buf, os, runparams);
240 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
244 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
245 OutputParams const & runparams) const
248 os << '<' << from_ascii(params_.type) << '>';
249 int const i = InsetText::docbook(buf, os, runparams);
250 os << "</" << from_ascii(params_.type) << '>';
255 bool InsetWrap::insetAllowed(InsetCode code) const
263 return InsetCollapsable::insetAllowed(code);
268 bool InsetWrap::showInsetDialog(BufferView * bv) const
270 if (!InsetText::showInsetDialog(bv))
271 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
276 string const InsetWrapMailer::name_("wrap");
278 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
283 string const InsetWrapMailer::inset2string(Buffer const &) const
285 return params2string(inset_.params());
289 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
291 params = InsetWrapParams();
295 istringstream data(in);
301 if (!lex || name != name_)
302 return print_mailer_error("InsetWrapMailer", in, 1, name_);
304 // This is part of the inset proper that is usually swallowed
305 // by Text::readInset
308 if (!lex || id != "Wrap")
309 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
311 // We have to read the type here!
317 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
320 data << name_ << ' ';