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"
15 #include "InsetCaption.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
22 #include "DispatchResult.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "LaTeXFeatures.h"
29 #include "TextClass.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
35 #include "frontends/Application.h"
42 InsetWrap::InsetWrap(Buffer const & buf, string const & type)
43 : InsetCollapsable(buf)
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 hideDialogs("wrap", this);
60 docstring InsetWrap::name() const
62 return "Wrap:" + from_utf8(params_.type);
66 docstring InsetWrap::toolTip(BufferView const & bv, int x, int y) const
68 OutputParams rp(&buffer().params().encoding());
69 docstring default_tip = InsetCollapsable::toolTip(bv, x, y);
70 docstring caption_tip = getCaptionText(rp);
71 if (!isOpen(bv) && !caption_tip.empty())
72 return caption_tip + '\n' + default_tip;
77 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
80 case LFUN_INSET_MODIFY: {
81 InsetWrapParams params;
82 InsetWrap::string2params(to_utf8(cmd.argument()), params);
83 params_.lines = params.lines;
84 params_.placement = params.placement;
85 params_.overhang = params.overhang;
86 params_.width = params.width;
90 case LFUN_INSET_DIALOG_UPDATE:
91 cur.bv().updateDialog("wrap", params2string(params()));
95 InsetCollapsable::doDispatch(cur, cmd);
101 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
102 FuncStatus & flag) const
104 switch (cmd.action) {
105 case LFUN_INSET_MODIFY:
106 case LFUN_INSET_DIALOG_UPDATE:
107 flag.setEnabled(true);
111 return InsetCollapsable::getStatus(cur, cmd, flag);
116 void InsetWrap::updateLabels(ParIterator const & it)
118 setLabel(_("wrap: ") + floatName(params_.type, buffer().params()));
120 buffer().masterBuffer()->params().documentClass().counters();
121 string const saveflt = cnts.current_float();
123 // Tell to captions what the current float is
124 cnts.current_float(params().type);
126 InsetCollapsable::updateLabels(it);
129 cnts.current_float(saveflt);
133 void InsetWrapParams::write(ostream & os) const
135 os << "Wrap " << type << '\n';
136 os << "lines " << lines << '\n';
137 os << "placement " << placement << '\n';
138 os << "overhang " << overhang.asString() << '\n';
139 os << "width \"" << width.asString() << "\"\n";
143 void InsetWrapParams::read(Lexer & lex)
145 lex.setContext("InsetWrapParams::read");
146 lex >> "lines" >> lines;
147 lex >> "placement" >> placement;
148 lex >> "overhang" >> overhang;
149 lex >> "width" >> width;
153 void InsetWrap::write(ostream & os) const
156 InsetCollapsable::write(os);
160 void InsetWrap::read(Lexer & lex)
163 InsetCollapsable::read(lex);
167 void InsetWrap::validate(LaTeXFeatures & features) const
169 features.require("wrapfig");
170 features.inFloat(true);
171 InsetCollapsable::validate(features);
172 features.inFloat(false);
176 docstring InsetWrap::editMessage() const
178 return _("Opened Wrap Inset");
182 int InsetWrap::latex(odocstream & os, OutputParams const & runparams_in) const
184 OutputParams runparams(runparams_in);
185 runparams.inFloat = OutputParams::MAINFLOAT;
186 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
187 // no optional argument when lines are zero
188 if (params_.lines != 0)
189 os << '[' << params_.lines << ']';
190 os << '{' << from_ascii(params_.placement) << '}';
191 Length over(params_.overhang);
192 // no optional argument when the value is zero
193 if (over.value() != 0)
194 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
195 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
196 int const i = InsetText::latex(os, runparams);
197 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
202 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
204 os << '[' << buffer().B_("wrap") << ' '
205 << floatName(params_.type, buffer().params()) << ":\n";
206 InsetText::plaintext(os, runparams);
209 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
213 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
216 os << '<' << from_ascii(params_.type) << '>';
217 int const i = InsetText::docbook(os, runparams);
218 os << "</" << from_ascii(params_.type) << '>';
223 docstring InsetWrap::xhtml(odocstream &, OutputParams const & rp) const
225 string const len = params_.width.asHTMLString();
226 docstring retval = from_ascii("<div class='wrap'");
228 retval += from_ascii(" style='width: " + len + ";");
229 retval += from_ascii("'>");
231 docstring const deferred = InsetText::xhtml(os, rp);
232 retval += os.str() + from_ascii("</div>");
238 bool InsetWrap::insetAllowed(InsetCode code) const
246 return InsetCollapsable::insetAllowed(code);
251 bool InsetWrap::showInsetDialog(BufferView * bv) const
253 if (!InsetText::showInsetDialog(bv))
254 bv->showDialog("wrap", params2string(params()),
255 const_cast<InsetWrap *>(this));
260 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
262 params = InsetWrapParams();
263 istringstream data(in);
266 lex.setContext("InsetWrap::string2params");
268 lex >> "Wrap"; // Part of the inset proper, swallowed by Text::readInset
269 lex >> params.type; // We have to read the type here!
274 string InsetWrap::params2string(InsetWrapParams const & params)
277 data << "wrap" << ' ';