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/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 paragraphs().begin()->layout(tclass[caplayout]);
77 InsetWrap::~InsetWrap()
79 InsetWrapMailer(*this).hideDialog();
83 void InsetWrap::priv_dispatch(LCursor & cur, FuncRequest & cmd)
86 case LFUN_INSET_MODIFY: {
87 InsetWrapParams params;
88 InsetWrapMailer::string2params(cmd.argument, params);
89 params_.placement = params.placement;
90 params_.width = params.width;
95 case LFUN_INSET_DIALOG_UPDATE:
96 InsetWrapMailer(*this).updateDialog(&cur.bv());
99 case LFUN_MOUSE_RELEASE: {
100 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
101 InsetWrapMailer(*this).showDialog(&cur.bv());
104 InsetCollapsable::priv_dispatch(cur, cmd);
109 InsetCollapsable::priv_dispatch(cur, cmd);
115 void InsetWrapParams::write(ostream & os) const
117 os << "Wrap " << type << '\n';
119 if (!placement.empty())
120 os << "placement " << placement << "\n";
122 os << "width \"" << width.asString() << "\"\n";
126 void InsetWrapParams::read(LyXLex & lex)
130 if (token == "placement")
133 // take countermeasures
134 lex.pushToken(token);
139 if (token == "width") {
141 width = LyXLength(lex.getString());
143 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
145 // take countermeasures
146 lex.pushToken(token);
151 void InsetWrap::write(Buffer const & buf, ostream & os) const
154 InsetCollapsable::write(buf, os);
158 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
161 InsetCollapsable::read(buf, lex);
165 void InsetWrap::validate(LaTeXFeatures & features) const
167 features.require("floatflt");
168 InsetCollapsable::validate(features);
172 auto_ptr<InsetBase> InsetWrap::clone() const
174 return auto_ptr<InsetBase>(new InsetWrap(*this));
178 string const InsetWrap::editMessage() const
180 return _("Opened Wrap Inset");
184 int InsetWrap::latex(Buffer const & buf, ostream & os,
185 OutputParams const & runparams) const
187 os << "\\begin{floating" << params_.type << '}';
188 if (!params_.placement.empty())
189 os << '[' << params_.placement << ']';
190 os << '{' << params_.width.asLatexString() << "}%\n";
191 int const i = InsetText::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 = InsetText::docbook(buf, os, runparams);
202 os << "</" << params_.type << '>';
207 bool InsetWrap::insetAllowed(InsetOld::Code code) const
215 return InsetCollapsable::insetAllowed(code);
220 bool InsetWrap::showInsetDialog(BufferView * bv) const
222 if (!InsetText::showInsetDialog(bv))
223 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
228 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
230 // Now find the caption in the float...
231 ParagraphList::const_iterator tmp = paragraphs().begin();
232 ParagraphList::const_iterator end = paragraphs().end();
234 for (; tmp != end; ++tmp) {
235 if (tmp->layout()->name() == caplayout) {
236 string const name = floatname(params_.type, buf.params());
238 tostr(toclist[name].size() + 1)
239 + ". " + tmp->asString(buf, false);
240 lyx::toc::TocItem const item(tmp->id(), 0 , str);
241 toclist[name].push_back(item);
247 string const InsetWrapMailer::name_("wrap");
249 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
254 string const InsetWrapMailer::inset2string(Buffer const &) const
256 return params2string(inset_.params());
260 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
262 params = InsetWrapParams();
266 istringstream data(in);
272 if (!lex || name != name_)
273 return print_mailer_error("InsetWrapMailer", in, 1, name_);
275 // This is part of the inset proper that is usually swallowed
276 // by LyXText::readInset
279 if (!lex || id != "Wrap")
280 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
282 // We have to read the type here!
288 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
291 data << name_ << ' ';