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
70 return InsetCollapsable::toolTip(bv, x, y);
71 OutputParams rp(&buffer().params().encoding());
72 docstring caption_tip = getCaptionText(rp);
73 if (!caption_tip.empty())
74 caption_tip += from_ascii("\n");
75 return toolTipText(caption_tip);
79 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
81 switch (cmd.action()) {
82 case LFUN_INSET_MODIFY: {
83 cur.recordUndoInset(ATOMIC_UNDO, this);
84 InsetWrapParams params;
85 InsetWrap::string2params(to_utf8(cmd.argument()), params);
86 params_.lines = params.lines;
87 params_.placement = params.placement;
88 params_.overhang = params.overhang;
89 params_.width = params.width;
93 case LFUN_INSET_DIALOG_UPDATE:
94 cur.bv().updateDialog("wrap", params2string(params()));
98 InsetCollapsable::doDispatch(cur, cmd);
104 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
105 FuncStatus & flag) const
107 switch (cmd.action()) {
108 case LFUN_INSET_MODIFY:
109 case LFUN_INSET_DIALOG_UPDATE:
110 flag.setEnabled(true);
114 return InsetCollapsable::getStatus(cur, cmd, flag);
119 void InsetWrap::updateBuffer(ParIterator const & it, UpdateType utype)
121 setLabel(_("wrap: ") + floatName(params_.type));
123 buffer().masterBuffer()->params().documentClass().counters();
124 if (utype == OutputUpdate) {
125 // counters are local to the wrap
126 cnts.saveLastCounter();
128 string const saveflt = cnts.current_float();
130 // Tell to captions what the current float is
131 cnts.current_float(params().type);
133 InsetCollapsable::updateBuffer(it, utype);
136 cnts.current_float(saveflt);
137 if (utype == OutputUpdate)
138 cnts.restoreLastCounter();
142 void InsetWrapParams::write(ostream & os) const
144 os << "Wrap " << type << '\n';
145 os << "lines " << lines << '\n';
146 os << "placement " << placement << '\n';
147 os << "overhang " << overhang.asString() << '\n';
148 os << "width \"" << width.asString() << "\"\n";
152 void InsetWrapParams::read(Lexer & lex)
154 lex.setContext("InsetWrapParams::read");
155 lex >> "lines" >> lines;
156 lex >> "placement" >> placement;
157 lex >> "overhang" >> overhang;
158 lex >> "width" >> width;
162 void InsetWrap::write(ostream & os) const
165 InsetCollapsable::write(os);
169 void InsetWrap::read(Lexer & lex)
172 InsetCollapsable::read(lex);
176 void InsetWrap::validate(LaTeXFeatures & features) const
178 features.require("wrapfig");
179 features.inFloat(true);
180 InsetCollapsable::validate(features);
181 features.inFloat(false);
185 int InsetWrap::latex(otexstream & os, OutputParams const & runparams_in) const
187 OutputParams runparams(runparams_in);
188 runparams.inFloat = OutputParams::MAINFLOAT;
189 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
190 // no optional argument when lines are zero
191 if (params_.lines != 0)
192 os << '[' << params_.lines << ']';
193 os << '{' << from_ascii(params_.placement) << '}';
194 Length over(params_.overhang);
195 // no optional argument when the value is zero
196 if (over.value() != 0)
197 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
198 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
199 int const i = InsetText::latex(os, runparams);
200 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
205 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
207 os << '[' << buffer().B_("wrap") << ' '
208 << floatName(params_.type) << ":\n";
209 InsetText::plaintext(os, runparams);
212 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
216 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
219 os << '<' << from_ascii(params_.type) << '>';
220 int const i = InsetText::docbook(os, runparams);
221 os << "</" << from_ascii(params_.type) << '>';
226 docstring InsetWrap::xhtml(XHTMLStream & xs, OutputParams const & rp) const
228 string const len = params_.width.asHTMLString();
229 string const width = len.empty() ? "50%" : len;
230 string const attr = "class='wrap' style='width: " + len + ";'";
231 xs << html::StartTag("div", attr);
232 docstring const deferred =
233 InsetText::insetAsXHTML(xs, rp, InsetText::WriteInnerTag);
235 xs << html::EndTag("div");
240 bool InsetWrap::insetAllowed(InsetCode code) const
248 return InsetCollapsable::insetAllowed(code);
253 bool InsetWrap::showInsetDialog(BufferView * bv) const
255 if (!InsetText::showInsetDialog(bv))
256 bv->showDialog("wrap", params2string(params()),
257 const_cast<InsetWrap *>(this));
262 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
264 params = InsetWrapParams();
265 istringstream data(in);
268 lex.setContext("InsetWrap::string2params");
270 lex >> "Wrap"; // Part of the inset proper, swallowed by Text::readInset
271 lex >> params.type; // We have to read the type here!
276 string InsetWrap::params2string(InsetWrapParams const & params)
279 data << "wrap" << ' ';