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/convert.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
38 #include "frontends/Application.h"
45 InsetWrap::InsetWrap(Buffer const & buf, string const & type)
46 : InsetCollapsable(buf)
48 setLabel(_("wrap: ") + floatName(type, buf.params()));
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::name() const
65 return "Wrap:" + from_utf8(params_.type);
69 docstring InsetWrap::toolTip(BufferView const & bv, int x, int y) const
71 OutputParams rp(&buffer().params().encoding());
72 docstring default_tip = InsetCollapsable::toolTip(bv, x, y);
73 docstring caption_tip = getCaptionText(rp);
74 if (!isOpen(bv) && !caption_tip.empty())
75 return caption_tip + '\n' + default_tip;
80 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
83 case LFUN_INSET_MODIFY: {
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::updateLabels(ParIterator const & it)
121 setLabel(_("wrap: ") + floatName(params_.type, buffer().params()));
123 buffer().masterBuffer()->params().documentClass().counters();
124 string const saveflt = cnts.current_float();
126 // Tell to captions what the current float is
127 cnts.current_float(params().type);
129 InsetCollapsable::updateLabels(it);
132 cnts.current_float(saveflt);
136 void InsetWrapParams::write(ostream & os) const
138 os << "Wrap " << type << '\n';
139 os << "lines " << lines << '\n';
140 os << "placement " << placement << '\n';
141 os << "overhang " << overhang.asString() << '\n';
142 os << "width \"" << width.asString() << "\"\n";
146 void InsetWrapParams::read(Lexer & lex)
148 lex.setContext("InsetWrapParams::read");
149 lex >> "lines" >> lines;
150 lex >> "placement" >> placement;
151 lex >> "overhang" >> overhang;
152 lex >> "width" >> width;
156 void InsetWrap::write(ostream & os) const
159 InsetCollapsable::write(os);
163 void InsetWrap::read(Lexer & lex)
166 InsetCollapsable::read(lex);
170 void InsetWrap::validate(LaTeXFeatures & features) const
172 features.require("wrapfig");
173 features.inFloat(true);
174 InsetCollapsable::validate(features);
175 features.inFloat(false);
179 docstring InsetWrap::editMessage() const
181 return _("Opened Wrap Inset");
185 int InsetWrap::latex(odocstream & 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, buffer().params()) << ":\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 bool InsetWrap::insetAllowed(InsetCode code) const
234 return InsetCollapsable::insetAllowed(code);
239 bool InsetWrap::showInsetDialog(BufferView * bv) const
241 if (!InsetText::showInsetDialog(bv))
242 bv->showDialog("wrap", params2string(params()),
243 const_cast<InsetWrap *>(this));
248 docstring InsetWrap::getCaptionText(OutputParams const & runparams) const
250 if (paragraphs().empty())
253 ParagraphList::const_iterator pit = paragraphs().begin();
254 for (; pit != paragraphs().end(); ++pit) {
255 InsetList::const_iterator it = pit->insetList().begin();
256 for (; it != pit->insetList().end(); ++it) {
257 Inset & inset = *it->inset;
258 if (inset.lyxCode() == CAPTION_CODE) {
259 odocstringstream ods;
261 static_cast<InsetCaption *>(it->inset);
262 ins->getCaptionText(ods, runparams);
271 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
273 params = InsetWrapParams();
274 istringstream data(in);
277 lex.setContext("InsetWrap::string2params");
279 lex >> "Wrap"; // Part of the inset proper, swallowed by Text::readInset
280 lex >> params.type; // We have to read the type here!
285 string InsetWrap::params2string(InsetWrapParams const & params)
288 data << "wrap" << ' ';