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"
21 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
28 #include "OutputParams.h"
29 #include "TextClass.h"
30 #include "TocBackend.h"
32 #include "support/convert.h"
33 #include "support/docstream.h"
34 #include "support/debug.h"
35 #include "support/gettext.h"
42 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
43 : InsetCollapsable(bp), name_(from_utf8(type))
45 setLabel(_("wrap: ") + floatName(type, bp));
48 params_.placement = "o";
49 params_.overhang = Length(0, Length::PCW);
50 params_.width = Length(50, Length::PCW);
54 InsetWrap::~InsetWrap()
56 InsetWrapMailer(*this).hideDialog();
60 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
63 case LFUN_INSET_MODIFY: {
64 InsetWrapParams params;
65 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
66 params_.lines = params.lines;
67 params_.placement = params.placement;
68 params_.overhang = params.overhang;
69 params_.width = params.width;
73 case LFUN_INSET_DIALOG_UPDATE:
74 InsetWrapMailer(*this).updateDialog(&cur.bv());
77 case LFUN_MOUSE_RELEASE: {
78 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
79 InsetWrapMailer(*this).showDialog(&cur.bv());
82 InsetCollapsable::doDispatch(cur, cmd);
87 InsetCollapsable::doDispatch(cur, cmd);
93 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
94 FuncStatus & flag) const
97 case LFUN_INSET_MODIFY:
98 case LFUN_INSET_DIALOG_UPDATE:
103 return InsetCollapsable::getStatus(cur, cmd, flag);
108 void InsetWrap::updateLabels(ParIterator const & it)
110 Counters & cnts = buffer().params().documentClass().counters();
111 string const saveflt = cnts.current_float();
113 // Tell to captions what the current float is
114 cnts.current_float(params().type);
116 InsetCollapsable::updateLabels(it);
119 cnts.current_float(saveflt);
123 void InsetWrapParams::write(ostream & os) const
125 os << "Wrap " << type << '\n';
126 os << "lines " << lines << "\n";
127 os << "placement " << placement << "\n";
128 os << "overhang " << overhang.asString() << "\n";
129 os << "width \"" << width.asString() << "\"\n";
133 void InsetWrapParams::read(Lexer & lex)
138 if (token == "lines")
141 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
143 // take countermeasures
144 lex.pushToken(token);
149 if (token == "placement")
152 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
154 lex.pushToken(token);
159 if (token == "overhang") {
161 overhang = Length(lex.getString());
163 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
165 lex.pushToken(token);
170 if (token == "width") {
172 width = Length(lex.getString());
174 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
176 lex.pushToken(token);
181 void InsetWrap::write(ostream & os) const
184 InsetCollapsable::write(os);
188 void InsetWrap::read(Lexer & lex)
191 InsetCollapsable::read(lex);
195 void InsetWrap::draw(PainterInfo & pi, int x, int y) const
197 const_cast<InsetWrap &>(*this).setLabel(
198 _("wrap: ") + floatName(params_.type, buffer().params()));
199 InsetCollapsable::draw(pi, x, y);
203 void InsetWrap::validate(LaTeXFeatures & features) const
205 features.require("wrapfig");
206 InsetCollapsable::validate(features);
210 Inset * InsetWrap::clone() const
212 return new InsetWrap(*this);
216 docstring InsetWrap::editMessage() const
218 return _("Opened Wrap Inset");
222 int InsetWrap::latex(odocstream & os, OutputParams const & runparams) const
224 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
225 // no optional argument when lines are zero
226 if (params_.lines != 0)
227 os << '[' << params_.lines << ']';
228 os << '{' << from_ascii(params_.placement) << '}';
229 Length over(params_.overhang);
230 // no optional argument when the value is zero
231 if (over.value() != 0)
232 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
233 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
234 int const i = InsetText::latex(os, runparams);
235 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
240 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
242 os << '[' << buffer().B_("wrap") << ' '
243 << floatName(params_.type, buffer().params()) << ":\n";
244 InsetText::plaintext(os, runparams);
247 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
251 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
254 os << '<' << from_ascii(params_.type) << '>';
255 int const i = InsetText::docbook(os, runparams);
256 os << "</" << from_ascii(params_.type) << '>';
261 bool InsetWrap::insetAllowed(InsetCode code) const
269 return InsetCollapsable::insetAllowed(code);
274 bool InsetWrap::showInsetDialog(BufferView * bv) const
276 if (!InsetText::showInsetDialog(bv))
277 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
282 string const InsetWrapMailer::name_("wrap");
284 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
289 string const InsetWrapMailer::inset2string(Buffer const &) const
291 return params2string(inset_.params());
295 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
297 params = InsetWrapParams();
301 istringstream data(in);
307 if (!lex || name != name_)
308 return print_mailer_error("InsetWrapMailer", in, 1, name_);
310 // This is part of the inset proper that is usually swallowed
311 // by Text::readInset
314 if (!lex || id != "Wrap")
315 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
317 // We have to read the type here!
323 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
326 data << name_ << ' ';