3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
14 #include "insetwrap.h"
17 #include "BufferView.h"
19 #include "insets/insettext.h"
20 #include "support/LOstream.h"
21 #include "support/lstrings.h"
22 #include "LaTeXFeatures.h"
25 #include "frontends/LyXView.h"
26 #include "frontends/Dialogs.h"
28 #include "FloatList.h"
35 // this should not be hardcoded, but be part of the definition
36 // of the float (JMarc)
37 string const caplayout("Caption");
38 string floatname(string const & type, BufferParams const & bp)
40 FloatList const & floats = bp.getLyXTextClass().floats();
41 FloatList::const_iterator it = floats[type];
42 if (it == floats.end())
45 return _(it->second.name());
51 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
52 : InsetCollapsable(bp), width_(50, LyXLength::PCW)
54 string lab(_("wrap: "));
55 lab += floatname(type, bp);
57 LyXFont font(LyXFont::ALL_SANE);
60 font.setColor(LColor::collapsable);
64 LyXTextClass const & tclass = bp.getLyXTextClass();
65 if (tclass.hasLayout(caplayout))
66 inset.paragraph()->layout(tclass[caplayout]);
70 InsetWrap::InsetWrap(InsetWrap const & in, bool same_id)
71 : InsetCollapsable(in, same_id), Type_(in.Type_),
72 Placement_(in.Placement_), width_(in.width_)
76 InsetWrap::~InsetWrap()
82 void InsetWrap::write(Buffer const * buf, ostream & os) const
84 os << "Wrap " // getInsetName()
87 if (!Placement_.empty()) {
88 os << "placement " << Placement_ << "\n";
90 os << "width \"" << width_.asString() << "\"\n";
92 InsetCollapsable::write(buf, os);
96 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
100 string token = lex.getString();
101 if (token == "placement") {
103 Placement_ = lex.getString();
105 // take countermeasures
106 lex.pushToken(token);
111 string token = lex.getString();
112 if (token == "width") {
114 width_ = LyXLength(lex.getString());
116 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
118 // take countermeasures
119 lex.pushToken(token);
122 InsetCollapsable::read(buf, lex);
126 void InsetWrap::validate(LaTeXFeatures & features) const
128 features.require("floatflt");
129 InsetCollapsable::validate(features);
133 Inset * InsetWrap::clone(Buffer const &, bool same_id) const
135 return new InsetWrap(*const_cast<InsetWrap *>(this), same_id);
139 string const InsetWrap::editMessage() const
141 return _("Opened Wrap Inset");
145 int InsetWrap::latex(Buffer const * buf,
146 ostream & os, bool fragile, bool fp) const
148 os << "\\begin{floating" << Type_ << '}';
149 if (!Placement_.empty()) {
150 os << '[' << Placement_ << ']';
152 os << '{' << width_.asLatexString() << "}%\n";
154 int const i = inset.latex(buf, os, fragile, fp);
156 os << "\\end{floating" << Type_ << "}%\n";
161 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
163 os << '<' << Type_ << '>';
164 int const i = inset.docbook(buf, os, mixcont);
165 os << "</" << Type_ << '>';
171 bool InsetWrap::insetAllowed(Inset::Code code) const
179 return InsetCollapsable::insetAllowed(code);
184 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
188 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
191 if (!width_.zero()) {
192 int const ww1 = latexTextWidth(bv);
193 int const ww2 = InsetCollapsable::getMaxWidth(bv, inset);
194 if (ww2 > 0 && ww2 < ww1) {
199 // this should not happen!
200 return InsetCollapsable::getMaxWidth(bv, inset);
204 int InsetWrap::latexTextWidth(BufferView * bv) const
206 return width_.inPixels(InsetCollapsable::latexTextWidth(bv));
210 string const & InsetWrap::type() const
216 LyXLength const & InsetWrap::pageWidth() const
222 void InsetWrap::pageWidth(LyXLength const & ll)
231 void InsetWrap::placement(string const & p)
237 string const & InsetWrap::placement() const
243 bool InsetWrap::showInsetDialog(BufferView * bv) const
245 if (!inset.showInsetDialog(bv)) {
246 bv->owner()->getDialogs().showWrap(const_cast<InsetWrap *>(this));
252 void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
254 // Now find the caption in the float...
255 // We now tranverse the paragraphs of
257 Paragraph * tmp = inset.paragraph();
259 if (tmp->layout()->name() == caplayout) {
260 string const name = floatname(type(), buf->params);
262 tostr(toclist[name].size() + 1)
263 + ". " + tmp->asString(buf, false);
264 toc::TocItem const item(tmp->id(), 0 , str);
265 toclist[name].push_back(item);