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 "pariterator.h"
33 #include "support/convert.h"
40 using std::istringstream;
42 using std::ostringstream;
47 string floatname(string const & type, BufferParams const & bp)
49 FloatList const & floats = bp.getLyXTextClass().floats();
50 FloatList::const_iterator it = floats[type];
51 return (it == floats.end()) ? type : _(it->second.name());
57 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
58 : InsetCollapsable(bp)
60 setLabel(_("wrap: ") + floatname(type, bp));
61 LyXFont font(LyXFont::ALL_SANE);
64 font.setColor(LColor::collapsable);
67 params_.width = LyXLength(50, LyXLength::PCW);
72 InsetWrap::~InsetWrap()
74 InsetWrapMailer(*this).hideDialog();
78 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
81 case LFUN_INSET_MODIFY: {
82 InsetWrapParams params;
83 InsetWrapMailer::string2params(cmd.argument, params);
84 params_.placement = params.placement;
85 params_.width = params.width;
90 case LFUN_INSET_DIALOG_UPDATE:
91 InsetWrapMailer(*this).updateDialog(&cur.bv());
94 case LFUN_MOUSE_RELEASE: {
95 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
96 InsetWrapMailer(*this).showDialog(&cur.bv());
99 InsetCollapsable::doDispatch(cur, cmd);
104 InsetCollapsable::doDispatch(cur, cmd);
110 bool InsetWrap::getStatus(LCursor & cur, FuncRequest const & cmd,
111 FuncStatus & flag) const
113 switch (cmd.action) {
114 case LFUN_INSET_MODIFY:
115 case LFUN_INSET_DIALOG_UPDATE:
120 return InsetCollapsable::getStatus(cur, cmd, flag);
125 void InsetWrapParams::write(ostream & os) const
127 os << "Wrap " << type << '\n';
129 if (!placement.empty())
130 os << "placement " << placement << "\n";
132 os << "width \"" << width.asString() << "\"\n";
136 void InsetWrapParams::read(LyXLex & lex)
140 if (token == "placement")
143 // take countermeasures
144 lex.pushToken(token);
149 if (token == "width") {
151 width = LyXLength(lex.getString());
153 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
155 // take countermeasures
156 lex.pushToken(token);
161 void InsetWrap::write(Buffer const & buf, ostream & os) const
164 InsetCollapsable::write(buf, os);
168 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
171 InsetCollapsable::read(buf, lex);
175 void InsetWrap::validate(LaTeXFeatures & features) const
177 features.require("floatflt");
178 InsetCollapsable::validate(features);
182 auto_ptr<InsetBase> InsetWrap::doClone() const
184 return auto_ptr<InsetBase>(new InsetWrap(*this));
188 string const InsetWrap::editMessage() const
190 return _("Opened Wrap Inset");
194 int InsetWrap::latex(Buffer const & buf, ostream & os,
195 OutputParams const & runparams) const
197 os << "\\begin{floating" << params_.type << '}';
198 if (!params_.placement.empty())
199 os << '[' << params_.placement << ']';
200 os << '{' << params_.width.asLatexString() << "}%\n";
201 int const i = InsetText::latex(buf, os, runparams);
202 os << "\\end{floating" << params_.type << "}%\n";
207 int InsetWrap::docbook(Buffer const & buf, ostream & os,
208 OutputParams const & runparams) const
210 os << '<' << params_.type << '>';
211 int const i = InsetText::docbook(buf, os, runparams);
212 os << "</" << params_.type << '>';
217 bool InsetWrap::insetAllowed(InsetBase::Code code) const
225 return InsetCollapsable::insetAllowed(code);
230 bool InsetWrap::showInsetDialog(BufferView * bv) const
232 if (!InsetText::showInsetDialog(bv))
233 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
238 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
240 ParConstIterator pit = par_const_iterator_begin(*this);
241 ParConstIterator end = par_const_iterator_end(*this);
243 // Find a caption layout in one of the (child inset's) pars
244 for (; pit != end; ++pit) {
245 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
246 string const name = floatname(params_.type, buf.params());
248 convert<string>(toclist[name].size() + 1)
249 + ". " + pit->asString(buf, false);
250 lyx::toc::TocItem const item(pit->id(), 0 , str);
251 toclist[name].push_back(item);
257 string const InsetWrapMailer::name_("wrap");
259 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
264 string const InsetWrapMailer::inset2string(Buffer const &) const
266 return params2string(inset_.params());
270 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
272 params = InsetWrapParams();
276 istringstream data(in);
282 if (!lex || name != name_)
283 return print_mailer_error("InsetWrapMailer", in, 1, name_);
285 // This is part of the inset proper that is usually swallowed
286 // by LyXText::readInset
289 if (!lex || id != "Wrap")
290 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
292 // We have to read the type here!
298 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
301 data << name_ << ' ';