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 "output_xhtml.h"
30 #include "TextClass.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
36 #include "frontends/Application.h"
43 InsetWrap::InsetWrap(Buffer * buf, string const & type)
44 : InsetCollapsable(buf)
46 setLabel(_("wrap: ") + floatName(type));
49 params_.placement = "o";
50 params_.overhang = Length(0, Length::PCW);
51 params_.width = Length(50, Length::PCW);
55 InsetWrap::~InsetWrap()
57 hideDialogs("wrap", this);
61 docstring InsetWrap::name() const
63 return "Wrap:" + from_utf8(params_.type);
67 docstring InsetWrap::toolTip(BufferView const & bv, int x, int y) const
69 OutputParams rp(&buffer().params().encoding());
70 docstring default_tip = InsetCollapsable::toolTip(bv, x, y);
71 docstring caption_tip = getCaptionText(rp);
72 if (!isOpen(bv) && !caption_tip.empty())
73 return caption_tip + '\n' + default_tip;
78 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
81 case LFUN_INSET_MODIFY: {
82 InsetWrapParams params;
83 InsetWrap::string2params(to_utf8(cmd.argument()), params);
84 params_.lines = params.lines;
85 params_.placement = params.placement;
86 params_.overhang = params.overhang;
87 params_.width = params.width;
91 case LFUN_INSET_DIALOG_UPDATE:
92 cur.bv().updateDialog("wrap", params2string(params()));
96 InsetCollapsable::doDispatch(cur, cmd);
102 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
103 FuncStatus & flag) const
105 switch (cmd.action) {
106 case LFUN_INSET_MODIFY:
107 case LFUN_INSET_DIALOG_UPDATE:
108 flag.setEnabled(true);
112 return InsetCollapsable::getStatus(cur, cmd, flag);
117 void InsetWrap::updateBuffer(ParIterator const & it, UpdateType utype)
119 setLabel(_("wrap: ") + floatName(params_.type));
121 buffer().masterBuffer()->params().documentClass().counters();
122 if (utype == OutputUpdate) {
123 // counters are local to the wrap
124 cnts.saveLastCounter();
126 string const saveflt = cnts.current_float();
128 // Tell to captions what the current float is
129 cnts.current_float(params().type);
131 InsetCollapsable::updateBuffer(it, utype);
134 cnts.current_float(saveflt);
135 if (utype == OutputUpdate)
136 cnts.restoreLastCounter();
140 void InsetWrapParams::write(ostream & os) const
142 os << "Wrap " << type << '\n';
143 os << "lines " << lines << '\n';
144 os << "placement " << placement << '\n';
145 os << "overhang " << overhang.asString() << '\n';
146 os << "width \"" << width.asString() << "\"\n";
150 void InsetWrapParams::read(Lexer & lex)
152 lex.setContext("InsetWrapParams::read");
153 lex >> "lines" >> lines;
154 lex >> "placement" >> placement;
155 lex >> "overhang" >> overhang;
156 lex >> "width" >> width;
160 void InsetWrap::write(ostream & os) const
163 InsetCollapsable::write(os);
167 void InsetWrap::read(Lexer & lex)
170 InsetCollapsable::read(lex);
174 void InsetWrap::validate(LaTeXFeatures & features) const
176 features.require("wrapfig");
177 features.inFloat(true);
178 InsetCollapsable::validate(features);
179 features.inFloat(false);
183 int InsetWrap::latex(odocstream & os, OutputParams const & runparams_in) const
185 OutputParams runparams(runparams_in);
186 runparams.inFloat = OutputParams::MAINFLOAT;
187 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
188 // no optional argument when lines are zero
189 if (params_.lines != 0)
190 os << '[' << params_.lines << ']';
191 os << '{' << from_ascii(params_.placement) << '}';
192 Length over(params_.overhang);
193 // no optional argument when the value is zero
194 if (over.value() != 0)
195 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
196 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
197 int const i = InsetText::latex(os, runparams);
198 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
203 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
205 os << '[' << buffer().B_("wrap") << ' '
206 << floatName(params_.type) << ":\n";
207 InsetText::plaintext(os, runparams);
210 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
214 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
217 os << '<' << from_ascii(params_.type) << '>';
218 int const i = InsetText::docbook(os, runparams);
219 os << "</" << from_ascii(params_.type) << '>';
224 docstring InsetWrap::xhtml(XHTMLStream & xs, OutputParams const & rp) const
226 string const len = params_.width.asHTMLString();
227 string const width = len.empty() ? "50%" : len;
228 string const attr = "class='wrap' style='width: " + len + ";'";
229 xs << html::StartTag("div", attr);
230 docstring const deferred =
231 InsetText::insetAsXHTML(xs, rp, InsetText::WriteInnerTag);
233 xs << html::EndTag("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" << ' ';