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"
42 using std::istringstream;
44 using std::ostringstream;
47 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
48 : InsetCollapsable(bp)
50 setLabel(_("wrap: ") + floatName(type, bp));
51 LyXFont font(LyXFont::ALL_SANE);
54 font.setColor(LColor::collapsable);
57 params_.width = LyXLength(50, LyXLength::PCW);
62 InsetWrap::~InsetWrap()
64 InsetWrapMailer(*this).hideDialog();
68 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
71 case LFUN_INSET_MODIFY: {
72 InsetWrapParams params;
73 InsetWrapMailer::string2params(lyx::to_utf8(cmd.argument()), params);
74 params_.placement = params.placement;
75 params_.width = params.width;
79 case LFUN_INSET_DIALOG_UPDATE:
80 InsetWrapMailer(*this).updateDialog(&cur.bv());
83 case LFUN_MOUSE_RELEASE: {
84 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
85 InsetWrapMailer(*this).showDialog(&cur.bv());
88 InsetCollapsable::doDispatch(cur, cmd);
93 InsetCollapsable::doDispatch(cur, cmd);
99 bool InsetWrap::getStatus(LCursor & cur, FuncRequest const & cmd,
100 FuncStatus & flag) const
102 switch (cmd.action) {
103 case LFUN_INSET_MODIFY:
104 case LFUN_INSET_DIALOG_UPDATE:
109 return InsetCollapsable::getStatus(cur, cmd, flag);
114 void InsetWrapParams::write(ostream & os) const
116 os << "Wrap " << type << '\n';
118 if (!placement.empty())
119 os << "placement " << placement << "\n";
121 os << "width \"" << width.asString() << "\"\n";
125 void InsetWrapParams::read(LyXLex & lex)
129 if (token == "placement")
132 // take countermeasures
133 lex.pushToken(token);
138 if (token == "width") {
140 width = LyXLength(lex.getString());
142 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
144 // take countermeasures
145 lex.pushToken(token);
150 void InsetWrap::write(Buffer const & buf, ostream & os) const
153 InsetCollapsable::write(buf, os);
157 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
160 InsetCollapsable::read(buf, lex);
164 void InsetWrap::validate(LaTeXFeatures & features) const
166 features.require("floatflt");
167 InsetCollapsable::validate(features);
171 auto_ptr<InsetBase> InsetWrap::doClone() const
173 return auto_ptr<InsetBase>(new InsetWrap(*this));
177 docstring const InsetWrap::editMessage() const
179 return _("Opened Wrap Inset");
183 int InsetWrap::latex(Buffer const & buf, ostream & os,
184 OutputParams const & runparams) const
186 os << "\\begin{floating" << params_.type << '}';
187 if (!params_.placement.empty())
188 os << '[' << params_.placement << ']';
189 os << '{' << params_.width.asLatexString() << "}%\n";
190 int const i = InsetText::latex(buf, os, runparams);
191 os << "\\end{floating" << params_.type << "}%\n";
196 int InsetWrap::docbook(Buffer const & buf, ostream & os,
197 OutputParams const & runparams) const
199 os << '<' << params_.type << '>';
200 int const i = InsetText::docbook(buf, os, runparams);
201 os << "</" << params_.type << '>';
206 bool InsetWrap::insetAllowed(InsetBase::Code code) const
214 return InsetCollapsable::insetAllowed(code);
219 bool InsetWrap::showInsetDialog(BufferView * bv) const
221 if (!InsetText::showInsetDialog(bv))
222 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
227 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
229 ParConstIterator pit = par_const_iterator_begin(*this);
230 ParConstIterator end = par_const_iterator_end(*this);
232 // Find a caption layout in one of the (child inset's) pars
233 for (; pit != end; ++pit) {
234 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
235 string const type = params_.type;
236 docstring const str =
237 convert<docstring>(toclist[type].size() + 1)
238 + ". " + pit->asString(buf, false);
239 lyx::toc::TocItem const item(pit, 0, str);
240 toclist[type].push_back(item);
246 string const InsetWrapMailer::name_("wrap");
248 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
253 string const InsetWrapMailer::inset2string(Buffer const &) const
255 return params2string(inset_.params());
259 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
261 params = InsetWrapParams();
265 istringstream data(in);
271 if (!lex || name != name_)
272 return print_mailer_error("InsetWrapMailer", in, 1, name_);
274 // This is part of the inset proper that is usually swallowed
275 // by LyXText::readInset
278 if (!lex || id != "Wrap")
279 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
281 // We have to read the type here!
287 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
290 data << name_ << ' ';