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"
40 using std::istringstream;
42 using std::ostringstream;
45 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
46 : InsetCollapsable(bp), name_(from_utf8(type))
48 setLabel(_("wrap: ") + floatName(type, bp));
49 FontInfo font = sane_font;
52 font.setColor(Color_collapsable);
56 params_.placement = "o";
57 params_.overhang = Length(0, Length::PCW);
58 params_.width = Length(50, Length::PCW);
62 InsetWrap::~InsetWrap()
64 InsetWrapMailer(*this).hideDialog();
68 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
71 case LFUN_INSET_MODIFY: {
72 InsetWrapParams params;
73 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
74 params_.lines = params.lines;
75 params_.placement = params.placement;
76 params_.overhang = params.overhang;
77 params_.width = params.width;
81 case LFUN_INSET_DIALOG_UPDATE:
82 InsetWrapMailer(*this).updateDialog(&cur.bv());
85 case LFUN_MOUSE_RELEASE: {
86 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
87 InsetWrapMailer(*this).showDialog(&cur.bv());
90 InsetCollapsable::doDispatch(cur, cmd);
95 InsetCollapsable::doDispatch(cur, cmd);
101 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
102 FuncStatus & flag) const
104 switch (cmd.action) {
105 case LFUN_INSET_MODIFY:
106 case LFUN_INSET_DIALOG_UPDATE:
111 return InsetCollapsable::getStatus(cur, cmd, flag);
116 void InsetWrap::updateLabels(Buffer const & buf, ParIterator const & it)
118 Counters & cnts = buf.params().getTextClass().counters();
119 string const saveflt = cnts.current_float();
121 // Tell to captions what the current float is
122 cnts.current_float(params().type);
124 InsetCollapsable::updateLabels(buf, it);
127 cnts.current_float(saveflt);
131 void InsetWrapParams::write(ostream & os) const
133 os << "Wrap " << type << '\n';
134 os << "lines " << lines << "\n";
135 os << "placement " << placement << "\n";
136 os << "overhang " << overhang.asString() << "\n";
137 os << "width \"" << width.asString() << "\"\n";
141 void InsetWrapParams::read(Lexer & lex)
146 if (token == "lines")
149 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
151 // take countermeasures
152 lex.pushToken(token);
157 if (token == "placement")
160 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
162 lex.pushToken(token);
167 if (token == "overhang") {
169 overhang = Length(lex.getString());
171 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
173 lex.pushToken(token);
178 if (token == "width") {
180 width = Length(lex.getString());
182 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
184 lex.pushToken(token);
189 void InsetWrap::write(Buffer const & buf, ostream & os) const
192 InsetCollapsable::write(buf, os);
196 void InsetWrap::read(Buffer const & buf, Lexer & lex)
199 InsetCollapsable::read(buf, lex);
203 void InsetWrap::validate(LaTeXFeatures & features) const
205 features.require("wrapfig");
206 InsetCollapsable::validate(features);
210 Inset * InsetWrap::clone() const
212 return new InsetWrap(*this);
216 docstring const InsetWrap::editMessage() const
218 return _("Opened Wrap Inset");
222 int InsetWrap::latex(Buffer const & buf, odocstream & os,
223 OutputParams const & runparams) const
225 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
226 // no optional argument when lines are zero
227 if (params_.lines != 0)
228 os << '[' << params_.lines << ']';
229 os << '{' << from_ascii(params_.placement) << '}';
230 Length over(params_.overhang);
231 // no optional argument when the value is zero
232 if (over.value() != 0)
233 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
234 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
235 int const i = InsetText::latex(buf, os, runparams);
236 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
241 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
242 OutputParams const & runparams) const
244 os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
245 InsetText::plaintext(buf, os, runparams);
248 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
252 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
253 OutputParams const & runparams) const
256 os << '<' << from_ascii(params_.type) << '>';
257 int const i = InsetText::docbook(buf, os, runparams);
258 os << "</" << from_ascii(params_.type) << '>';
263 bool InsetWrap::insetAllowed(InsetCode code) const
271 return InsetCollapsable::insetAllowed(code);
276 bool InsetWrap::showInsetDialog(BufferView * bv) const
278 if (!InsetText::showInsetDialog(bv))
279 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
284 string const InsetWrapMailer::name_("wrap");
286 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
291 string const InsetWrapMailer::inset2string(Buffer const &) const
293 return params2string(inset_.params());
297 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
299 params = InsetWrapParams();
303 istringstream data(in);
309 if (!lex || name != name_)
310 return print_mailer_error("InsetWrapMailer", in, 1, name_);
312 // This is part of the inset proper that is usually swallowed
313 // by Text::readInset
316 if (!lex || id != "Wrap")
317 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
319 // We have to read the type here!
325 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
328 data << name_ << ' ';