3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "InsetWrap.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
22 #include "DispatchResult.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
30 #include "OutputParams.h"
31 #include "TocBackend.h"
33 #include "support/convert.h"
34 #include "support/docstream.h"
41 using std::istringstream;
43 using std::ostringstream;
46 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
47 : InsetCollapsable(bp), name_(from_utf8(type))
49 setLabel(_("wrap: ") + floatName(type, bp));
50 FontInfo font = sane_font;
53 font.setColor(Color_collapsable);
57 params_.placement = "o";
58 params_.overhang = Length(0, Length::PCW);
59 params_.width = Length(50, Length::PCW);
63 InsetWrap::~InsetWrap()
65 InsetWrapMailer(*this).hideDialog();
69 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
72 case LFUN_INSET_MODIFY: {
73 InsetWrapParams params;
74 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
75 params_.lines = params.lines;
76 params_.placement = params.placement;
77 params_.overhang = params.overhang;
78 params_.width = params.width;
82 case LFUN_INSET_DIALOG_UPDATE:
83 InsetWrapMailer(*this).updateDialog(&cur.bv());
86 case LFUN_MOUSE_RELEASE: {
87 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
88 InsetWrapMailer(*this).showDialog(&cur.bv());
91 InsetCollapsable::doDispatch(cur, cmd);
96 InsetCollapsable::doDispatch(cur, cmd);
102 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
103 FuncStatus & flag) const
105 switch (cmd.action) {
106 case LFUN_INSET_MODIFY:
107 case LFUN_INSET_DIALOG_UPDATE:
112 return InsetCollapsable::getStatus(cur, cmd, flag);
117 void InsetWrap::updateLabels(Buffer const & buf, ParIterator const & it)
119 Counters & cnts = buf.params().getTextClass().counters();
120 string const saveflt = cnts.current_float();
122 // Tell to captions what the current float is
123 cnts.current_float(params().type);
125 InsetCollapsable::updateLabels(buf, it);
128 cnts.current_float(saveflt);
132 void InsetWrapParams::write(ostream & os) const
134 os << "Wrap " << type << '\n';
135 os << "lines " << lines << "\n";
136 os << "placement " << placement << "\n";
137 os << "overhang " << overhang.asString() << "\n";
138 os << "width \"" << width.asString() << "\"\n";
142 void InsetWrapParams::read(Lexer & lex)
147 if (token == "lines")
150 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
152 // take countermeasures
153 lex.pushToken(token);
158 if (token == "placement")
161 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
163 lex.pushToken(token);
168 if (token == "overhang") {
170 overhang = Length(lex.getString());
172 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
174 lex.pushToken(token);
179 if (token == "width") {
181 width = Length(lex.getString());
183 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
185 lex.pushToken(token);
190 void InsetWrap::write(Buffer const & buf, ostream & os) const
193 InsetCollapsable::write(buf, os);
197 void InsetWrap::read(Buffer const & buf, Lexer & lex)
200 InsetCollapsable::read(buf, lex);
204 void InsetWrap::validate(LaTeXFeatures & features) const
206 features.require("wrapfig");
207 InsetCollapsable::validate(features);
211 Inset * InsetWrap::clone() const
213 return new InsetWrap(*this);
217 docstring const InsetWrap::editMessage() const
219 return _("Opened Wrap Inset");
223 int InsetWrap::latex(Buffer const & buf, odocstream & os,
224 OutputParams const & runparams) const
226 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
227 // no optional argument when lines are zero
228 if (params_.lines != 0)
229 os << '[' << params_.lines << ']';
230 os << '{' << from_ascii(params_.placement) << '}';
231 Length over(params_.overhang);
232 // no optional argument when the value is zero
233 if (over.value() != 0)
234 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
235 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
236 int const i = InsetText::latex(buf, os, runparams);
237 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
242 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
243 OutputParams const & runparams) const
245 os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
246 InsetText::plaintext(buf, os, runparams);
249 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
253 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
254 OutputParams const & runparams) const
257 os << '<' << from_ascii(params_.type) << '>';
258 int const i = InsetText::docbook(buf, os, runparams);
259 os << "</" << from_ascii(params_.type) << '>';
264 bool InsetWrap::insetAllowed(InsetCode code) const
272 return InsetCollapsable::insetAllowed(code);
277 bool InsetWrap::showInsetDialog(BufferView * bv) const
279 if (!InsetText::showInsetDialog(bv))
280 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
285 string const InsetWrapMailer::name_("wrap");
287 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
292 string const InsetWrapMailer::inset2string(Buffer const &) const
294 return params2string(inset_.params());
298 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
300 params = InsetWrapParams();
304 istringstream data(in);
310 if (!lex || name != name_)
311 return print_mailer_error("InsetWrapMailer", in, 1, name_);
313 // This is part of the inset proper that is usually swallowed
314 // by Text::readInset
317 if (!lex || id != "Wrap")
318 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
320 // We have to read the type here!
326 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
329 data << name_ << ' ';