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"
19 #include "dispatchresult.h"
21 #include "FloatList.h"
22 #include "funcrequest.h"
24 #include "LaTeXFeatures.h"
27 #include "outputparams.h"
28 #include "paragraph.h"
30 #include "support/std_sstream.h"
31 #include "support/tostr.h"
37 using std::istringstream;
39 using std::ostringstream;
44 // this should not be hardcoded, but be part of the definition
45 // of the float (JMarc)
46 string const caplayout("Caption");
48 string floatname(string const & type, BufferParams const & bp)
50 FloatList const & floats = bp.getLyXTextClass().floats();
51 FloatList::const_iterator it = floats[type];
52 return (it == floats.end()) ? type : _(it->second.name());
58 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
59 : InsetCollapsable(bp)
61 setLabel(_("wrap: ") + floatname(type, bp));
62 LyXFont font(LyXFont::ALL_SANE);
65 font.setColor(LColor::collapsable);
68 params_.width = LyXLength(50, LyXLength::PCW);
70 LyXTextClass const & tclass = bp.getLyXTextClass();
71 if (tclass.hasLayout(caplayout))
72 inset.paragraphs().begin()->layout(tclass[caplayout]);
76 InsetWrap::~InsetWrap()
78 InsetWrapMailer(*this).hideDialog();
83 InsetWrap::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
86 case LFUN_INSET_MODIFY: {
87 InsetWrapParams params;
88 InsetWrapMailer::string2params(cmd.argument, params);
90 params_.placement = params.placement;
91 params_.width = params.width;
94 return DispatchResult(true, true);
97 case LFUN_INSET_DIALOG_UPDATE:
98 InsetWrapMailer(*this).updateDialog(&bv);
99 return DispatchResult(true, true);
102 return InsetCollapsable::priv_dispatch(bv, cmd);
107 void InsetWrapParams::write(ostream & os) const
109 os << "Wrap " << type << '\n';
111 if (!placement.empty())
112 os << "placement " << placement << "\n";
114 os << "width \"" << width.asString() << "\"\n";
118 void InsetWrapParams::read(LyXLex & lex)
122 string token = lex.getString();
123 if (token == "placement") {
125 placement = lex.getString();
127 // take countermeasures
128 lex.pushToken(token);
133 string token = lex.getString();
134 if (token == "width") {
136 width = LyXLength(lex.getString());
138 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
140 // take countermeasures
141 lex.pushToken(token);
147 void InsetWrap::write(Buffer const & buf, ostream & os) const
150 InsetCollapsable::write(buf, os);
154 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
157 InsetCollapsable::read(buf, lex);
161 void InsetWrap::validate(LaTeXFeatures & features) const
163 features.require("floatflt");
164 InsetCollapsable::validate(features);
168 auto_ptr<InsetBase> InsetWrap::clone() const
170 return auto_ptr<InsetBase>(new InsetWrap(*this));
174 string const InsetWrap::editMessage() const
176 return _("Opened Wrap Inset");
180 int InsetWrap::latex(Buffer const & buf, ostream & os,
181 OutputParams const & runparams) const
183 os << "\\begin{floating" << params_.type << '}';
184 if (!params_.placement.empty()) {
185 os << '[' << params_.placement << ']';
187 os << '{' << params_.width.asLatexString() << "}%\n";
189 int const i = inset.latex(buf, os, runparams);
191 os << "\\end{floating" << params_.type << "}%\n";
196 int InsetWrap::docbook(Buffer const & buf, ostream & os,
197 OutputParams const & runparams) const
199 os << '<' << params_.type << '>';
200 int const i = inset.docbook(buf, os, runparams);
201 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 (!inset.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::iterator tmp = inset.paragraphs().begin();
232 ParagraphList::iterator end = inset.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");
286 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
289 data << name_ << ' ';