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"
14 #include "insettext.h"
17 #include "BufferView.h"
19 #include "funcrequest.h"
21 #include "FloatList.h"
23 #include "LaTeXFeatures.h"
30 #include "frontends/LyXView.h"
31 #include "frontends/Dialogs.h"
33 #include "support/LOstream.h"
34 #include "support/tostr.h"
42 // this should not be hardcoded, but be part of the definition
43 // of the float (JMarc)
44 string const caplayout("Caption");
46 string floatname(string const & type, BufferParams const & bp)
48 FloatList const & floats = bp.getLyXTextClass().floats();
49 FloatList::const_iterator it = floats[type];
50 if (it == floats.end())
53 return _(it->second.name());
59 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
60 : InsetCollapsable(bp)
62 string lab(_("wrap: "));
63 lab += floatname(type, bp);
65 LyXFont font(LyXFont::ALL_SANE);
68 font.setColor(LColor::collapsable);
71 params_.width = LyXLength(50, LyXLength::PCW);
73 LyXTextClass const & tclass = bp.getLyXTextClass();
74 if (tclass.hasLayout(caplayout))
75 inset.paragraphs.begin()->layout(tclass[caplayout]);
79 InsetWrap::~InsetWrap()
81 InsetWrapMailer(*this).hideDialog();
85 dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
88 case LFUN_INSET_MODIFY: {
89 InsetWrapParams params;
90 InsetWrapMailer::string2params(cmd.argument, params);
92 params_.placement = params.placement;
93 params_.width = params.width;
95 cmd.view()->updateInset(this);
99 case LFUN_INSET_DIALOG_UPDATE:
100 InsetWrapMailer(*this).updateDialog(cmd.view());
104 return InsetCollapsable::localDispatch(cmd);
109 void InsetWrapParams::write(ostream & os) const
111 os << "Wrap " << type << '\n';
113 if (!placement.empty())
114 os << "placement " << placement << "\n";
116 os << "width \"" << width.asString() << "\"\n";
120 void InsetWrapParams::read(LyXLex & lex)
124 string token = lex.getString();
125 if (token == "placement") {
127 placement = lex.getString();
129 // take countermeasures
130 lex.pushToken(token);
135 string token = lex.getString();
136 if (token == "width") {
138 width = LyXLength(lex.getString());
140 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
142 // take countermeasures
143 lex.pushToken(token);
149 void InsetWrap::write(Buffer const & buf, ostream & os) const
152 InsetCollapsable::write(buf, os);
156 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
159 InsetCollapsable::read(buf, lex);
163 void InsetWrap::validate(LaTeXFeatures & features) const
165 features.require("floatflt");
166 InsetCollapsable::validate(features);
170 auto_ptr<InsetBase> InsetWrap::clone() const
172 return auto_ptr<InsetBase>(new InsetWrap(*this));
176 string const InsetWrap::editMessage() const
178 return _("Opened Wrap Inset");
182 int InsetWrap::latex(Buffer const & buf, ostream & os,
183 LatexRunParams const & runparams) const
185 os << "\\begin{floating" << params_.type << '}';
186 if (!params_.placement.empty()) {
187 os << '[' << params_.placement << ']';
189 os << '{' << params_.width.asLatexString() << "}%\n";
191 int const i = inset.latex(buf, os, runparams);
193 os << "\\end{floating" << params_.type << "}%\n";
198 int InsetWrap::docbook(Buffer const & buf, ostream & os, bool mixcont) const
200 os << '<' << params_.type << '>';
201 int const i = inset.docbook(buf, os, mixcont);
202 os << "</" << params_.type << '>';
208 bool InsetWrap::insetAllowed(InsetOld::Code code) const
216 return InsetCollapsable::insetAllowed(code);
221 int InsetWrap::latexTextWidth(BufferView * bv) const
223 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
227 bool InsetWrap::showInsetDialog(BufferView * bv) const
229 if (!inset.showInsetDialog(bv)) {
230 InsetWrap * tmp = const_cast<InsetWrap *>(this);
231 InsetWrapMailer(*tmp).showDialog(bv);
237 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
239 // Now find the caption in the float...
240 ParagraphList::iterator tmp = inset.paragraphs.begin();
241 ParagraphList::iterator end = inset.paragraphs.end();
243 for (; tmp != end; ++tmp) {
244 if (tmp->layout()->name() == caplayout) {
245 string const name = floatname(params_.type, buf.params);
247 tostr(toclist[name].size() + 1)
248 + ". " + tmp->asString(buf, false);
249 lyx::toc::TocItem const item(tmp->id(), 0 , str);
250 toclist[name].push_back(item);
256 string const InsetWrapMailer::name_("wrap");
258 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
263 string const InsetWrapMailer::inset2string(Buffer const &) const
265 return params2string(inset_.params());
269 void InsetWrapMailer::string2params(string const & in,
270 InsetWrapParams & params)
272 params = InsetWrapParams();
277 istringstream data(STRCONV(in));
283 string const token = lex.getString();
288 // This is part of the inset proper that is usually swallowed
289 // by Buffer::readInset
292 string const token = lex.getString();
293 if (token != "Wrap" || !lex.eatLine())
303 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
306 data << name_ << ' ';
308 return STRCONV(data.str());