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 "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
28 #include "OutputParams.h"
29 #include "TextClass.h"
30 #include "TocBackend.h"
32 #include "support/convert.h"
33 #include "support/docstream.h"
34 #include "support/debug.h"
35 #include "support/gettext.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(ParIterator const & it)
110 setLabel(_("wrap: ") + floatName(params_.type, buffer().params()));
111 Counters & cnts = buffer().params().documentClass().counters();
112 string const saveflt = cnts.current_float();
114 // Tell to captions what the current float is
115 cnts.current_float(params().type);
117 InsetCollapsable::updateLabels(it);
120 cnts.current_float(saveflt);
124 void InsetWrapParams::write(ostream & os) const
126 os << "Wrap " << type << '\n';
127 os << "lines " << lines << "\n";
128 os << "placement " << placement << "\n";
129 os << "overhang " << overhang.asString() << "\n";
130 os << "width \"" << width.asString() << "\"\n";
134 void InsetWrapParams::read(Lexer & lex)
139 if (token == "lines")
142 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
144 // take countermeasures
145 lex.pushToken(token);
150 if (token == "placement")
153 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
155 lex.pushToken(token);
160 if (token == "overhang") {
162 overhang = Length(lex.getString());
164 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
166 lex.pushToken(token);
171 if (token == "width") {
173 width = Length(lex.getString());
175 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
177 lex.pushToken(token);
182 void InsetWrap::write(ostream & os) const
185 InsetCollapsable::write(os);
189 void InsetWrap::read(Lexer & lex)
192 InsetCollapsable::read(lex);
196 void InsetWrap::validate(LaTeXFeatures & features) const
198 features.require("wrapfig");
199 InsetCollapsable::validate(features);
203 Inset * InsetWrap::clone() const
205 return new InsetWrap(*this);
209 docstring InsetWrap::editMessage() const
211 return _("Opened Wrap Inset");
215 int InsetWrap::latex(odocstream & os, 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(os, runparams);
228 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
233 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
235 os << '[' << buffer().B_("wrap") << ' '
236 << floatName(params_.type, buffer().params()) << ":\n";
237 InsetText::plaintext(os, runparams);
240 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
244 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
247 os << '<' << from_ascii(params_.type) << '>';
248 int const i = InsetText::docbook(os, runparams);
249 os << "</" << from_ascii(params_.type) << '>';
254 bool InsetWrap::insetAllowed(InsetCode code) const
262 return InsetCollapsable::insetAllowed(code);
267 bool InsetWrap::showInsetDialog(BufferView * bv) const
269 if (!InsetText::showInsetDialog(bv))
270 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
275 string const InsetWrapMailer::name_("wrap");
277 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
282 string const InsetWrapMailer::inset2string(Buffer const &) const
284 return params2string(inset_.params());
288 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
290 params = InsetWrapParams();
294 istringstream data(in);
300 if (!lex || name != name_)
301 return print_mailer_error("InsetWrapMailer", in, 1, name_);
303 // This is part of the inset proper that is usually swallowed
304 // by Text::readInset
307 if (!lex || id != "Wrap")
308 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
310 // We have to read the type here!
316 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
319 data << name_ << ' ';