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"
42 using std::istringstream;
44 using std::ostringstream;
49 string floatname(string const & type, BufferParams const & bp)
51 FloatList const & floats = bp.getLyXTextClass().floats();
52 FloatList::const_iterator it = floats[type];
54 return (it == floats.end()) ? type : lyx::to_utf8(_(it->second.name()));
60 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
61 : InsetCollapsable(bp)
64 setLabel(lyx::to_utf8(_("wrap: ")) + floatname(type, bp));
65 LyXFont font(LyXFont::ALL_SANE);
68 font.setColor(LColor::collapsable);
71 params_.width = LyXLength(50, LyXLength::PCW);
76 InsetWrap::~InsetWrap()
78 InsetWrapMailer(*this).hideDialog();
82 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
85 case LFUN_INSET_MODIFY: {
86 InsetWrapParams params;
87 InsetWrapMailer::string2params(lyx::to_utf8(cmd.argument()), params);
88 params_.placement = params.placement;
89 params_.width = params.width;
93 case LFUN_INSET_DIALOG_UPDATE:
94 InsetWrapMailer(*this).updateDialog(&cur.bv());
97 case LFUN_MOUSE_RELEASE: {
98 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
99 InsetWrapMailer(*this).showDialog(&cur.bv());
102 InsetCollapsable::doDispatch(cur, cmd);
107 InsetCollapsable::doDispatch(cur, cmd);
113 bool InsetWrap::getStatus(LCursor & cur, FuncRequest const & cmd,
114 FuncStatus & flag) const
116 switch (cmd.action) {
117 case LFUN_INSET_MODIFY:
118 case LFUN_INSET_DIALOG_UPDATE:
123 return InsetCollapsable::getStatus(cur, cmd, flag);
128 void InsetWrapParams::write(ostream & os) const
130 os << "Wrap " << type << '\n';
132 if (!placement.empty())
133 os << "placement " << placement << "\n";
135 os << "width \"" << width.asString() << "\"\n";
139 void InsetWrapParams::read(LyXLex & lex)
143 if (token == "placement")
146 // take countermeasures
147 lex.pushToken(token);
152 if (token == "width") {
154 width = LyXLength(lex.getString());
156 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
158 // take countermeasures
159 lex.pushToken(token);
164 void InsetWrap::write(Buffer const & buf, ostream & os) const
167 InsetCollapsable::write(buf, os);
171 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
174 InsetCollapsable::read(buf, lex);
178 void InsetWrap::validate(LaTeXFeatures & features) const
180 features.require("floatflt");
181 InsetCollapsable::validate(features);
185 auto_ptr<InsetBase> InsetWrap::doClone() const
187 return auto_ptr<InsetBase>(new InsetWrap(*this));
191 docstring const InsetWrap::editMessage() const
193 return _("Opened Wrap Inset");
197 int InsetWrap::latex(Buffer const & buf, ostream & os,
198 OutputParams const & runparams) const
200 os << "\\begin{floating" << params_.type << '}';
201 if (!params_.placement.empty())
202 os << '[' << params_.placement << ']';
203 os << '{' << params_.width.asLatexString() << "}%\n";
204 int const i = InsetText::latex(buf, os, runparams);
205 os << "\\end{floating" << params_.type << "}%\n";
210 int InsetWrap::docbook(Buffer const & buf, ostream & os,
211 OutputParams const & runparams) const
213 os << '<' << params_.type << '>';
214 int const i = InsetText::docbook(buf, os, runparams);
215 os << "</" << params_.type << '>';
220 bool InsetWrap::insetAllowed(InsetBase::Code code) const
228 return InsetCollapsable::insetAllowed(code);
233 bool InsetWrap::showInsetDialog(BufferView * bv) const
235 if (!InsetText::showInsetDialog(bv))
236 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
241 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
243 ParConstIterator pit = par_const_iterator_begin(*this);
244 ParConstIterator end = par_const_iterator_end(*this);
246 // Find a caption layout in one of the (child inset's) pars
247 for (; pit != end; ++pit) {
248 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
249 string const type = params_.type;
251 convert<string>(toclist[type].size() + 1)
252 + ". " + pit->asString(buf, false);
253 lyx::toc::TocItem const item(pit, 0 , str);
254 toclist[type].push_back(item);
260 string const InsetWrapMailer::name_("wrap");
262 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
267 string const InsetWrapMailer::inset2string(Buffer const &) const
269 return params2string(inset_.params());
273 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
275 params = InsetWrapParams();
279 istringstream data(in);
285 if (!lex || name != name_)
286 return print_mailer_error("InsetWrapMailer", in, 1, name_);
288 // This is part of the inset proper that is usually swallowed
289 // by LyXText::readInset
292 if (!lex || id != "Wrap")
293 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
295 // We have to read the type here!
301 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
304 data << name_ << ' ';