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 "paragraph.h"
29 #include "support/tostr.h"
31 #include "support/std_sstream.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 if (it == floats.end())
55 return _(it->second.name());
61 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
62 : InsetCollapsable(bp)
64 string lab(_("wrap: "));
65 lab += floatname(type, bp);
67 LyXFont font(LyXFont::ALL_SANE);
70 font.setColor(LColor::collapsable);
73 params_.width = LyXLength(50, LyXLength::PCW);
75 LyXTextClass const & tclass = bp.getLyXTextClass();
76 if (tclass.hasLayout(caplayout))
77 inset.paragraphs.begin()->layout(tclass[caplayout]);
81 InsetWrap::~InsetWrap()
83 InsetWrapMailer(*this).hideDialog();
88 InsetWrap::priv_dispatch(FuncRequest const & cmd,
89 idx_type & idx, pos_type & pos)
92 case LFUN_INSET_MODIFY: {
93 InsetWrapParams params;
94 InsetWrapMailer::string2params(cmd.argument, params);
96 params_.placement = params.placement;
97 params_.width = params.width;
99 cmd.view()->updateInset(this);
100 return DispatchResult(DISPATCHED);
103 case LFUN_INSET_DIALOG_UPDATE:
104 InsetWrapMailer(*this).updateDialog(cmd.view());
105 return DispatchResult(DISPATCHED);
108 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
113 void InsetWrapParams::write(ostream & os) const
115 os << "Wrap " << type << '\n';
117 if (!placement.empty())
118 os << "placement " << placement << "\n";
120 os << "width \"" << width.asString() << "\"\n";
124 void InsetWrapParams::read(LyXLex & lex)
128 string token = lex.getString();
129 if (token == "placement") {
131 placement = lex.getString();
133 // take countermeasures
134 lex.pushToken(token);
139 string token = lex.getString();
140 if (token == "width") {
142 width = LyXLength(lex.getString());
144 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
146 // take countermeasures
147 lex.pushToken(token);
153 void InsetWrap::write(Buffer const & buf, ostream & os) const
156 InsetCollapsable::write(buf, os);
160 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
163 InsetCollapsable::read(buf, lex);
167 void InsetWrap::validate(LaTeXFeatures & features) const
169 features.require("floatflt");
170 InsetCollapsable::validate(features);
174 auto_ptr<InsetBase> InsetWrap::clone() const
176 return auto_ptr<InsetBase>(new InsetWrap(*this));
180 string const InsetWrap::editMessage() const
182 return _("Opened Wrap Inset");
186 int InsetWrap::latex(Buffer const & buf, ostream & os,
187 LatexRunParams const & runparams) const
189 os << "\\begin{floating" << params_.type << '}';
190 if (!params_.placement.empty()) {
191 os << '[' << params_.placement << ']';
193 os << '{' << params_.width.asLatexString() << "}%\n";
195 int const i = inset.latex(buf, os, runparams);
197 os << "\\end{floating" << params_.type << "}%\n";
202 int InsetWrap::docbook(Buffer const & buf, ostream & os, bool mixcont) const
204 os << '<' << params_.type << '>';
205 int const i = inset.docbook(buf, os, mixcont);
206 os << "</" << params_.type << '>';
212 bool InsetWrap::insetAllowed(InsetOld::Code code) const
220 return InsetCollapsable::insetAllowed(code);
225 int InsetWrap::latexTextWidth(BufferView * bv) const
227 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
231 bool InsetWrap::showInsetDialog(BufferView * bv) const
233 if (!inset.showInsetDialog(bv)) {
234 InsetWrap * tmp = const_cast<InsetWrap *>(this);
235 InsetWrapMailer(*tmp).showDialog(bv);
241 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
243 // Now find the caption in the float...
244 ParagraphList::iterator tmp = inset.paragraphs.begin();
245 ParagraphList::iterator end = inset.paragraphs.end();
247 for (; tmp != end; ++tmp) {
248 if (tmp->layout()->name() == caplayout) {
249 string const name = floatname(params_.type, buf.params());
251 tostr(toclist[name].size() + 1)
252 + ". " + tmp->asString(buf, false);
253 lyx::toc::TocItem const item(tmp->id(), 0 , str);
254 toclist[name].push_back(item);
260 string const InsetWrapMailer::name_("wrap");
262 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
267 string const InsetWrapMailer::inset2string(Buffer const &) const
269 return params2string(inset_.params());
273 void InsetWrapMailer::string2params(string const & in,
274 InsetWrapParams & params)
276 params = InsetWrapParams();
281 istringstream data(in);
287 string const token = lex.getString();
292 // This is part of the inset proper that is usually swallowed
293 // by Buffer::readInset
296 string const token = lex.getString();
297 if (token != "Wrap" || !lex.eatLine())
307 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
310 data << name_ << ' ';