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"
20 #include "DispatchResult.h"
22 #include "FloatList.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
29 #include "OutputParams.h"
30 #include "Paragraph.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 InsetWrapParams::write(ostream & os) const
114 os << "Wrap " << type << '\n';
116 if (!placement.empty())
117 os << "placement " << placement << "\n";
119 os << "width \"" << width.asString() << "\"\n";
123 void InsetWrapParams::read(Lexer & lex)
127 if (token == "placement")
130 // take countermeasures
131 lex.pushToken(token);
136 if (token == "width") {
138 width = Length(lex.getString());
140 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
142 // take countermeasures
143 lex.pushToken(token);
148 void InsetWrap::write(Buffer const & buf, ostream & os) const
151 InsetCollapsable::write(buf, os);
155 void InsetWrap::read(Buffer const & buf, Lexer & lex)
158 InsetCollapsable::read(buf, lex);
162 void InsetWrap::validate(LaTeXFeatures & features) const
164 features.require("floatflt");
165 InsetCollapsable::validate(features);
169 auto_ptr<Inset> InsetWrap::doClone() const
171 return auto_ptr<Inset>(new InsetWrap(*this));
175 docstring const InsetWrap::editMessage() const
177 return _("Opened Wrap Inset");
181 int InsetWrap::latex(Buffer const & buf, odocstream & os,
182 OutputParams const & runparams) const
184 os << "\\begin{floating" << from_ascii(params_.type) << '}';
185 if (!params_.placement.empty())
186 os << '[' << from_ascii(params_.placement) << ']';
187 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
188 int const i = InsetText::latex(buf, os, runparams);
189 os << "\\end{floating" << from_ascii(params_.type) << "}%\n";
194 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
195 OutputParams const & runparams) const
197 os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
198 InsetText::plaintext(buf, os, runparams);
201 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
205 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
206 OutputParams const & runparams) const
209 os << '<' << from_ascii(params_.type) << '>';
210 int const i = InsetText::docbook(buf, os, runparams);
211 os << "</" << from_ascii(params_.type) << '>';
216 bool InsetWrap::insetAllowed(Inset::Code code) const
224 return InsetCollapsable::insetAllowed(code);
229 bool InsetWrap::showInsetDialog(BufferView * bv) const
231 if (!InsetText::showInsetDialog(bv))
232 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
237 void InsetWrap::addToToc(TocList & toclist, Buffer const & buf) const
239 // Is there a need to provide a list of wrap insets?
244 string const InsetWrapMailer::name_("wrap");
246 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
251 string const InsetWrapMailer::inset2string(Buffer const &) const
253 return params2string(inset_.params());
257 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
259 params = InsetWrapParams();
263 istringstream data(in);
269 if (!lex || name != name_)
270 return print_mailer_error("InsetWrapMailer", in, 1, name_);
272 // This is part of the inset proper that is usually swallowed
273 // by Text::readInset
276 if (!lex || id != "Wrap")
277 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
279 // We have to read the type here!
285 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
288 data << name_ << ' ';