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];
52 return (it == floats.end()) ? type : lyx::to_utf8(_(it->second.name()));
58 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
59 : InsetCollapsable(bp)
62 setLabel(lyx::to_utf8(_("wrap: ")) + floatname(type, bp));
63 LyXFont font(LyXFont::ALL_SANE);
66 font.setColor(LColor::collapsable);
69 params_.width = LyXLength(50, LyXLength::PCW);
74 InsetWrap::~InsetWrap()
76 InsetWrapMailer(*this).hideDialog();
80 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
83 case LFUN_INSET_MODIFY: {
84 InsetWrapParams params;
85 InsetWrapMailer::string2params(lyx::to_utf8(cmd.argument()), params);
86 params_.placement = params.placement;
87 params_.width = params.width;
91 case LFUN_INSET_DIALOG_UPDATE:
92 InsetWrapMailer(*this).updateDialog(&cur.bv());
95 case LFUN_MOUSE_RELEASE: {
96 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
97 InsetWrapMailer(*this).showDialog(&cur.bv());
100 InsetCollapsable::doDispatch(cur, cmd);
105 InsetCollapsable::doDispatch(cur, cmd);
111 bool InsetWrap::getStatus(LCursor & cur, FuncRequest const & cmd,
112 FuncStatus & flag) const
114 switch (cmd.action) {
115 case LFUN_INSET_MODIFY:
116 case LFUN_INSET_DIALOG_UPDATE:
121 return InsetCollapsable::getStatus(cur, cmd, flag);
126 void InsetWrapParams::write(ostream & os) const
128 os << "Wrap " << type << '\n';
130 if (!placement.empty())
131 os << "placement " << placement << "\n";
133 os << "width \"" << width.asString() << "\"\n";
137 void InsetWrapParams::read(LyXLex & lex)
141 if (token == "placement")
144 // take countermeasures
145 lex.pushToken(token);
150 if (token == "width") {
152 width = LyXLength(lex.getString());
154 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
156 // take countermeasures
157 lex.pushToken(token);
162 void InsetWrap::write(Buffer const & buf, ostream & os) const
165 InsetCollapsable::write(buf, os);
169 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
172 InsetCollapsable::read(buf, lex);
176 void InsetWrap::validate(LaTeXFeatures & features) const
178 features.require("floatflt");
179 InsetCollapsable::validate(features);
183 auto_ptr<InsetBase> InsetWrap::doClone() const
185 return auto_ptr<InsetBase>(new InsetWrap(*this));
189 string const InsetWrap::editMessage() const
192 return lyx::to_utf8(_("Opened Wrap Inset"));
196 int InsetWrap::latex(Buffer const & buf, ostream & os,
197 OutputParams const & runparams) const
199 os << "\\begin{floating" << params_.type << '}';
200 if (!params_.placement.empty())
201 os << '[' << params_.placement << ']';
202 os << '{' << params_.width.asLatexString() << "}%\n";
203 int const i = InsetText::latex(buf, os, runparams);
204 os << "\\end{floating" << params_.type << "}%\n";
209 int InsetWrap::docbook(Buffer const & buf, ostream & os,
210 OutputParams const & runparams) const
212 os << '<' << params_.type << '>';
213 int const i = InsetText::docbook(buf, os, runparams);
214 os << "</" << params_.type << '>';
219 bool InsetWrap::insetAllowed(InsetBase::Code code) const
227 return InsetCollapsable::insetAllowed(code);
232 bool InsetWrap::showInsetDialog(BufferView * bv) const
234 if (!InsetText::showInsetDialog(bv))
235 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
240 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
242 ParConstIterator pit = par_const_iterator_begin(*this);
243 ParConstIterator end = par_const_iterator_end(*this);
245 // Find a caption layout in one of the (child inset's) pars
246 for (; pit != end; ++pit) {
247 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
248 string const type = params_.type;
250 convert<string>(toclist[type].size() + 1)
251 + ". " + pit->asString(buf, false);
252 lyx::toc::TocItem const item(pit, 0 , str);
253 toclist[type].push_back(item);
259 string const InsetWrapMailer::name_("wrap");
261 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
266 string const InsetWrapMailer::inset2string(Buffer const &) const
268 return params2string(inset_.params());
272 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
274 params = InsetWrapParams();
278 istringstream data(in);
284 if (!lex || name != name_)
285 return print_mailer_error("InsetWrapMailer", in, 1, name_);
287 // This is part of the inset proper that is usually swallowed
288 // by LyXText::readInset
291 if (!lex || id != "Wrap")
292 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
294 // We have to read the type here!
300 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
303 data << name_ << ' ';