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::updateLabels(ParIterator const & it)
119 setLabel(_("wrap: ") + floatName(params_.type));
121 buffer().masterBuffer()->params().documentClass().counters();
122 string const saveflt = cnts.current_float();
124 // Tell to captions what the current float is
125 cnts.current_float(params().type);
127 InsetCollapsable::updateLabels(it);
130 cnts.current_float(saveflt);
134 void InsetWrapParams::write(ostream & os) const
136 os << "Wrap " << type << '\n';
137 os << "lines " << lines << '\n';
138 os << "placement " << placement << '\n';
139 os << "overhang " << overhang.asString() << '\n';
140 os << "width \"" << width.asString() << "\"\n";
144 void InsetWrapParams::read(Lexer & lex)
146 lex.setContext("InsetWrapParams::read");
147 lex >> "lines" >> lines;
148 lex >> "placement" >> placement;
149 lex >> "overhang" >> overhang;
150 lex >> "width" >> width;
154 void InsetWrap::write(ostream & os) const
157 InsetCollapsable::write(os);
161 void InsetWrap::read(Lexer & lex)
164 InsetCollapsable::read(lex);
168 void InsetWrap::validate(LaTeXFeatures & features) const
170 features.require("wrapfig");
171 features.inFloat(true);
172 InsetCollapsable::validate(features);
173 features.inFloat(false);
177 int InsetWrap::latex(odocstream & os, OutputParams const & runparams_in) const
179 OutputParams runparams(runparams_in);
180 runparams.inFloat = OutputParams::MAINFLOAT;
181 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
182 // no optional argument when lines are zero
183 if (params_.lines != 0)
184 os << '[' << params_.lines << ']';
185 os << '{' << from_ascii(params_.placement) << '}';
186 Length over(params_.overhang);
187 // no optional argument when the value is zero
188 if (over.value() != 0)
189 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
190 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
191 int const i = InsetText::latex(os, runparams);
192 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
197 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
199 os << '[' << buffer().B_("wrap") << ' '
200 << floatName(params_.type) << ":\n";
201 InsetText::plaintext(os, runparams);
204 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
208 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
211 os << '<' << from_ascii(params_.type) << '>';
212 int const i = InsetText::docbook(os, runparams);
213 os << "</" << from_ascii(params_.type) << '>';
218 docstring InsetWrap::xhtml(XHTMLStream & xs, OutputParams const & rp) const
220 string const len = params_.width.asHTMLString();
221 string const width = len.empty() ? "50%" : len;
222 string const attr = "class='wrap' style='width: " + len + ";'";
223 xs << StartTag("div", attr);
224 docstring const deferred =
225 InsetText::insetAsXHTML(xs, rp, InsetText::WriteInnerTag);
232 bool InsetWrap::insetAllowed(InsetCode code) const
240 return InsetCollapsable::insetAllowed(code);
245 bool InsetWrap::showInsetDialog(BufferView * bv) const
247 if (!InsetText::showInsetDialog(bv))
248 bv->showDialog("wrap", params2string(params()),
249 const_cast<InsetWrap *>(this));
254 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
256 params = InsetWrapParams();
257 istringstream data(in);
260 lex.setContext("InsetWrap::string2params");
262 lex >> "Wrap"; // Part of the inset proper, swallowed by Text::readInset
263 lex >> params.type; // We have to read the type here!
268 string InsetWrap::params2string(InsetWrapParams const & params)
271 data << "wrap" << ' ';