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"
29 #include "frontends/LyXView.h"
30 #include "frontends/Dialogs.h"
32 #include "support/LOstream.h"
33 #include "support/tostr.h"
41 // this should not be hardcoded, but be part of the definition
42 // of the float (JMarc)
43 string const caplayout("Caption");
45 string floatname(string const & type, BufferParams const & bp)
47 FloatList const & floats = bp.getLyXTextClass().floats();
48 FloatList::const_iterator it = floats[type];
49 if (it == floats.end())
52 return _(it->second.name());
58 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
59 : InsetCollapsable(bp)
61 string lab(_("wrap: "));
62 lab += floatname(type, bp);
64 LyXFont font(LyXFont::ALL_SANE);
67 font.setColor(LColor::collapsable);
70 params_.width = LyXLength(50, LyXLength::PCW);
72 LyXTextClass const & tclass = bp.getLyXTextClass();
73 if (tclass.hasLayout(caplayout))
74 inset.paragraphs.begin()->layout(tclass[caplayout]);
78 InsetWrap::InsetWrap(InsetWrap const & in)
79 : InsetCollapsable(in), params_(in.params_)
83 InsetWrap::~InsetWrap()
85 InsetWrapMailer mailer(*this);
90 dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
92 Inset::RESULT result = UNDISPATCHED;
95 case LFUN_INSET_MODIFY: {
96 InsetWrapParams params;
97 InsetWrapMailer::string2params(cmd.argument, params);
99 params_.placement = params.placement;
100 params_.width = params.width;
102 cmd.view()->updateInset(this);
107 case LFUN_INSET_DIALOG_UPDATE: {
108 InsetWrapMailer mailer(*this);
109 mailer.updateDialog(cmd.view());
114 result = InsetCollapsable::localDispatch(cmd);
121 void InsetWrapParams::write(ostream & os) const
123 os << "Wrap " // getInsetName()
126 if (!placement.empty()) {
127 os << "placement " << placement << "\n";
129 os << "width \"" << width.asString() << "\"\n";
133 void InsetWrapParams::read(LyXLex & lex)
137 string token = lex.getString();
138 if (token == "placement") {
140 placement = lex.getString();
142 // take countermeasures
143 lex.pushToken(token);
148 string token = lex.getString();
149 if (token == "width") {
151 width = LyXLength(lex.getString());
153 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
155 // take countermeasures
156 lex.pushToken(token);
162 void InsetWrap::write(Buffer const * buf, ostream & os) const
165 InsetCollapsable::write(buf, os);
169 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
172 InsetCollapsable::read(buf, lex);
176 void InsetWrap::validate(LaTeXFeatures & features) const
178 features.require("floatflt");
179 InsetCollapsable::validate(features);
183 Inset * InsetWrap::clone(Buffer const &) const
185 return new InsetWrap(*this);
189 string const InsetWrap::editMessage() const
191 return _("Opened Wrap Inset");
195 int InsetWrap::latex(Buffer const * buf, ostream & os,
196 LatexRunParams const & runparams) const
198 os << "\\begin{floating" << params_.type << '}';
199 if (!params_.placement.empty()) {
200 os << '[' << params_.placement << ']';
202 os << '{' << params_.width.asLatexString() << "}%\n";
204 int const i = inset.latex(buf, os, runparams);
206 os << "\\end{floating" << params_.type << "}%\n";
211 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
213 os << '<' << params_.type << '>';
214 int const i = inset.docbook(buf, os, mixcont);
215 os << "</" << params_.type << '>';
221 bool InsetWrap::insetAllowed(Inset::Code code) const
229 return InsetCollapsable::insetAllowed(code);
234 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
238 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
241 if (!params_.width.zero()) {
242 int const ww1 = latexTextWidth(bv);
243 int const ww2 = InsetCollapsable::getMaxWidth(bv, inset);
244 if (ww2 > 0 && ww2 < ww1) {
249 // this should not happen!
250 return InsetCollapsable::getMaxWidth(bv, inset);
254 int InsetWrap::latexTextWidth(BufferView * bv) const
256 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
260 bool InsetWrap::showInsetDialog(BufferView * bv) const
262 if (!inset.showInsetDialog(bv)) {
263 InsetWrap * tmp = const_cast<InsetWrap *>(this);
264 InsetWrapMailer mailer(*tmp);
265 mailer.showDialog(bv);
271 void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
273 // Now find the caption in the float...
274 ParagraphList::iterator tmp = inset.paragraphs.begin();
275 ParagraphList::iterator end = inset.paragraphs.end();
277 for (; tmp != end; ++tmp) {
278 if (tmp->layout()->name() == caplayout) {
279 string const name = floatname(params_.type, buf->params);
281 tostr(toclist[name].size() + 1)
282 + ". " + tmp->asString(buf, false);
283 toc::TocItem const item(tmp->id(), 0 , str);
284 toclist[name].push_back(item);
290 string const InsetWrapMailer::name_("wrap");
292 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
297 string const InsetWrapMailer::inset2string() const
299 return params2string(inset_.params());
303 void InsetWrapMailer::string2params(string const & in,
304 InsetWrapParams & params)
306 params = InsetWrapParams();
311 istringstream data(STRCONV(in));
317 string const token = lex.getString();
322 // This is part of the inset proper that is usually swallowed
323 // by Buffer::readInset
326 string const token = lex.getString();
327 if (token != "Wrap" || !lex.eatLine())
337 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
340 data << name_ << ' ';
342 return STRCONV(data.str());