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"
25 #include "LaTeXFeatures.h"
28 #include "outputparams.h"
29 #include "paragraph.h"
30 #include "pariterator.h"
32 #include "support/convert.h"
39 using std::istringstream;
41 using std::ostringstream;
46 string floatname(string const & type, BufferParams const & bp)
48 FloatList const & floats = bp.getLyXTextClass().floats();
49 FloatList::const_iterator it = floats[type];
50 return (it == floats.end()) ? type : _(it->second.name());
56 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
57 : InsetCollapsable(bp)
59 setLabel(_("wrap: ") + floatname(type, bp));
60 LyXFont font(LyXFont::ALL_SANE);
63 font.setColor(LColor::collapsable);
66 params_.width = LyXLength(50, LyXLength::PCW);
71 InsetWrap::~InsetWrap()
73 InsetWrapMailer(*this).hideDialog();
77 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
80 case LFUN_INSET_MODIFY: {
81 InsetWrapParams params;
82 InsetWrapMailer::string2params(cmd.argument, params);
83 params_.placement = params.placement;
84 params_.width = params.width;
89 case LFUN_INSET_DIALOG_UPDATE:
90 InsetWrapMailer(*this).updateDialog(&cur.bv());
93 case LFUN_MOUSE_RELEASE: {
94 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
95 InsetWrapMailer(*this).showDialog(&cur.bv());
98 InsetCollapsable::doDispatch(cur, cmd);
103 InsetCollapsable::doDispatch(cur, cmd);
109 void InsetWrapParams::write(ostream & os) const
111 os << "Wrap " << type << '\n';
113 if (!placement.empty())
114 os << "placement " << placement << "\n";
116 os << "width \"" << width.asString() << "\"\n";
120 void InsetWrapParams::read(LyXLex & lex)
124 if (token == "placement")
127 // take countermeasures
128 lex.pushToken(token);
133 if (token == "width") {
135 width = LyXLength(lex.getString());
137 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
139 // take countermeasures
140 lex.pushToken(token);
145 void InsetWrap::write(Buffer const & buf, ostream & os) const
148 InsetCollapsable::write(buf, os);
152 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
155 InsetCollapsable::read(buf, lex);
159 void InsetWrap::validate(LaTeXFeatures & features) const
161 features.require("floatflt");
162 InsetCollapsable::validate(features);
166 auto_ptr<InsetBase> InsetWrap::doClone() const
168 return auto_ptr<InsetBase>(new InsetWrap(*this));
172 string const InsetWrap::editMessage() const
174 return _("Opened Wrap Inset");
178 int InsetWrap::latex(Buffer const & buf, ostream & os,
179 OutputParams const & runparams) const
181 os << "\\begin{floating" << params_.type << '}';
182 if (!params_.placement.empty())
183 os << '[' << params_.placement << ']';
184 os << '{' << params_.width.asLatexString() << "}%\n";
185 int const i = InsetText::latex(buf, os, runparams);
186 os << "\\end{floating" << params_.type << "}%\n";
191 int InsetWrap::docbook(Buffer const & buf, ostream & os,
192 OutputParams const & runparams) const
194 os << '<' << params_.type << '>';
195 int const i = InsetText::docbook(buf, os, runparams);
196 os << "</" << params_.type << '>';
201 bool InsetWrap::insetAllowed(InsetBase::Code code) const
209 return InsetCollapsable::insetAllowed(code);
214 bool InsetWrap::showInsetDialog(BufferView * bv) const
216 if (!InsetText::showInsetDialog(bv))
217 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
222 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
224 ParConstIterator pit = par_const_iterator_begin(*this);
225 ParConstIterator end = par_const_iterator_end(*this);
227 // Find a caption layout in one of the (child inset's) pars
228 for (; pit != end; ++pit) {
229 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
230 string const name = floatname(params_.type, buf.params());
232 convert<string>(toclist[name].size() + 1)
233 + ". " + pit->asString(buf, false);
234 lyx::toc::TocItem const item(pit->id(), 0 , str);
235 toclist[name].push_back(item);
241 string const InsetWrapMailer::name_("wrap");
243 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
248 string const InsetWrapMailer::inset2string(Buffer const &) const
250 return params2string(inset_.params());
254 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
256 params = InsetWrapParams();
260 istringstream data(in);
266 if (!lex || name != name_)
267 return print_mailer_error("InsetWrapMailer", in, 1, name_);
269 // This is part of the inset proper that is usually swallowed
270 // by LyXText::readInset
273 if (!lex || id != "Wrap")
274 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
276 // We have to read the type here!
282 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
285 data << name_ << ' ';