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"
37 #include "frontends/Application.h"
44 InsetWrap::InsetWrap(Buffer const & buf, string const & type)
45 : InsetCollapsable(buf), name_(from_utf8(type))
47 setLabel(_("wrap: ") + floatName(type, buf.params()));
50 params_.placement = "o";
51 params_.overhang = Length(0, Length::PCW);
52 params_.width = Length(50, Length::PCW);
56 InsetWrap::~InsetWrap()
58 hideDialogs("wrap", this);
62 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
65 case LFUN_INSET_MODIFY: {
66 InsetWrapParams params;
67 InsetWrap::string2params(to_utf8(cmd.argument()), params);
68 params_.lines = params.lines;
69 params_.placement = params.placement;
70 params_.overhang = params.overhang;
71 params_.width = params.width;
75 case LFUN_INSET_DIALOG_UPDATE:
76 cur.bv().updateDialog("wrap", params2string(params()));
80 InsetCollapsable::doDispatch(cur, cmd);
86 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
87 FuncStatus & flag) const
90 case LFUN_INSET_MODIFY:
91 case LFUN_INSET_DIALOG_UPDATE:
96 return InsetCollapsable::getStatus(cur, cmd, flag);
101 void InsetWrap::updateLabels(ParIterator const & it)
103 setLabel(_("wrap: ") + floatName(params_.type, buffer().params()));
104 Counters & cnts = buffer().params().documentClass().counters();
105 string const saveflt = cnts.current_float();
107 // Tell to captions what the current float is
108 cnts.current_float(params().type);
110 InsetCollapsable::updateLabels(it);
113 cnts.current_float(saveflt);
117 void InsetWrapParams::write(ostream & os) const
119 os << "Wrap " << type << '\n';
120 os << "lines " << lines << "\n";
121 os << "placement " << placement << "\n";
122 os << "overhang " << overhang.asString() << "\n";
123 os << "width \"" << width.asString() << "\"\n";
127 void InsetWrapParams::read(Lexer & lex)
132 if (token == "lines")
135 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
137 // take countermeasures
138 lex.pushToken(token);
143 if (token == "placement")
146 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
148 lex.pushToken(token);
153 if (token == "overhang") {
155 overhang = Length(lex.getString());
157 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
159 lex.pushToken(token);
164 if (token == "width") {
166 width = Length(lex.getString());
168 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
170 lex.pushToken(token);
175 void InsetWrap::write(ostream & os) const
178 InsetCollapsable::write(os);
182 void InsetWrap::read(Lexer & lex)
185 InsetCollapsable::read(lex);
189 void InsetWrap::validate(LaTeXFeatures & features) const
191 features.require("wrapfig");
192 InsetCollapsable::validate(features);
196 docstring InsetWrap::editMessage() const
198 return _("Opened Wrap Inset");
202 int InsetWrap::latex(odocstream & os, OutputParams const & runparams) const
204 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
205 // no optional argument when lines are zero
206 if (params_.lines != 0)
207 os << '[' << params_.lines << ']';
208 os << '{' << from_ascii(params_.placement) << '}';
209 Length over(params_.overhang);
210 // no optional argument when the value is zero
211 if (over.value() != 0)
212 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
213 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
214 int const i = InsetText::latex(os, runparams);
215 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
220 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
222 os << '[' << buffer().B_("wrap") << ' '
223 << floatName(params_.type, buffer().params()) << ":\n";
224 InsetText::plaintext(os, runparams);
227 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
231 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
234 os << '<' << from_ascii(params_.type) << '>';
235 int const i = InsetText::docbook(os, runparams);
236 os << "</" << from_ascii(params_.type) << '>';
241 bool InsetWrap::insetAllowed(InsetCode code) const
249 return InsetCollapsable::insetAllowed(code);
254 bool InsetWrap::showInsetDialog(BufferView * bv) const
256 if (!InsetText::showInsetDialog(bv))
257 bv->showDialog("wrap", params2string(params()),
258 const_cast<InsetWrap *>(this));
263 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
265 params = InsetWrapParams();
269 istringstream data(in);
275 if (!lex || name != "wrap") {
276 LYXERR0("Expected arg 1 to be \"wrap\" in " << in);
280 // This is part of the inset proper that is usually swallowed
281 // by Text::readInset
284 if (!lex || id != "Wrap") {
285 LYXERR0("Expected arg 2 to be \"Wrap\" in " << in);
289 // We have to read the type here!
295 string InsetWrap::params2string(InsetWrapParams const & params)
298 data << "wrap" << ' ';