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 * buf, string const & type)
43 : InsetCollapsable(buf)
45 setLabel(_("wrap: ") + floatName(type));
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));
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 int InsetWrap::latex(odocstream & os, OutputParams const & runparams_in) const
178 OutputParams runparams(runparams_in);
179 runparams.inFloat = OutputParams::MAINFLOAT;
180 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
181 // no optional argument when lines are zero
182 if (params_.lines != 0)
183 os << '[' << params_.lines << ']';
184 os << '{' << from_ascii(params_.placement) << '}';
185 Length over(params_.overhang);
186 // no optional argument when the value is zero
187 if (over.value() != 0)
188 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
189 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
190 int const i = InsetText::latex(os, runparams);
191 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
196 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
198 os << '[' << buffer().B_("wrap") << ' '
199 << floatName(params_.type) << ":\n";
200 InsetText::plaintext(os, runparams);
203 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
207 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
210 os << '<' << from_ascii(params_.type) << '>';
211 int const i = InsetText::docbook(os, runparams);
212 os << "</" << from_ascii(params_.type) << '>';
217 docstring InsetWrap::xhtml(odocstream &, OutputParams const & rp) const
219 string const len = params_.width.asHTMLString();
220 docstring retval = from_ascii("<div class='wrap'");
222 retval += from_ascii(" style='width: " + len + ";");
223 retval += from_ascii("'>");
225 docstring const deferred = InsetText::xhtml(os, rp);
226 retval += os.str() + from_ascii("</div>");
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" << ' ';