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 #pragma implementation
17 #include "insetwrap.h"
20 #include "BufferView.h"
22 #include "insets/insettext.h"
23 #include "support/LOstream.h"
24 #include "support/lstrings.h"
25 #include "LaTeXFeatures.h"
28 #include "frontends/LyXView.h"
29 #include "frontends/Dialogs.h"
31 #include "FloatList.h"
38 // this should not be hardcoded, but be part of the definition
39 // of the float (JMarc)
40 string const caplayout("Caption");
41 string floatname(string const & type, BufferParams const & bp)
43 FloatList const & floats = bp.getLyXTextClass().floats();
44 FloatList::const_iterator it = floats[type];
45 if (it == floats.end())
48 return _(it->second.name());
54 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
55 : InsetCollapsable(bp), width_(50, LyXLength::PCW)
57 string lab(_("wrap: "));
58 lab += floatname(type, bp);
60 LyXFont font(LyXFont::ALL_SANE);
63 font.setColor(LColor::collapsable);
67 LyXTextClass const & tclass = bp.getLyXTextClass();
68 if (tclass.hasLayout(caplayout))
69 inset.paragraph()->layout(tclass[caplayout]);
73 InsetWrap::InsetWrap(InsetWrap const & in, bool same_id)
74 : InsetCollapsable(in, same_id), Type_(in.Type_),
75 Placement_(in.Placement_), width_(in.width_)
79 InsetWrap::~InsetWrap()
85 void InsetWrap::write(Buffer const * buf, ostream & os) const
87 os << "Wrap " // getInsetName()
90 if (!Placement_.empty()) {
91 os << "placement " << Placement_ << "\n";
93 os << "width \"" << width_.asString() << "\"\n";
95 InsetCollapsable::write(buf, os);
99 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
103 string token = lex.getString();
104 if (token == "placement") {
106 Placement_ = lex.getString();
108 // take countermeasures
109 lex.pushToken(token);
114 string token = lex.getString();
115 if (token == "width") {
117 width_ = LyXLength(lex.getString());
119 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
121 // take countermeasures
122 lex.pushToken(token);
125 InsetCollapsable::read(buf, lex);
129 void InsetWrap::validate(LaTeXFeatures & features) const
131 features.require("floatflt");
132 InsetCollapsable::validate(features);
136 Inset * InsetWrap::clone(Buffer const &, bool same_id) const
138 return new InsetWrap(*const_cast<InsetWrap *>(this), same_id);
142 string const InsetWrap::editMessage() const
144 return _("Opened Wrap Inset");
148 int InsetWrap::latex(Buffer const * buf,
149 ostream & os, bool fragile, bool fp) const
151 os << "\\begin{floating" << Type_ << "}";
152 if (!Placement_.empty()) {
153 os << "[" << Placement_ << "]";
155 os << "{" << width_.asLatexString() << "}%\n";
157 int const i = inset.latex(buf, os, fragile, fp);
159 os << "\\end{floating" << Type_ << "}%\n";
164 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
166 os << "<" << Type_ << ">";
167 int const i = inset.docbook(buf, os, mixcont);
168 os << "</" << Type_ << ">";
174 bool InsetWrap::insetAllowed(Inset::Code code) const
182 return InsetCollapsable::insetAllowed(code);
186 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
190 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
193 if (!width_.zero()) {
194 int ww1 = latexTextWidth(bv);
195 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
196 if (ww2 > 0 && ww2 < ww1) {
201 // this should not happen!
202 return InsetCollapsable::getMaxWidth(bv, inset);
206 int InsetWrap::latexTextWidth(BufferView * bv) const
208 return width_.inPixels(InsetCollapsable::latexTextWidth(bv),
209 bv->text->defaultHeight());
213 string const & InsetWrap::type() const
219 LyXLength const & InsetWrap::pageWidth() const
225 void InsetWrap::pageWidth(LyXLength const & ll)
234 void InsetWrap::placement(string const & p)
240 string const & InsetWrap::placement() const
246 bool InsetWrap::showInsetDialog(BufferView * bv) const
248 if (!inset.showInsetDialog(bv)) {
249 bv->owner()->getDialogs().showWrap(const_cast<InsetWrap *>(this));
255 void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
257 // Now find the caption in the float...
258 // We now tranverse the paragraphs of
260 Paragraph * tmp = inset.paragraph();
262 if (tmp->layout()->name() == caplayout) {
264 tostr(toclist[type()].size() + 1)
265 + ". " + tmp->asString(buf, false);
266 toc::TocItem const item(tmp, 0 , str);
267 toclist[type()].push_back(item);