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(this);
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 " // getInsetName()
113 if (!placement.empty()) {
114 os << "placement " << placement << "\n";
116 os << "width \"" << width.asString() << "\"\n";
120 void InsetWrapParams::read(LyXLex & lex)
124 string token = lex.getString();
125 if (token == "placement") {
127 placement = lex.getString();
129 // take countermeasures
130 lex.pushToken(token);
135 string token = lex.getString();
136 if (token == "width") {
138 width = LyXLength(lex.getString());
140 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
142 // take countermeasures
143 lex.pushToken(token);
149 void InsetWrap::write(Buffer const * buf, ostream & os) const
152 InsetCollapsable::write(buf, os);
156 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
159 InsetCollapsable::read(buf, lex);
163 void InsetWrap::validate(LaTeXFeatures & features) const
165 features.require("floatflt");
166 InsetCollapsable::validate(features);
170 Inset * InsetWrap::clone() const
172 return new InsetWrap(*this);
176 string const InsetWrap::editMessage() const
178 return _("Opened Wrap Inset");
182 int InsetWrap::latex(Buffer const * buf, ostream & os,
183 LatexRunParams const & runparams) const
185 os << "\\begin{floating" << params_.type << '}';
186 if (!params_.placement.empty()) {
187 os << '[' << params_.placement << ']';
189 os << '{' << params_.width.asLatexString() << "}%\n";
191 int const i = inset.latex(buf, os, runparams);
193 os << "\\end{floating" << params_.type << "}%\n";
198 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
200 os << '<' << params_.type << '>';
201 int const i = inset.docbook(buf, os, mixcont);
202 os << "</" << params_.type << '>';
208 bool InsetWrap::insetAllowed(Inset::Code code) const
216 return InsetCollapsable::insetAllowed(code);
221 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
225 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
228 if (!params_.width.zero()) {
229 int const ww1 = latexTextWidth(bv);
230 int const ww2 = InsetCollapsable::getMaxWidth(bv, inset);
231 if (ww2 > 0 && ww2 < ww1) {
236 // this should not happen!
237 return InsetCollapsable::getMaxWidth(bv, inset);
241 int InsetWrap::latexTextWidth(BufferView * bv) const
243 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
247 bool InsetWrap::showInsetDialog(BufferView * bv) const
249 if (!inset.showInsetDialog(bv)) {
250 InsetWrap * tmp = const_cast<InsetWrap *>(this);
251 InsetWrapMailer(*tmp).showDialog(bv);
257 void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
259 // Now find the caption in the float...
260 ParagraphList::iterator tmp = inset.paragraphs.begin();
261 ParagraphList::iterator end = inset.paragraphs.end();
263 for (; tmp != end; ++tmp) {
264 if (tmp->layout()->name() == caplayout) {
265 string const name = floatname(params_.type, buf->params);
267 tostr(toclist[name].size() + 1)
268 + ". " + tmp->asString(buf, false);
269 toc::TocItem const item(tmp->id(), 0 , str);
270 toclist[name].push_back(item);
276 string const InsetWrapMailer::name_("wrap");
278 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
283 string const InsetWrapMailer::inset2string() const
285 return params2string(inset_.params());
289 void InsetWrapMailer::string2params(string const & in,
290 InsetWrapParams & params)
292 params = InsetWrapParams();
297 istringstream data(STRCONV(in));
303 string const token = lex.getString();
308 // This is part of the inset proper that is usually swallowed
309 // by Buffer::readInset
312 string const token = lex.getString();
313 if (token != "Wrap" || !lex.eatLine())
323 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
326 data << name_ << ' ';
328 return STRCONV(data.str());