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"
20 #include "FloatList.h"
22 #include "LaTeXFeatures.h"
27 #include "frontends/LyXView.h"
28 #include "frontends/Dialogs.h"
30 #include "support/LOstream.h"
31 #include "support/lstrings.h"
38 // this should not be hardcoded, but be part of the definition
39 // of the float (JMarc)
40 string const caplayout("Caption");
41 string floatname(string const & type, BufferParams const & bp)
43 FloatList const & floats = bp.getLyXTextClass().floats();
44 FloatList::const_iterator it = floats[type];
45 if (it == floats.end())
48 return _(it->second.name());
54 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
55 : InsetCollapsable(bp)
57 string lab(_("wrap: "));
58 lab += floatname(type, bp);
60 LyXFont font(LyXFont::ALL_SANE);
63 font.setColor(LColor::collapsable);
66 params_.width = LyXLength(50, LyXLength::PCW);
68 LyXTextClass const & tclass = bp.getLyXTextClass();
69 if (tclass.hasLayout(caplayout))
70 inset.paragraph()->layout(tclass[caplayout]);
74 InsetWrap::InsetWrap(InsetWrap const & in, bool same_id)
75 : InsetCollapsable(in, same_id), params_(in.params_)
79 InsetWrap::~InsetWrap()
81 InsetWrapMailer mailer(*this);
86 dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
88 Inset::RESULT result = UNDISPATCHED;
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, true);
103 result = InsetCollapsable::localDispatch(cmd);
110 void InsetWrapParams::write(ostream & os) const
112 os << "Wrap " // getInsetName()
115 if (!placement.empty()) {
116 os << "placement " << placement << "\n";
118 os << "width \"" << width.asString() << "\"\n";
122 void InsetWrapParams::read(LyXLex & lex)
126 string token = lex.getString();
127 if (token == "placement") {
129 placement = lex.getString();
131 // take countermeasures
132 lex.pushToken(token);
137 string token = lex.getString();
138 if (token == "width") {
140 width = LyXLength(lex.getString());
142 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
144 // take countermeasures
145 lex.pushToken(token);
151 void InsetWrap::write(Buffer const * buf, ostream & os) const
154 InsetCollapsable::write(buf, os);
158 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
161 InsetCollapsable::read(buf, lex);
165 void InsetWrap::validate(LaTeXFeatures & features) const
167 features.require("floatflt");
168 InsetCollapsable::validate(features);
172 Inset * InsetWrap::clone(Buffer const &, bool same_id) const
174 return new InsetWrap(*const_cast<InsetWrap *>(this), same_id);
178 string const InsetWrap::editMessage() const
180 return _("Opened Wrap Inset");
184 int InsetWrap::latex(Buffer const * buf,
185 ostream & os, bool fragile, bool fp) const
187 os << "\\begin{floating" << params_.type << '}';
188 if (!params_.placement.empty()) {
189 os << '[' << params_.placement << ']';
191 os << '{' << params_.width.asLatexString() << "}%\n";
193 int const i = inset.latex(buf, os, fragile, fp);
195 os << "\\end{floating" << params_.type << "}%\n";
200 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
202 os << '<' << params_.type << '>';
203 int const i = inset.docbook(buf, os, mixcont);
204 os << "</" << params_.type << '>';
210 bool InsetWrap::insetAllowed(Inset::Code code) const
218 return InsetCollapsable::insetAllowed(code);
223 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
227 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
230 if (!params_.width.zero()) {
231 int const ww1 = latexTextWidth(bv);
232 int const ww2 = InsetCollapsable::getMaxWidth(bv, inset);
233 if (ww2 > 0 && ww2 < ww1) {
238 // this should not happen!
239 return InsetCollapsable::getMaxWidth(bv, inset);
243 int InsetWrap::latexTextWidth(BufferView * bv) const
245 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
249 string const & InsetWrap::type() const
255 LyXLength const & InsetWrap::pageWidth() const
257 return params_.width;
261 void InsetWrap::pageWidth(LyXLength const & ll)
263 if (ll != params_.width) {
270 void InsetWrap::placement(string const & p)
272 params_.placement = p;
276 string const & InsetWrap::placement() const
278 return params_.placement;
282 bool InsetWrap::showInsetDialog(BufferView * bv) const
284 if (!inset.showInsetDialog(bv)) {
285 InsetWrap * tmp = const_cast<InsetWrap *>(this);
286 InsetWrapMailer mailer(*tmp);
293 void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
295 // Now find the caption in the float...
296 // We now tranverse the paragraphs of
298 Paragraph * tmp = inset.paragraph();
300 if (tmp->layout()->name() == caplayout) {
301 string const name = floatname(type(), buf->params);
303 tostr(toclist[name].size() + 1)
304 + ". " + tmp->asString(buf, false);
305 toc::TocItem const item(tmp->id(), 0 , str);
306 toclist[name].push_back(item);
313 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
314 : name_("wrap"), inset_(inset)
318 string const InsetWrapMailer::inset2string() const
320 return params2string(name(), inset_.params());
324 void InsetWrapMailer::string2params(string const & in,
325 InsetWrapParams & params)
327 params = InsetWrapParams();
330 string body = split(in, name, ' ');
332 if (name != "wrap" || body.empty())
335 // This is part of the inset proper that is usually swallowed
336 // by Buffer::readInset
337 body = split(body, name, '\n');
338 if (!prefixIs(name, "Wrap "))
341 istringstream data(body);
350 InsetWrapMailer::params2string(string const & name,
351 InsetWrapParams const & params)