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);
103 case LFUN_INSET_DIALOG_UPDATE: {
104 InsetWrapMailer mailer(*this);
105 mailer.updateDialog(cmd.view());
110 result = InsetCollapsable::localDispatch(cmd);
117 void InsetWrapParams::write(ostream & os) const
119 os << "Wrap " // getInsetName()
122 if (!placement.empty()) {
123 os << "placement " << placement << "\n";
125 os << "width \"" << width.asString() << "\"\n";
129 void InsetWrapParams::read(LyXLex & lex)
133 string token = lex.getString();
134 if (token == "placement") {
136 placement = lex.getString();
138 // take countermeasures
139 lex.pushToken(token);
144 string token = lex.getString();
145 if (token == "width") {
147 width = LyXLength(lex.getString());
149 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
151 // take countermeasures
152 lex.pushToken(token);
158 void InsetWrap::write(Buffer const * buf, ostream & os) const
161 InsetCollapsable::write(buf, os);
165 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
168 InsetCollapsable::read(buf, lex);
172 void InsetWrap::validate(LaTeXFeatures & features) const
174 features.require("floatflt");
175 InsetCollapsable::validate(features);
179 Inset * InsetWrap::clone(Buffer const &, bool same_id) const
181 return new InsetWrap(*const_cast<InsetWrap *>(this), same_id);
185 string const InsetWrap::editMessage() const
187 return _("Opened Wrap Inset");
191 int InsetWrap::latex(Buffer const * buf,
192 ostream & os, bool fragile, bool fp) const
194 os << "\\begin{floating" << params_.type << '}';
195 if (!params_.placement.empty()) {
196 os << '[' << params_.placement << ']';
198 os << '{' << params_.width.asLatexString() << "}%\n";
200 int const i = inset.latex(buf, os, fragile, fp);
202 os << "\\end{floating" << params_.type << "}%\n";
207 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
209 os << '<' << params_.type << '>';
210 int const i = inset.docbook(buf, os, mixcont);
211 os << "</" << params_.type << '>';
217 bool InsetWrap::insetAllowed(Inset::Code code) const
225 return InsetCollapsable::insetAllowed(code);
230 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
234 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
237 if (!params_.width.zero()) {
238 int const ww1 = latexTextWidth(bv);
239 int const ww2 = InsetCollapsable::getMaxWidth(bv, inset);
240 if (ww2 > 0 && ww2 < ww1) {
245 // this should not happen!
246 return InsetCollapsable::getMaxWidth(bv, inset);
250 int InsetWrap::latexTextWidth(BufferView * bv) const
252 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
256 bool InsetWrap::showInsetDialog(BufferView * bv) const
258 if (!inset.showInsetDialog(bv)) {
259 InsetWrap * tmp = const_cast<InsetWrap *>(this);
260 InsetWrapMailer mailer(*tmp);
261 mailer.showDialog(bv);
267 void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
269 // Now find the caption in the float...
270 // We now tranverse the paragraphs of
272 Paragraph * tmp = inset.paragraph();
274 if (tmp->layout()->name() == caplayout) {
275 string const name = floatname(params_.type, buf->params);
277 tostr(toclist[name].size() + 1)
278 + ". " + tmp->asString(buf, false);
279 toc::TocItem const item(tmp->id(), 0 , str);
280 toclist[name].push_back(item);
287 string const InsetWrapMailer::name_("wrap");
289 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
294 string const InsetWrapMailer::inset2string() const
296 return params2string(inset_.params());
300 void InsetWrapMailer::string2params(string const & in,
301 InsetWrapParams & params)
303 params = InsetWrapParams();
305 istringstream data(in);
311 string const token = lex.getString();
316 // This is part of the inset proper that is usually swallowed
317 // by Buffer::readInset
320 string const token = lex.getString();
321 if (token != "Wrap" || !lex.eatLine())
332 InsetWrapMailer::params2string(InsetWrapParams const & params)
335 data << name_ << ' ';