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"
16 #include "bufferparams.h"
17 #include "BufferView.h"
20 #include "dispatchresult.h"
22 #include "FloatList.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "pariterator.h"
33 #include "support/convert.h"
40 using std::istringstream;
42 using std::ostringstream;
47 string floatname(string const & type, BufferParams const & bp)
49 FloatList const & floats = bp.getLyXTextClass().floats();
50 FloatList::const_iterator it = floats[type];
51 return (it == floats.end()) ? type : _(it->second.name());
57 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
58 : InsetCollapsable(bp)
60 setLabel(_("wrap: ") + floatname(type, bp));
61 LyXFont font(LyXFont::ALL_SANE);
64 font.setColor(LColor::collapsable);
67 params_.width = LyXLength(50, LyXLength::PCW);
72 InsetWrap::~InsetWrap()
74 InsetWrapMailer(*this).hideDialog();
78 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
81 case LFUN_INSET_MODIFY: {
82 InsetWrapParams params;
83 InsetWrapMailer::string2params(cmd.argument, params);
84 params_.placement = params.placement;
85 params_.width = params.width;
89 case LFUN_INSET_DIALOG_UPDATE:
90 InsetWrapMailer(*this).updateDialog(&cur.bv());
93 case LFUN_MOUSE_RELEASE: {
94 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
95 InsetWrapMailer(*this).showDialog(&cur.bv());
98 InsetCollapsable::doDispatch(cur, cmd);
103 InsetCollapsable::doDispatch(cur, cmd);
109 bool InsetWrap::getStatus(LCursor & cur, FuncRequest const & cmd,
110 FuncStatus & flag) const
112 switch (cmd.action) {
113 case LFUN_INSET_MODIFY:
114 case LFUN_INSET_DIALOG_UPDATE:
119 return InsetCollapsable::getStatus(cur, cmd, flag);
124 void InsetWrapParams::write(ostream & os) const
126 os << "Wrap " << type << '\n';
128 if (!placement.empty())
129 os << "placement " << placement << "\n";
131 os << "width \"" << width.asString() << "\"\n";
135 void InsetWrapParams::read(LyXLex & lex)
139 if (token == "placement")
142 // take countermeasures
143 lex.pushToken(token);
148 if (token == "width") {
150 width = LyXLength(lex.getString());
152 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
154 // take countermeasures
155 lex.pushToken(token);
160 void InsetWrap::write(Buffer const & buf, ostream & os) const
163 InsetCollapsable::write(buf, os);
167 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
170 InsetCollapsable::read(buf, lex);
174 void InsetWrap::validate(LaTeXFeatures & features) const
176 features.require("floatflt");
177 InsetCollapsable::validate(features);
181 auto_ptr<InsetBase> InsetWrap::doClone() const
183 return auto_ptr<InsetBase>(new InsetWrap(*this));
187 string const InsetWrap::editMessage() const
189 return _("Opened Wrap Inset");
193 int InsetWrap::latex(Buffer const & buf, ostream & os,
194 OutputParams const & runparams) const
196 os << "\\begin{floating" << params_.type << '}';
197 if (!params_.placement.empty())
198 os << '[' << params_.placement << ']';
199 os << '{' << params_.width.asLatexString() << "}%\n";
200 int const i = InsetText::latex(buf, os, runparams);
201 os << "\\end{floating" << params_.type << "}%\n";
206 int InsetWrap::docbook(Buffer const & buf, ostream & os,
207 OutputParams const & runparams) const
209 os << '<' << params_.type << '>';
210 int const i = InsetText::docbook(buf, os, runparams);
211 os << "</" << params_.type << '>';
216 bool InsetWrap::insetAllowed(InsetBase::Code code) const
224 return InsetCollapsable::insetAllowed(code);
229 bool InsetWrap::showInsetDialog(BufferView * bv) const
231 if (!InsetText::showInsetDialog(bv))
232 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
237 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
239 ParConstIterator pit = par_const_iterator_begin(*this);
240 ParConstIterator end = par_const_iterator_end(*this);
242 // Find a caption layout in one of the (child inset's) pars
243 for (; pit != end; ++pit) {
244 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
245 string const type = params_.type;
247 convert<string>(toclist[type].size() + 1)
248 + ". " + pit->asString(buf, false);
249 lyx::toc::TocItem const item(pit, 0 , str);
250 toclist[type].push_back(item);
256 string const InsetWrapMailer::name_("wrap");
258 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
263 string const InsetWrapMailer::inset2string(Buffer const &) const
265 return params2string(inset_.params());
269 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
271 params = InsetWrapParams();
275 istringstream data(in);
281 if (!lex || name != name_)
282 return print_mailer_error("InsetWrapMailer", in, 1, name_);
284 // This is part of the inset proper that is usually swallowed
285 // by LyXText::readInset
288 if (!lex || id != "Wrap")
289 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
291 // We have to read the type here!
297 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
300 data << name_ << ' ';