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(Buffer const & buf, string const & type)
43 : InsetCollapsable(buf), name_(from_utf8(type))
45 setLabel(_("wrap: ") + floatName(type, buf.params()));
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 setLabel(_("wrap: ") + floatName(params_.type, buffer().params()));
111 Counters & cnts = buffer().params().documentClass().counters();
112 string const saveflt = cnts.current_float();
114 // Tell to captions what the current float is
115 cnts.current_float(params().type);
117 InsetCollapsable::updateLabels(it);
120 cnts.current_float(saveflt);
124 void InsetWrapParams::write(ostream & os) const
126 os << "Wrap " << type << '\n';
127 os << "lines " << lines << "\n";
128 os << "placement " << placement << "\n";
129 os << "overhang " << overhang.asString() << "\n";
130 os << "width \"" << width.asString() << "\"\n";
134 void InsetWrapParams::read(Lexer & lex)
139 if (token == "lines")
142 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
144 // take countermeasures
145 lex.pushToken(token);
150 if (token == "placement")
153 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
155 lex.pushToken(token);
160 if (token == "overhang") {
162 overhang = Length(lex.getString());
164 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
166 lex.pushToken(token);
171 if (token == "width") {
173 width = Length(lex.getString());
175 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
177 lex.pushToken(token);
182 void InsetWrap::write(ostream & os) const
185 InsetCollapsable::write(os);
189 void InsetWrap::read(Lexer & lex)
192 InsetCollapsable::read(lex);
196 void InsetWrap::validate(LaTeXFeatures & features) const
198 features.require("wrapfig");
199 InsetCollapsable::validate(features);
203 docstring InsetWrap::editMessage() const
205 return _("Opened Wrap Inset");
209 int InsetWrap::latex(odocstream & os, OutputParams const & runparams) const
211 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
212 // no optional argument when lines are zero
213 if (params_.lines != 0)
214 os << '[' << params_.lines << ']';
215 os << '{' << from_ascii(params_.placement) << '}';
216 Length over(params_.overhang);
217 // no optional argument when the value is zero
218 if (over.value() != 0)
219 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
220 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
221 int const i = InsetText::latex(os, runparams);
222 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
227 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
229 os << '[' << buffer().B_("wrap") << ' '
230 << floatName(params_.type, buffer().params()) << ":\n";
231 InsetText::plaintext(os, runparams);
234 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
238 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
241 os << '<' << from_ascii(params_.type) << '>';
242 int const i = InsetText::docbook(os, runparams);
243 os << "</" << from_ascii(params_.type) << '>';
248 bool InsetWrap::insetAllowed(InsetCode code) const
256 return InsetCollapsable::insetAllowed(code);
261 bool InsetWrap::showInsetDialog(BufferView * bv) const
263 if (!InsetText::showInsetDialog(bv))
264 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
269 string const InsetWrapMailer::name_("wrap");
271 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
276 string const InsetWrapMailer::inset2string(Buffer const &) const
278 return params2string(inset_.params());
282 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
284 params = InsetWrapParams();
288 istringstream data(in);
294 if (!lex || name != name_)
295 return print_mailer_error("InsetWrapMailer", in, 1, name_);
297 // This is part of the inset proper that is usually swallowed
298 // by Text::readInset
301 if (!lex || id != "Wrap")
302 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
304 // We have to read the type here!
310 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
313 data << name_ << ' ';