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"
28 #include "frontends/LyXView.h"
29 #include "frontends/Dialogs.h"
31 #include "support/LOstream.h"
32 #include "support/tostr.h"
39 // this should not be hardcoded, but be part of the definition
40 // of the float (JMarc)
41 string const caplayout("Caption");
42 string floatname(string const & type, BufferParams const & bp)
44 FloatList const & floats = bp.getLyXTextClass().floats();
45 FloatList::const_iterator it = floats[type];
46 if (it == floats.end())
49 return _(it->second.name());
55 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
56 : InsetCollapsable(bp)
58 string lab(_("wrap: "));
59 lab += floatname(type, bp);
61 LyXFont font(LyXFont::ALL_SANE);
64 font.setColor(LColor::collapsable);
67 params_.width = LyXLength(50, LyXLength::PCW);
69 LyXTextClass const & tclass = bp.getLyXTextClass();
70 if (tclass.hasLayout(caplayout))
71 inset.paragraphs.begin()->layout(tclass[caplayout]);
75 InsetWrap::InsetWrap(InsetWrap const & in, bool same_id)
76 : InsetCollapsable(in, same_id), params_(in.params_)
80 InsetWrap::~InsetWrap()
82 InsetWrapMailer mailer(*this);
87 dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
89 Inset::RESULT result = UNDISPATCHED;
92 case LFUN_INSET_MODIFY: {
93 InsetWrapParams params;
94 InsetWrapMailer::string2params(cmd.argument, params);
96 params_.placement = params.placement;
97 params_.width = params.width;
99 cmd.view()->updateInset(this);
104 case LFUN_INSET_DIALOG_UPDATE: {
105 InsetWrapMailer mailer(*this);
106 mailer.updateDialog(cmd.view());
111 result = InsetCollapsable::localDispatch(cmd);
118 void InsetWrapParams::write(ostream & os) const
120 os << "Wrap " // getInsetName()
123 if (!placement.empty()) {
124 os << "placement " << placement << "\n";
126 os << "width \"" << width.asString() << "\"\n";
130 void InsetWrapParams::read(LyXLex & lex)
134 string token = lex.getString();
135 if (token == "placement") {
137 placement = lex.getString();
139 // take countermeasures
140 lex.pushToken(token);
145 string token = lex.getString();
146 if (token == "width") {
148 width = LyXLength(lex.getString());
150 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
152 // take countermeasures
153 lex.pushToken(token);
159 void InsetWrap::write(Buffer const * buf, ostream & os) const
162 InsetCollapsable::write(buf, os);
166 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
169 InsetCollapsable::read(buf, lex);
173 void InsetWrap::validate(LaTeXFeatures & features) const
175 features.require("floatflt");
176 InsetCollapsable::validate(features);
180 Inset * InsetWrap::clone(Buffer const &, bool same_id) const
182 return new InsetWrap(*const_cast<InsetWrap *>(this), same_id);
186 string const InsetWrap::editMessage() const
188 return _("Opened Wrap Inset");
192 int InsetWrap::latex(Buffer const * buf,
193 ostream & os, bool fragile, bool fp) const
195 os << "\\begin{floating" << params_.type << '}';
196 if (!params_.placement.empty()) {
197 os << '[' << params_.placement << ']';
199 os << '{' << params_.width.asLatexString() << "}%\n";
201 int const i = inset.latex(buf, os, fragile, fp);
203 os << "\\end{floating" << params_.type << "}%\n";
208 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
210 os << '<' << params_.type << '>';
211 int const i = inset.docbook(buf, os, mixcont);
212 os << "</" << params_.type << '>';
218 bool InsetWrap::insetAllowed(Inset::Code code) const
226 return InsetCollapsable::insetAllowed(code);
231 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
235 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
238 if (!params_.width.zero()) {
239 int const ww1 = latexTextWidth(bv);
240 int const ww2 = InsetCollapsable::getMaxWidth(bv, inset);
241 if (ww2 > 0 && ww2 < ww1) {
246 // this should not happen!
247 return InsetCollapsable::getMaxWidth(bv, inset);
251 int InsetWrap::latexTextWidth(BufferView * bv) const
253 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
257 bool InsetWrap::showInsetDialog(BufferView * bv) const
259 if (!inset.showInsetDialog(bv)) {
260 InsetWrap * tmp = const_cast<InsetWrap *>(this);
261 InsetWrapMailer mailer(*tmp);
262 mailer.showDialog(bv);
268 void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
270 // Now find the caption in the float...
271 ParagraphList::iterator tmp = inset.paragraphs.begin();
272 ParagraphList::iterator end = inset.paragraphs.end();
274 for (; tmp != end; ++tmp) {
275 if (tmp->layout()->name() == caplayout) {
276 string const name = floatname(params_.type, buf->params);
278 tostr(toclist[name].size() + 1)
279 + ". " + tmp->asString(buf, false);
280 toc::TocItem const item(tmp->id(), 0 , str);
281 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();
308 istringstream data(in);
314 string const token = lex.getString();
319 // This is part of the inset proper that is usually swallowed
320 // by Buffer::readInset
323 string const token = lex.getString();
324 if (token != "Wrap" || !lex.eatLine())
335 InsetWrapMailer::params2string(InsetWrapParams const & params)
338 data << name_ << ' ';