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)
49 setLabel(_("wrap: ") + floatName(type, bp));
50 LyXFont font(LyXFont::ALL_SANE);
53 font.setColor(LColor::collapsable);
56 params_.width = LyXLength(50, LyXLength::PCW);
57 setInsetName(from_utf8(type));
61 InsetWrap::~InsetWrap()
63 InsetWrapMailer(*this).hideDialog();
67 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
70 case LFUN_INSET_MODIFY: {
71 InsetWrapParams params;
72 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
73 params_.placement = params.placement;
74 params_.width = params.width;
78 case LFUN_INSET_DIALOG_UPDATE:
79 InsetWrapMailer(*this).updateDialog(&cur.bv());
82 case LFUN_MOUSE_RELEASE: {
83 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
84 InsetWrapMailer(*this).showDialog(&cur.bv());
87 InsetCollapsable::doDispatch(cur, cmd);
92 InsetCollapsable::doDispatch(cur, cmd);
98 bool InsetWrap::getStatus(LCursor & cur, FuncRequest const & cmd,
99 FuncStatus & flag) const
101 switch (cmd.action) {
102 case LFUN_INSET_MODIFY:
103 case LFUN_INSET_DIALOG_UPDATE:
108 return InsetCollapsable::getStatus(cur, cmd, flag);
113 void InsetWrapParams::write(ostream & os) const
115 os << "Wrap " << type << '\n';
117 if (!placement.empty())
118 os << "placement " << placement << "\n";
120 os << "width \"" << width.asString() << "\"\n";
124 void InsetWrapParams::read(LyXLex & lex)
128 if (token == "placement")
131 // take countermeasures
132 lex.pushToken(token);
137 if (token == "width") {
139 width = LyXLength(lex.getString());
141 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
143 // take countermeasures
144 lex.pushToken(token);
149 void InsetWrap::write(Buffer const & buf, ostream & os) const
152 InsetCollapsable::write(buf, os);
156 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
159 InsetCollapsable::read(buf, lex);
163 void InsetWrap::validate(LaTeXFeatures & features) const
165 features.require("floatflt");
166 InsetCollapsable::validate(features);
170 auto_ptr<InsetBase> InsetWrap::doClone() const
172 return auto_ptr<InsetBase>(new InsetWrap(*this));
176 docstring const InsetWrap::editMessage() const
178 return _("Opened Wrap Inset");
182 int InsetWrap::latex(Buffer const & buf, odocstream & os,
183 OutputParams const & runparams) const
185 os << "\\begin{floating" << from_ascii(params_.type) << '}';
186 if (!params_.placement.empty())
187 os << '[' << from_ascii(params_.placement) << ']';
188 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
189 int const i = InsetText::latex(buf, os, runparams);
190 os << "\\end{floating" << from_ascii(params_.type) << "}%\n";
195 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
196 OutputParams const & runparams) const
199 os << '<' << from_ascii(params_.type) << '>';
200 int const i = InsetText::docbook(buf, os, runparams);
201 os << "</" << from_ascii(params_.type) << '>';
206 bool InsetWrap::insetAllowed(InsetBase::Code code) const
214 return InsetCollapsable::insetAllowed(code);
219 bool InsetWrap::showInsetDialog(BufferView * bv) const
221 if (!InsetText::showInsetDialog(bv))
222 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
227 void InsetWrap::addToToc(TocList & toclist, Buffer const & buf) const
229 ParConstIterator pit = par_const_iterator_begin(*this);
230 ParConstIterator end = par_const_iterator_end(*this);
232 // Find a caption layout in one of the (child inset's) pars
233 for (; pit != end; ++pit) {
234 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
235 string const type = params_.type;
236 docstring const str =
237 convert<docstring>(toclist[type].size() + 1)
238 + ". " + pit->asString(buf, false);
239 TocItem const item(pit, 0, str);
240 toclist[type].push_back(item);
246 string const InsetWrapMailer::name_("wrap");
248 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
253 string const InsetWrapMailer::inset2string(Buffer const &) const
255 return params2string(inset_.params());
259 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
261 params = InsetWrapParams();
265 istringstream data(in);
271 if (!lex || name != name_)
272 return print_mailer_error("InsetWrapMailer", in, 1, name_);
274 // This is part of the inset proper that is usually swallowed
275 // by LyXText::readInset
278 if (!lex || id != "Wrap")
279 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
281 // We have to read the type here!
287 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
290 data << name_ << ' ';