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"
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));
50 Font font(Font::ALL_SANE);
53 font.setColor(Color::collapsable);
56 params_.width = Length(50, Length::PCW);
60 InsetWrap::~InsetWrap()
62 InsetWrapMailer(*this).hideDialog();
66 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
69 case LFUN_INSET_MODIFY: {
70 InsetWrapParams params;
71 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
72 params_.placement = params.placement;
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';
131 if (!placement.empty())
132 os << "placement " << placement << "\n";
134 os << "width \"" << width.asString() << "\"\n";
138 void InsetWrapParams::read(Lexer & lex)
142 if (token == "placement")
145 // take countermeasures
146 lex.pushToken(token);
151 if (token == "width") {
153 width = Length(lex.getString());
155 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
157 // take countermeasures
158 lex.pushToken(token);
163 void InsetWrap::write(Buffer const & buf, ostream & os) const
166 InsetCollapsable::write(buf, os);
170 void InsetWrap::read(Buffer const & buf, Lexer & lex)
173 InsetCollapsable::read(buf, lex);
177 void InsetWrap::validate(LaTeXFeatures & features) const
179 features.require("floatflt");
180 InsetCollapsable::validate(features);
184 auto_ptr<Inset> InsetWrap::doClone() const
186 return auto_ptr<Inset>(new InsetWrap(*this));
190 docstring const InsetWrap::editMessage() const
192 return _("Opened Wrap Inset");
196 int InsetWrap::latex(Buffer const & buf, odocstream & os,
197 OutputParams const & runparams) const
199 os << "\\begin{floating" << from_ascii(params_.type) << '}';
200 if (!params_.placement.empty())
201 os << '[' << from_ascii(params_.placement) << ']';
202 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
203 int const i = InsetText::latex(buf, os, runparams);
204 os << "\\end{floating" << from_ascii(params_.type) << "}%\n";
209 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
210 OutputParams const & runparams) const
212 os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
213 InsetText::plaintext(buf, os, runparams);
216 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
220 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
221 OutputParams const & runparams) const
224 os << '<' << from_ascii(params_.type) << '>';
225 int const i = InsetText::docbook(buf, os, runparams);
226 os << "</" << from_ascii(params_.type) << '>';
231 bool InsetWrap::insetAllowed(Inset::Code code) const
239 return InsetCollapsable::insetAllowed(code);
244 bool InsetWrap::showInsetDialog(BufferView * bv) const
246 if (!InsetText::showInsetDialog(bv))
247 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
252 string const InsetWrapMailer::name_("wrap");
254 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
259 string const InsetWrapMailer::inset2string(Buffer const &) const
261 return params2string(inset_.params());
265 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
267 params = InsetWrapParams();
271 istringstream data(in);
277 if (!lex || name != name_)
278 return print_mailer_error("InsetWrapMailer", in, 1, name_);
280 // This is part of the inset proper that is usually swallowed
281 // by Text::readInset
284 if (!lex || id != "Wrap")
285 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
287 // We have to read the type here!
293 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
296 data << name_ << ' ';