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 "BufferView.h"
19 #include "FloatList.h"
20 #include "funcrequest.h"
22 #include "LaTeXFeatures.h"
25 #include "support/tostr.h"
27 #include "support/std_sstream.h"
36 // this should not be hardcoded, but be part of the definition
37 // of the float (JMarc)
38 string const caplayout("Caption");
40 string floatname(string const & type, BufferParams const & bp)
42 FloatList const & floats = bp.getLyXTextClass().floats();
43 FloatList::const_iterator it = floats[type];
44 if (it == floats.end())
47 return _(it->second.name());
53 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
54 : InsetCollapsable(bp)
56 string lab(_("wrap: "));
57 lab += floatname(type, bp);
59 LyXFont font(LyXFont::ALL_SANE);
62 font.setColor(LColor::collapsable);
65 params_.width = LyXLength(50, LyXLength::PCW);
67 LyXTextClass const & tclass = bp.getLyXTextClass();
68 if (tclass.hasLayout(caplayout))
69 inset.paragraphs.begin()->layout(tclass[caplayout]);
73 InsetWrap::~InsetWrap()
75 InsetWrapMailer(*this).hideDialog();
79 dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
82 case LFUN_INSET_MODIFY: {
83 InsetWrapParams params;
84 InsetWrapMailer::string2params(cmd.argument, params);
86 params_.placement = params.placement;
87 params_.width = params.width;
89 cmd.view()->updateInset(this);
93 case LFUN_INSET_DIALOG_UPDATE:
94 InsetWrapMailer(*this).updateDialog(cmd.view());
98 return InsetCollapsable::localDispatch(cmd);
103 void InsetWrapParams::write(ostream & os) const
105 os << "Wrap " << type << '\n';
107 if (!placement.empty())
108 os << "placement " << placement << "\n";
110 os << "width \"" << width.asString() << "\"\n";
114 void InsetWrapParams::read(LyXLex & lex)
118 string token = lex.getString();
119 if (token == "placement") {
121 placement = lex.getString();
123 // take countermeasures
124 lex.pushToken(token);
129 string token = lex.getString();
130 if (token == "width") {
132 width = LyXLength(lex.getString());
134 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
136 // take countermeasures
137 lex.pushToken(token);
143 void InsetWrap::write(Buffer const & buf, ostream & os) const
146 InsetCollapsable::write(buf, os);
150 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
153 InsetCollapsable::read(buf, lex);
157 void InsetWrap::validate(LaTeXFeatures & features) const
159 features.require("floatflt");
160 InsetCollapsable::validate(features);
164 auto_ptr<InsetBase> InsetWrap::clone() const
166 return auto_ptr<InsetBase>(new InsetWrap(*this));
170 string const InsetWrap::editMessage() const
172 return _("Opened Wrap Inset");
176 int InsetWrap::latex(Buffer const & buf, ostream & os,
177 LatexRunParams const & runparams) const
179 os << "\\begin{floating" << params_.type << '}';
180 if (!params_.placement.empty()) {
181 os << '[' << params_.placement << ']';
183 os << '{' << params_.width.asLatexString() << "}%\n";
185 int const i = inset.latex(buf, os, runparams);
187 os << "\\end{floating" << params_.type << "}%\n";
192 int InsetWrap::docbook(Buffer const & buf, ostream & os, bool mixcont) const
194 os << '<' << params_.type << '>';
195 int const i = inset.docbook(buf, os, mixcont);
196 os << "</" << params_.type << '>';
202 bool InsetWrap::insetAllowed(InsetOld::Code code) const
210 return InsetCollapsable::insetAllowed(code);
215 int InsetWrap::latexTextWidth(BufferView * bv) const
217 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
221 bool InsetWrap::showInsetDialog(BufferView * bv) const
223 if (!inset.showInsetDialog(bv)) {
224 InsetWrap * tmp = const_cast<InsetWrap *>(this);
225 InsetWrapMailer(*tmp).showDialog(bv);
231 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
233 // Now find the caption in the float...
234 ParagraphList::iterator tmp = inset.paragraphs.begin();
235 ParagraphList::iterator end = inset.paragraphs.end();
237 for (; tmp != end; ++tmp) {
238 if (tmp->layout()->name() == caplayout) {
239 string const name = floatname(params_.type, buf.params);
241 tostr(toclist[name].size() + 1)
242 + ". " + tmp->asString(buf, false);
243 lyx::toc::TocItem const item(tmp->id(), 0 , str);
244 toclist[name].push_back(item);
250 string const InsetWrapMailer::name_("wrap");
252 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
257 string const InsetWrapMailer::inset2string(Buffer const &) const
259 return params2string(inset_.params());
263 void InsetWrapMailer::string2params(string const & in,
264 InsetWrapParams & params)
266 params = InsetWrapParams();
271 istringstream data(STRCONV(in));
277 string const token = lex.getString();
282 // This is part of the inset proper that is usually swallowed
283 // by Buffer::readInset
286 string const token = lex.getString();
287 if (token != "Wrap" || !lex.eatLine())
297 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
300 data << name_ << ' ';
302 return STRCONV(data.str());