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"
45 InsetWrap::InsetWrap(Buffer * buf, string const & type)
46 : InsetCollapsable(buf)
48 setLabel(_("wrap: ") + floatName(type));
51 params_.placement = "o";
52 params_.overhang = Length(0, Length::PCW);
53 params_.width = Length(50, Length::PCW);
57 InsetWrap::~InsetWrap()
59 hideDialogs("wrap", this);
63 docstring InsetWrap::layoutName() const
65 return "Wrap:" + from_utf8(params_.type);
69 docstring InsetWrap::toolTip(BufferView const & bv, int x, int y) const
72 return InsetCollapsable::toolTip(bv, x, y);
73 OutputParams rp(&buffer().params().encoding());
74 docstring caption_tip = getCaptionText(rp);
75 if (!caption_tip.empty())
76 caption_tip += from_ascii("\n");
77 return toolTipText(caption_tip);
81 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
83 switch (cmd.action()) {
84 case LFUN_INSET_MODIFY: {
85 cur.recordUndoInset(ATOMIC_UNDO, this);
86 InsetWrapParams params;
87 InsetWrap::string2params(to_utf8(cmd.argument()), params);
88 params_.lines = params.lines;
89 params_.placement = params.placement;
90 params_.overhang = params.overhang;
91 params_.width = params.width;
95 case LFUN_INSET_DIALOG_UPDATE:
96 cur.bv().updateDialog("wrap", params2string(params()));
100 InsetCollapsable::doDispatch(cur, cmd);
106 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
107 FuncStatus & flag) const
109 switch (cmd.action()) {
110 case LFUN_INSET_MODIFY:
111 case LFUN_INSET_DIALOG_UPDATE:
112 flag.setEnabled(true);
116 return InsetCollapsable::getStatus(cur, cmd, flag);
121 void InsetWrap::updateBuffer(ParIterator const & it, UpdateType utype)
123 setLabel(_("wrap: ") + floatName(params_.type));
125 buffer().masterBuffer()->params().documentClass().counters();
126 if (utype == OutputUpdate) {
127 // counters are local to the wrap
128 cnts.saveLastCounter();
130 string const saveflt = cnts.current_float();
132 // Tell to captions what the current float is
133 cnts.current_float(params().type);
135 InsetCollapsable::updateBuffer(it, utype);
138 cnts.current_float(saveflt);
139 if (utype == OutputUpdate)
140 cnts.restoreLastCounter();
144 void InsetWrapParams::write(ostream & os) const
146 os << "Wrap " << type << '\n';
147 os << "lines " << lines << '\n';
148 os << "placement " << placement << '\n';
149 os << "overhang " << overhang.asString() << '\n';
150 os << "width \"" << width.asString() << "\"\n";
154 void InsetWrapParams::read(Lexer & lex)
156 lex.setContext("InsetWrapParams::read");
157 lex >> "lines" >> lines;
158 lex >> "placement" >> placement;
159 lex >> "overhang" >> overhang;
160 lex >> "width" >> width;
164 void InsetWrap::write(ostream & os) const
167 InsetCollapsable::write(os);
171 void InsetWrap::read(Lexer & lex)
174 InsetCollapsable::read(lex);
178 void InsetWrap::validate(LaTeXFeatures & features) const
180 features.require("wrapfig");
181 features.inFloat(true);
182 InsetCollapsable::validate(features);
183 features.inFloat(false);
187 void InsetWrap::latex(otexstream & os, OutputParams const & runparams_in) const
189 OutputParams runparams(runparams_in);
190 runparams.inFloat = OutputParams::MAINFLOAT;
191 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
192 // no optional argument when lines are zero
193 if (params_.lines != 0)
194 os << '[' << params_.lines << ']';
195 os << '{' << from_ascii(params_.placement) << '}';
196 Length over(params_.overhang);
197 // no optional argument when the value is zero
198 if (over.value() != 0)
199 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
200 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
201 InsetText::latex(os, runparams);
202 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
206 int InsetWrap::plaintext(odocstringstream & os,
207 OutputParams const & runparams, size_t max_length) const
209 os << '[' << buffer().B_("wrap") << ' '
210 << floatName(params_.type) << ":\n";
211 InsetText::plaintext(os, runparams, max_length);
214 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
218 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
221 os << '<' << from_ascii(params_.type) << '>';
222 int const i = InsetText::docbook(os, runparams);
223 os << "</" << from_ascii(params_.type) << '>';
228 docstring InsetWrap::xhtml(XHTMLStream & xs, OutputParams const & rp) const
230 string const len = params_.width.asHTMLString();
231 string const width = len.empty() ? "50%" : len;
232 InsetLayout const & il = getLayout();
233 string const tag = il.htmltag();
234 string const attr = il.htmlattr() + " style='width:" + width + ";'";
235 xs << html::StartTag(tag, attr);
236 docstring const deferred =
237 InsetText::insetAsXHTML(xs, rp, InsetText::WriteInnerTag);
238 xs << html::EndTag(tag);
243 bool InsetWrap::insetAllowed(InsetCode code) const
251 return InsetCollapsable::insetAllowed(code);
256 bool InsetWrap::showInsetDialog(BufferView * bv) const
258 if (!InsetText::showInsetDialog(bv))
259 bv->showDialog("wrap", params2string(params()),
260 const_cast<InsetWrap *>(this));
265 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
267 params = InsetWrapParams();
268 istringstream data(in);
271 lex.setContext("InsetWrap::string2params");
273 lex >> "Wrap"; // Part of the inset proper, swallowed by Text::readInset
274 lex >> params.type; // We have to read the type here!
279 string InsetWrap::params2string(InsetWrapParams const & params)
282 data << "wrap" << ' ';