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"
31 #include "support/std_sstream.h"
32 #include "support/tostr.h"
38 using std::istringstream;
40 using std::ostringstream;
45 // this should not be hardcoded, but be part of the definition
46 // of the float (JMarc)
47 string const caplayout("Caption");
49 string floatname(string const & type, BufferParams const & bp)
51 FloatList const & floats = bp.getLyXTextClass().floats();
52 FloatList::const_iterator it = floats[type];
53 return (it == floats.end()) ? type : _(it->second.name());
59 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
60 : InsetCollapsable(bp)
62 setLabel(_("wrap: ") + floatname(type, bp));
63 LyXFont font(LyXFont::ALL_SANE);
66 font.setColor(LColor::collapsable);
69 params_.width = LyXLength(50, LyXLength::PCW);
71 LyXTextClass const & tclass = bp.getLyXTextClass();
72 if (tclass.hasLayout(caplayout))
73 inset.paragraphs().begin()->layout(tclass[caplayout]);
77 InsetWrap::~InsetWrap()
79 InsetWrapMailer(*this).hideDialog();
84 InsetWrap::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
87 case LFUN_INSET_MODIFY: {
88 InsetWrapParams params;
89 InsetWrapMailer::string2params(cmd.argument, params);
91 params_.placement = params.placement;
92 params_.width = params.width;
95 return DispatchResult(true, true);
98 case LFUN_INSET_DIALOG_UPDATE:
99 InsetWrapMailer(*this).updateDialog(&cur.bv());
100 return DispatchResult(true, true);
103 return InsetCollapsable::priv_dispatch(cur, cmd);
108 void InsetWrapParams::write(ostream & os) const
110 os << "Wrap " << type << '\n';
112 if (!placement.empty())
113 os << "placement " << placement << "\n";
115 os << "width \"" << width.asString() << "\"\n";
119 void InsetWrapParams::read(LyXLex & lex)
123 string token = lex.getString();
124 if (token == "placement") {
126 placement = lex.getString();
128 // take countermeasures
129 lex.pushToken(token);
134 string token = lex.getString();
135 if (token == "width") {
137 width = LyXLength(lex.getString());
139 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
141 // take countermeasures
142 lex.pushToken(token);
148 void InsetWrap::write(Buffer const & buf, ostream & os) const
151 InsetCollapsable::write(buf, os);
155 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
158 InsetCollapsable::read(buf, lex);
162 void InsetWrap::validate(LaTeXFeatures & features) const
164 features.require("floatflt");
165 InsetCollapsable::validate(features);
169 auto_ptr<InsetBase> InsetWrap::clone() const
171 return auto_ptr<InsetBase>(new InsetWrap(*this));
175 string const InsetWrap::editMessage() const
177 return _("Opened Wrap Inset");
181 int InsetWrap::latex(Buffer const & buf, ostream & os,
182 OutputParams const & runparams) const
184 os << "\\begin{floating" << params_.type << '}';
185 if (!params_.placement.empty()) {
186 os << '[' << params_.placement << ']';
188 os << '{' << params_.width.asLatexString() << "}%\n";
190 int const i = inset.latex(buf, os, runparams);
192 os << "\\end{floating" << params_.type << "}%\n";
197 int InsetWrap::docbook(Buffer const & buf, ostream & os,
198 OutputParams const & runparams) const
200 os << '<' << params_.type << '>';
201 int const i = inset.docbook(buf, os, runparams);
202 os << "</" << params_.type << '>';
208 bool InsetWrap::insetAllowed(InsetOld::Code code) const
216 return InsetCollapsable::insetAllowed(code);
221 bool InsetWrap::showInsetDialog(BufferView * bv) const
223 if (!inset.showInsetDialog(bv))
224 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
229 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
231 // Now find the caption in the float...
232 ParagraphList::iterator tmp = inset.paragraphs().begin();
233 ParagraphList::iterator end = inset.paragraphs().end();
235 for (; tmp != end; ++tmp) {
236 if (tmp->layout()->name() == caplayout) {
237 string const name = floatname(params_.type, buf.params());
239 tostr(toclist[name].size() + 1)
240 + ". " + tmp->asString(buf, false);
241 lyx::toc::TocItem const item(tmp->id(), 0 , str);
242 toclist[name].push_back(item);
248 string const InsetWrapMailer::name_("wrap");
250 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
255 string const InsetWrapMailer::inset2string(Buffer const &) const
257 return params2string(inset_.params());
261 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
263 params = InsetWrapParams();
267 istringstream data(in);
273 if (!lex || name != name_)
274 return print_mailer_error("InsetWrapMailer", in, 1, name_);
276 // This is part of the inset proper that is usually swallowed
277 // by LyXText::readInset
280 if (!lex || id != "Wrap")
281 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
287 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
290 data << name_ << ' ';