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"
25 #include "paragraph.h"
27 #include "support/tostr.h"
29 #include "support/std_sstream.h"
33 using std::istringstream;
35 using std::ostringstream;
40 // this should not be hardcoded, but be part of the definition
41 // of the float (JMarc)
42 string const caplayout("Caption");
44 string floatname(string const & type, BufferParams const & bp)
46 FloatList const & floats = bp.getLyXTextClass().floats();
47 FloatList::const_iterator it = floats[type];
48 if (it == floats.end())
51 return _(it->second.name());
57 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
58 : InsetCollapsable(bp)
60 string lab(_("wrap: "));
61 lab += floatname(type, bp);
63 LyXFont font(LyXFont::ALL_SANE);
66 font.setColor(LColor::collapsable);
69 params_.width = LyXLength(50, LyXLength::PCW);
71 LyXTextClass const & tclass = bp.getLyXTextClass();
72 if (tclass.hasLayout(caplayout))
73 inset.paragraphs.begin()->layout(tclass[caplayout]);
77 InsetWrap::~InsetWrap()
79 InsetWrapMailer(*this).hideDialog();
83 dispatch_result InsetWrap::localDispatch(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;
93 cmd.view()->updateInset(this);
97 case LFUN_INSET_DIALOG_UPDATE:
98 InsetWrapMailer(*this).updateDialog(cmd.view());
102 return InsetCollapsable::localDispatch(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 LatexRunParams 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, bool mixcont) const
198 os << '<' << params_.type << '>';
199 int const i = inset.docbook(buf, os, mixcont);
200 os << "</" << params_.type << '>';
206 bool InsetWrap::insetAllowed(InsetOld::Code code) const
214 return InsetCollapsable::insetAllowed(code);
219 int InsetWrap::latexTextWidth(BufferView * bv) const
221 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
225 bool InsetWrap::showInsetDialog(BufferView * bv) const
227 if (!inset.showInsetDialog(bv)) {
228 InsetWrap * tmp = const_cast<InsetWrap *>(this);
229 InsetWrapMailer(*tmp).showDialog(bv);
235 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
237 // Now find the caption in the float...
238 ParagraphList::iterator tmp = inset.paragraphs.begin();
239 ParagraphList::iterator end = inset.paragraphs.end();
241 for (; tmp != end; ++tmp) {
242 if (tmp->layout()->name() == caplayout) {
243 string const name = floatname(params_.type, buf.params());
245 tostr(toclist[name].size() + 1)
246 + ". " + tmp->asString(buf, false);
247 lyx::toc::TocItem const item(tmp->id(), 0 , str);
248 toclist[name].push_back(item);
254 string const InsetWrapMailer::name_("wrap");
256 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
261 string const InsetWrapMailer::inset2string(Buffer const &) const
263 return params2string(inset_.params());
267 void InsetWrapMailer::string2params(string const & in,
268 InsetWrapParams & params)
270 params = InsetWrapParams();
275 istringstream data(STRCONV(in));
281 string const token = lex.getString();
286 // This is part of the inset proper that is usually swallowed
287 // by Buffer::readInset
290 string const token = lex.getString();
291 if (token != "Wrap" || !lex.eatLine())
301 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
304 data << name_ << ' ';
306 return STRCONV(data.str());