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 "InsetList.h"
28 #include "LaTeXFeatures.h"
30 #include "TextClass.h"
31 #include "TocBackend.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
37 #include "frontends/Application.h"
44 InsetWrap::InsetWrap(Buffer const & buf, string const & type)
45 : InsetCollapsable(buf)
47 setLabel(_("wrap: ") + floatName(type, buf.params()));
50 params_.placement = "o";
51 params_.overhang = Length(0, Length::PCW);
52 params_.width = Length(50, Length::PCW);
56 InsetWrap::~InsetWrap()
58 hideDialogs("wrap", this);
62 docstring InsetWrap::name() const
64 return "Wrap:" + from_utf8(params_.type);
68 docstring InsetWrap::toolTip(BufferView const & bv, int x, int y) const
70 OutputParams rp(&buffer().params().encoding());
71 docstring default_tip = InsetCollapsable::toolTip(bv, x, y);
72 docstring caption_tip = getCaptionText(rp);
73 if (!isOpen(bv) && !caption_tip.empty())
74 return caption_tip + '\n' + default_tip;
79 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
82 case LFUN_INSET_MODIFY: {
83 InsetWrapParams params;
84 InsetWrap::string2params(to_utf8(cmd.argument()), params);
85 params_.lines = params.lines;
86 params_.placement = params.placement;
87 params_.overhang = params.overhang;
88 params_.width = params.width;
92 case LFUN_INSET_DIALOG_UPDATE:
93 cur.bv().updateDialog("wrap", params2string(params()));
97 InsetCollapsable::doDispatch(cur, cmd);
103 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
104 FuncStatus & flag) const
106 switch (cmd.action) {
107 case LFUN_INSET_MODIFY:
108 case LFUN_INSET_DIALOG_UPDATE:
109 flag.setEnabled(true);
113 return InsetCollapsable::getStatus(cur, cmd, flag);
118 void InsetWrap::updateLabels(ParIterator const & it)
120 setLabel(_("wrap: ") + floatName(params_.type, buffer().params()));
122 buffer().masterBuffer()->params().documentClass().counters();
123 string const saveflt = cnts.current_float();
125 // Tell to captions what the current float is
126 cnts.current_float(params().type);
128 InsetCollapsable::updateLabels(it);
131 cnts.current_float(saveflt);
135 void InsetWrapParams::write(ostream & os) const
137 os << "Wrap " << type << '\n';
138 os << "lines " << lines << '\n';
139 os << "placement " << placement << '\n';
140 os << "overhang " << overhang.asString() << '\n';
141 os << "width \"" << width.asString() << "\"\n";
145 void InsetWrapParams::read(Lexer & lex)
147 lex.setContext("InsetWrapParams::read");
148 lex >> "lines" >> lines;
149 lex >> "placement" >> placement;
150 lex >> "overhang" >> overhang;
151 lex >> "width" >> width;
155 void InsetWrap::write(ostream & os) const
158 InsetCollapsable::write(os);
162 void InsetWrap::read(Lexer & lex)
165 InsetCollapsable::read(lex);
169 void InsetWrap::validate(LaTeXFeatures & features) const
171 features.require("wrapfig");
172 features.inFloat(true);
173 InsetCollapsable::validate(features);
174 features.inFloat(false);
178 docstring InsetWrap::editMessage() const
180 return _("Opened Wrap Inset");
184 int InsetWrap::latex(odocstream & os, OutputParams const & runparams_in) const
186 OutputParams runparams(runparams_in);
187 runparams.inFloat = OutputParams::MAINFLOAT;
188 os << "\\begin{wrap" << from_ascii(params_.type) << '}';
189 // no optional argument when lines are zero
190 if (params_.lines != 0)
191 os << '[' << params_.lines << ']';
192 os << '{' << from_ascii(params_.placement) << '}';
193 Length over(params_.overhang);
194 // no optional argument when the value is zero
195 if (over.value() != 0)
196 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
197 os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
198 int const i = InsetText::latex(os, runparams);
199 os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
204 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
206 os << '[' << buffer().B_("wrap") << ' '
207 << floatName(params_.type, buffer().params()) << ":\n";
208 InsetText::plaintext(os, runparams);
211 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
215 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
218 os << '<' << from_ascii(params_.type) << '>';
219 int const i = InsetText::docbook(os, runparams);
220 os << "</" << from_ascii(params_.type) << '>';
225 docstring InsetWrap::xhtml(odocstream &, OutputParams const & rp) const
227 string const len = params_.width.asHTMLString();
228 docstring retval = from_ascii("<div class='wrap'");
230 retval += from_ascii(" style='width: " + len + ";");
231 retval += from_ascii("'>");
233 docstring const deferred = InsetText::xhtml(os, rp);
234 retval += os.str() + from_ascii("</div>");
239 bool InsetWrap::insetAllowed(InsetCode code) const
247 return InsetCollapsable::insetAllowed(code);
252 bool InsetWrap::showInsetDialog(BufferView * bv) const
254 if (!InsetText::showInsetDialog(bv))
255 bv->showDialog("wrap", params2string(params()),
256 const_cast<InsetWrap *>(this));
261 docstring InsetWrap::getCaptionText(OutputParams const & runparams) const
263 if (paragraphs().empty())
266 ParagraphList::const_iterator pit = paragraphs().begin();
267 for (; pit != paragraphs().end(); ++pit) {
268 InsetList::const_iterator it = pit->insetList().begin();
269 for (; it != pit->insetList().end(); ++it) {
270 Inset & inset = *it->inset;
271 if (inset.lyxCode() == CAPTION_CODE) {
272 odocstringstream ods;
274 static_cast<InsetCaption *>(it->inset);
275 ins->getCaptionText(ods, runparams);
284 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
286 params = InsetWrapParams();
287 istringstream data(in);
290 lex.setContext("InsetWrap::string2params");
292 lex >> "Wrap"; // Part of the inset proper, swallowed by Text::readInset
293 lex >> params.type; // We have to read the type here!
298 string InsetWrap::params2string(InsetWrapParams const & params)
301 data << "wrap" << ' ';