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 "FloatList.h"
21 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
26 #include "paragraph.h"
28 #include "support/tostr.h"
30 #include "support/std_sstream.h"
36 using std::istringstream;
38 using std::ostringstream;
43 // this should not be hardcoded, but be part of the definition
44 // of the float (JMarc)
45 string const caplayout("Caption");
47 string floatname(string const & type, BufferParams const & bp)
49 FloatList const & floats = bp.getLyXTextClass().floats();
50 FloatList::const_iterator it = floats[type];
51 if (it == floats.end())
54 return _(it->second.name());
60 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
61 : InsetCollapsable(bp)
63 string lab(_("wrap: "));
64 lab += floatname(type, bp);
66 LyXFont font(LyXFont::ALL_SANE);
69 font.setColor(LColor::collapsable);
72 params_.width = LyXLength(50, LyXLength::PCW);
74 LyXTextClass const & tclass = bp.getLyXTextClass();
75 if (tclass.hasLayout(caplayout))
76 inset.paragraphs.begin()->layout(tclass[caplayout]);
80 InsetWrap::~InsetWrap()
82 InsetWrapMailer(*this).hideDialog();
87 InsetWrap::priv_dispatch(FuncRequest const & cmd,
88 idx_type & idx, pos_type & pos)
91 case LFUN_INSET_MODIFY: {
92 InsetWrapParams params;
93 InsetWrapMailer::string2params(cmd.argument, params);
95 params_.placement = params.placement;
96 params_.width = params.width;
98 cmd.view()->updateInset(this);
102 case LFUN_INSET_DIALOG_UPDATE:
103 InsetWrapMailer(*this).updateDialog(cmd.view());
107 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
112 void InsetWrapParams::write(ostream & os) const
114 os << "Wrap " << type << '\n';
116 if (!placement.empty())
117 os << "placement " << placement << "\n";
119 os << "width \"" << width.asString() << "\"\n";
123 void InsetWrapParams::read(LyXLex & lex)
127 string token = lex.getString();
128 if (token == "placement") {
130 placement = lex.getString();
132 // take countermeasures
133 lex.pushToken(token);
138 string token = lex.getString();
139 if (token == "width") {
141 width = LyXLength(lex.getString());
143 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
145 // take countermeasures
146 lex.pushToken(token);
152 void InsetWrap::write(Buffer const & buf, ostream & os) const
155 InsetCollapsable::write(buf, os);
159 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
162 InsetCollapsable::read(buf, lex);
166 void InsetWrap::validate(LaTeXFeatures & features) const
168 features.require("floatflt");
169 InsetCollapsable::validate(features);
173 auto_ptr<InsetBase> InsetWrap::clone() const
175 return auto_ptr<InsetBase>(new InsetWrap(*this));
179 string const InsetWrap::editMessage() const
181 return _("Opened Wrap Inset");
185 int InsetWrap::latex(Buffer const & buf, ostream & os,
186 LatexRunParams const & runparams) const
188 os << "\\begin{floating" << params_.type << '}';
189 if (!params_.placement.empty()) {
190 os << '[' << params_.placement << ']';
192 os << '{' << params_.width.asLatexString() << "}%\n";
194 int const i = inset.latex(buf, os, runparams);
196 os << "\\end{floating" << params_.type << "}%\n";
201 int InsetWrap::docbook(Buffer const & buf, ostream & os, bool mixcont) const
203 os << '<' << params_.type << '>';
204 int const i = inset.docbook(buf, os, mixcont);
205 os << "</" << params_.type << '>';
211 bool InsetWrap::insetAllowed(InsetOld::Code code) const
219 return InsetCollapsable::insetAllowed(code);
224 int InsetWrap::latexTextWidth(BufferView * bv) const
226 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
230 bool InsetWrap::showInsetDialog(BufferView * bv) const
232 if (!inset.showInsetDialog(bv)) {
233 InsetWrap * tmp = const_cast<InsetWrap *>(this);
234 InsetWrapMailer(*tmp).showDialog(bv);
240 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
242 // Now find the caption in the float...
243 ParagraphList::iterator tmp = inset.paragraphs.begin();
244 ParagraphList::iterator end = inset.paragraphs.end();
246 for (; tmp != end; ++tmp) {
247 if (tmp->layout()->name() == caplayout) {
248 string const name = floatname(params_.type, buf.params());
250 tostr(toclist[name].size() + 1)
251 + ". " + tmp->asString(buf, false);
252 lyx::toc::TocItem const item(tmp->id(), 0 , str);
253 toclist[name].push_back(item);
259 string const InsetWrapMailer::name_("wrap");
261 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
266 string const InsetWrapMailer::inset2string(Buffer const &) const
268 return params2string(inset_.params());
272 void InsetWrapMailer::string2params(string const & in,
273 InsetWrapParams & params)
275 params = InsetWrapParams();
280 istringstream data(in);
286 string const token = lex.getString();
291 // This is part of the inset proper that is usually swallowed
292 // by Buffer::readInset
295 string const token = lex.getString();
296 if (token != "Wrap" || !lex.eatLine())
306 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
309 data << name_ << ' ';