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()
80 InsetWrapMailer(*this).hideDialog();
84 dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
87 case LFUN_INSET_MODIFY: {
88 InsetWrapParams params;
89 InsetWrapMailer::string2params(cmd.argument, params);
91 params_.placement = params.placement;
92 params_.width = params.width;
94 cmd.view()->updateInset();
98 case LFUN_INSET_DIALOG_UPDATE:
99 InsetWrapMailer(*this).updateDialog(cmd.view());
103 return InsetCollapsable::localDispatch(cmd);
108 void InsetWrapParams::write(ostream & os) const
110 os << "Wrap " << type << '\n';
112 if (!placement.empty())
113 os << "placement " << placement << "\n";
115 os << "width \"" << width.asString() << "\"\n";
119 void InsetWrapParams::read(LyXLex & lex)
123 string token = lex.getString();
124 if (token == "placement") {
126 placement = lex.getString();
128 // take countermeasures
129 lex.pushToken(token);
134 string token = lex.getString();
135 if (token == "width") {
137 width = LyXLength(lex.getString());
139 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
141 // take countermeasures
142 lex.pushToken(token);
148 void InsetWrap::write(Buffer const * buf, ostream & os) const
151 InsetCollapsable::write(buf, os);
155 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
158 InsetCollapsable::read(buf, lex);
162 void InsetWrap::validate(LaTeXFeatures & features) const
164 features.require("floatflt");
165 InsetCollapsable::validate(features);
169 auto_ptr<InsetBase> InsetWrap::clone() const
171 return auto_ptr<InsetBase>(new InsetWrap(*this));
175 string const InsetWrap::editMessage() const
177 return _("Opened Wrap Inset");
181 int InsetWrap::latex(Buffer const * buf, ostream & os,
182 LatexRunParams const & runparams) const
184 os << "\\begin{floating" << params_.type << '}';
185 if (!params_.placement.empty()) {
186 os << '[' << params_.placement << ']';
188 os << '{' << params_.width.asLatexString() << "}%\n";
190 int const i = inset.latex(buf, os, runparams);
192 os << "\\end{floating" << params_.type << "}%\n";
197 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
199 os << '<' << params_.type << '>';
200 int const i = inset.docbook(buf, os, mixcont);
201 os << "</" << params_.type << '>';
207 bool InsetWrap::insetAllowed(InsetOld::Code code) const
215 return InsetCollapsable::insetAllowed(code);
220 int InsetWrap::latexTextWidth(BufferView * bv) const
222 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
226 bool InsetWrap::showInsetDialog(BufferView * bv) const
228 if (!inset.showInsetDialog(bv)) {
229 InsetWrap * tmp = const_cast<InsetWrap *>(this);
230 InsetWrapMailer(*tmp).showDialog(bv);
236 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const * buf) const
238 // Now find the caption in the float...
239 ParagraphList::iterator tmp = inset.paragraphs.begin();
240 ParagraphList::iterator end = inset.paragraphs.end();
242 for (; tmp != end; ++tmp) {
243 if (tmp->layout()->name() == caplayout) {
244 string const name = floatname(params_.type, buf->params);
246 tostr(toclist[name].size() + 1)
247 + ". " + tmp->asString(buf, false);
248 lyx::toc::TocItem const item(tmp->id(), 0 , str);
249 toclist[name].push_back(item);
255 string const InsetWrapMailer::name_("wrap");
257 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
262 string const InsetWrapMailer::inset2string(Buffer const &) const
264 return params2string(inset_.params());
268 void InsetWrapMailer::string2params(string const & in,
269 InsetWrapParams & params)
271 params = InsetWrapParams();
276 istringstream data(STRCONV(in));
282 string const token = lex.getString();
287 // This is part of the inset proper that is usually swallowed
288 // by Buffer::readInset
291 string const token = lex.getString();
292 if (token != "Wrap" || !lex.eatLine())
302 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
305 data << name_ << ' ';
307 return STRCONV(data.str());