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());
78 InsetCollapsable::doDispatch(cur, cmd);
84 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
85 FuncStatus & flag) const
88 case LFUN_INSET_MODIFY:
89 case LFUN_INSET_DIALOG_UPDATE:
94 return InsetCollapsable::getStatus(cur, cmd, flag);
99 void InsetWrap::updateLabels(ParIterator const & it)
101 setLabel(_("wrap: ") + floatName(params_.type, buffer().params()));
102 Counters & cnts = buffer().params().documentClass().counters();
103 string const saveflt = cnts.current_float();
105 // Tell to captions what the current float is
106 cnts.current_float(params().type);
108 InsetCollapsable::updateLabels(it);
111 cnts.current_float(saveflt);
115 void InsetWrapParams::write(ostream & os) const
117 os << "Wrap " << type << '\n';
118 os << "lines " << lines << "\n";
119 os << "placement " << placement << "\n";
120 os << "overhang " << overhang.asString() << "\n";
121 os << "width \"" << width.asString() << "\"\n";
125 void InsetWrapParams::read(Lexer & lex)
130 if (token == "lines")
133 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
135 // take countermeasures
136 lex.pushToken(token);
141 if (token == "placement")
144 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
146 lex.pushToken(token);
151 if (token == "overhang") {
153 overhang = Length(lex.getString());
155 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
157 lex.pushToken(token);
162 if (token == "width") {
164 width = Length(lex.getString());
166 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
168 lex.pushToken(token);
173 void InsetWrap::write(ostream & os) const
176 InsetCollapsable::write(os);
180 void InsetWrap::read(Lexer & lex)
183 InsetCollapsable::read(lex);
187 void InsetWrap::validate(LaTeXFeatures & features) const
189 features.require("wrapfig");
190 InsetCollapsable::validate(features);
194 docstring InsetWrap::editMessage() const
196 return _("Opened Wrap Inset");
200 int InsetWrap::latex(odocstream & os, OutputParams const & runparams) const
202 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
203 // no optional argument when lines are zero
204 if (params_.lines != 0)
205 os << '[' << params_.lines << ']';
206 os << '{' << from_ascii(params_.placement) << '}';
207 Length over(params_.overhang);
208 // no optional argument when the value is zero
209 if (over.value() != 0)
210 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
211 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
212 int const i = InsetText::latex(os, runparams);
213 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
218 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
220 os << '[' << buffer().B_("wrap") << ' '
221 << floatName(params_.type, buffer().params()) << ":\n";
222 InsetText::plaintext(os, runparams);
225 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
229 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
232 os << '<' << from_ascii(params_.type) << '>';
233 int const i = InsetText::docbook(os, runparams);
234 os << "</" << from_ascii(params_.type) << '>';
239 bool InsetWrap::insetAllowed(InsetCode code) const
247 return InsetCollapsable::insetAllowed(code);
252 bool InsetWrap::showInsetDialog(BufferView * bv) const
254 if (!InsetText::showInsetDialog(bv))
255 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
260 string const InsetWrapMailer::name_("wrap");
262 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
267 string const InsetWrapMailer::inset2string(Buffer const &) const
269 return params2string(inset_.params());
273 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
275 params = InsetWrapParams();
279 istringstream data(in);
285 if (!lex || name != name_)
286 return print_mailer_error("InsetWrapMailer", in, 1, name_);
288 // This is part of the inset proper that is usually swallowed
289 // by Text::readInset
292 if (!lex || id != "Wrap")
293 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
295 // We have to read the type here!
301 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
304 data << name_ << ' ';