3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetWrap.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
21 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
27 #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 Font font(Font::ALL_SANE);
52 font.setColor(Color::collapsable);
55 params_.width = Length(50, Length::PCW);
59 InsetWrap::~InsetWrap()
61 InsetWrapMailer(*this).hideDialog();
65 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
68 case LFUN_INSET_MODIFY: {
69 InsetWrapParams params;
70 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
71 params_.placement = params.placement;
72 params_.width = params.width;
76 case LFUN_INSET_DIALOG_UPDATE:
77 InsetWrapMailer(*this).updateDialog(&cur.bv());
80 case LFUN_MOUSE_RELEASE: {
81 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
82 InsetWrapMailer(*this).showDialog(&cur.bv());
85 InsetCollapsable::doDispatch(cur, cmd);
90 InsetCollapsable::doDispatch(cur, cmd);
96 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
97 FuncStatus & flag) const
100 case LFUN_INSET_MODIFY:
101 case LFUN_INSET_DIALOG_UPDATE:
106 return InsetCollapsable::getStatus(cur, cmd, flag);
111 void InsetWrap::updateLabels(Buffer const & buf, ParIterator const & it)
113 Counters & cnts = buf.params().getTextClass().counters();
114 string const saveflt = cnts.current_float();
116 // Tell to captions what the current float is
117 cnts.current_float(params().type);
119 InsetCollapsable::updateLabels(buf, it);
122 cnts.current_float(saveflt);
126 void InsetWrapParams::write(ostream & os) const
128 os << "Wrap " << type << '\n';
130 if (!placement.empty())
131 os << "placement " << placement << "\n";
133 os << "width \"" << width.asString() << "\"\n";
137 void InsetWrapParams::read(Lexer & lex)
141 if (token == "placement")
144 // take countermeasures
145 lex.pushToken(token);
150 if (token == "width") {
152 width = Length(lex.getString());
154 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
156 // take countermeasures
157 lex.pushToken(token);
162 void InsetWrap::write(Buffer const & buf, ostream & os) const
165 InsetCollapsable::write(buf, os);
169 void InsetWrap::read(Buffer const & buf, Lexer & lex)
172 InsetCollapsable::read(buf, lex);
176 void InsetWrap::validate(LaTeXFeatures & features) const
178 features.require("floatflt");
179 InsetCollapsable::validate(features);
183 Inset * InsetWrap::clone() const
185 return new InsetWrap(*this);
189 docstring const InsetWrap::editMessage() const
191 return _("Opened Wrap Inset");
195 int InsetWrap::latex(Buffer const & buf, odocstream & os,
196 OutputParams const & runparams) const
198 os << "\\begin{floating" << from_ascii(params_.type) << '}';
199 if (!params_.placement.empty())
200 os << '[' << from_ascii(params_.placement) << ']';
201 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
202 int const i = InsetText::latex(buf, os, runparams);
203 os << "\\end{floating" << from_ascii(params_.type) << "}%\n";
208 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
209 OutputParams const & runparams) const
211 os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
212 InsetText::plaintext(buf, os, runparams);
215 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
219 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
220 OutputParams const & runparams) const
223 os << '<' << from_ascii(params_.type) << '>';
224 int const i = InsetText::docbook(buf, os, runparams);
225 os << "</" << from_ascii(params_.type) << '>';
230 bool InsetWrap::insetAllowed(Inset::Code code) const
238 return InsetCollapsable::insetAllowed(code);
243 bool InsetWrap::showInsetDialog(BufferView * bv) const
245 if (!InsetText::showInsetDialog(bv))
246 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
251 string const InsetWrapMailer::name_("wrap");
253 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
258 string const InsetWrapMailer::inset2string(Buffer const &) const
260 return params2string(inset_.params());
264 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
266 params = InsetWrapParams();
270 istringstream data(in);
276 if (!lex || name != name_)
277 return print_mailer_error("InsetWrapMailer", in, 1, name_);
279 // This is part of the inset proper that is usually swallowed
280 // by Text::readInset
283 if (!lex || id != "Wrap")
284 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
286 // We have to read the type here!
292 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
295 data << name_ << ' ';