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/tostr.h"
32 #include "support/std_sstream.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 if (it == floats.end())
56 return _(it->second.name());
62 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
63 : InsetCollapsable(bp)
65 string lab(_("wrap: "));
66 lab += floatname(type, bp);
68 LyXFont font(LyXFont::ALL_SANE);
71 font.setColor(LColor::collapsable);
74 params_.width = LyXLength(50, LyXLength::PCW);
76 LyXTextClass const & tclass = bp.getLyXTextClass();
77 if (tclass.hasLayout(caplayout))
78 inset.paragraphs.begin()->layout(tclass[caplayout]);
82 InsetWrap::~InsetWrap()
84 InsetWrapMailer(*this).hideDialog();
89 InsetWrap::priv_dispatch(FuncRequest const & cmd,
90 idx_type & idx, pos_type & pos)
93 case LFUN_INSET_MODIFY: {
94 InsetWrapParams params;
95 InsetWrapMailer::string2params(cmd.argument, params);
97 params_.placement = params.placement;
98 params_.width = params.width;
100 cmd.view()->updateInset(this);
101 return DispatchResult(true, true);
104 case LFUN_INSET_DIALOG_UPDATE:
105 InsetWrapMailer(*this).updateDialog(cmd.view());
106 return DispatchResult(true, true);
109 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
114 void InsetWrapParams::write(ostream & os) const
116 os << "Wrap " << type << '\n';
118 if (!placement.empty())
119 os << "placement " << placement << "\n";
121 os << "width \"" << width.asString() << "\"\n";
125 void InsetWrapParams::read(LyXLex & lex)
129 string token = lex.getString();
130 if (token == "placement") {
132 placement = lex.getString();
134 // take countermeasures
135 lex.pushToken(token);
140 string token = lex.getString();
141 if (token == "width") {
143 width = LyXLength(lex.getString());
145 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
147 // take countermeasures
148 lex.pushToken(token);
154 void InsetWrap::write(Buffer const & buf, ostream & os) const
157 InsetCollapsable::write(buf, os);
161 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
164 InsetCollapsable::read(buf, lex);
168 void InsetWrap::validate(LaTeXFeatures & features) const
170 features.require("floatflt");
171 InsetCollapsable::validate(features);
175 auto_ptr<InsetBase> InsetWrap::clone() const
177 return auto_ptr<InsetBase>(new InsetWrap(*this));
181 string const InsetWrap::editMessage() const
183 return _("Opened Wrap Inset");
187 int InsetWrap::latex(Buffer const & buf, ostream & os,
188 OutputParams const & runparams) const
190 os << "\\begin{floating" << params_.type << '}';
191 if (!params_.placement.empty()) {
192 os << '[' << params_.placement << ']';
194 os << '{' << params_.width.asLatexString() << "}%\n";
196 int const i = inset.latex(buf, os, runparams);
198 os << "\\end{floating" << params_.type << "}%\n";
203 int InsetWrap::docbook(Buffer const & buf, ostream & os,
204 OutputParams const & runparams) const
206 os << '<' << params_.type << '>';
207 int const i = inset.docbook(buf, os, runparams);
208 os << "</" << params_.type << '>';
214 bool InsetWrap::insetAllowed(InsetOld::Code code) const
222 return InsetCollapsable::insetAllowed(code);
227 int InsetWrap::latexTextWidth(BufferView * bv) const
229 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
233 bool InsetWrap::showInsetDialog(BufferView * bv) const
235 if (!inset.showInsetDialog(bv)) {
236 InsetWrap * tmp = const_cast<InsetWrap *>(this);
237 InsetWrapMailer(*tmp).showDialog(bv);
243 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
245 // Now find the caption in the float...
246 ParagraphList::iterator tmp = inset.paragraphs.begin();
247 ParagraphList::iterator end = inset.paragraphs.end();
249 for (; tmp != end; ++tmp) {
250 if (tmp->layout()->name() == caplayout) {
251 string const name = floatname(params_.type, buf.params());
253 tostr(toclist[name].size() + 1)
254 + ". " + tmp->asString(buf, false);
255 lyx::toc::TocItem const item(tmp->id(), 0 , str);
256 toclist[name].push_back(item);
262 string const InsetWrapMailer::name_("wrap");
264 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
269 string const InsetWrapMailer::inset2string(Buffer const &) const
271 return params2string(inset_.params());
275 void InsetWrapMailer::string2params(string const & in,
276 InsetWrapParams & params)
278 params = InsetWrapParams();
283 istringstream data(in);
289 string const token = lex.getString();
294 // This is part of the inset proper that is usually swallowed
295 // by Buffer::readInset
298 string const token = lex.getString();
299 if (token != "Wrap" || !lex.eatLine())
309 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
312 data << name_ << ' ';