X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2Finsetwrap.C;h=570802c50d70b6371f71452d6cf531b577cd2b32;hb=e28331ed63062dea10d0a21b9ec12034b4b17b9a;hp=b48a1327e4b00efffbb8f27853e940d2163275a1;hpb=f413754ef5b053a1f42377175e7075b36a15eafe;p=lyx.git diff --git a/src/insets/insetwrap.C b/src/insets/insetwrap.C index b48a1327e4..570802c50d 100644 --- a/src/insets/insetwrap.C +++ b/src/insets/insetwrap.C @@ -5,120 +5,157 @@ * * \author Dekel Tsur * - * Full author contact details are available in file CREDITS + * Full author contact details are available in file CREDITS. */ #include - #include "insetwrap.h" -#include "gettext.h" -#include "lyxfont.h" + +#include "buffer.h" +#include "bufferparams.h" #include "BufferView.h" -#include "lyxtext.h" -#include "insets/insettext.h" -#include "support/LOstream.h" -#include "support/lstrings.h" -#include "LaTeXFeatures.h" +#include "cursor.h" #include "debug.h" -#include "buffer.h" -#include "frontends/LyXView.h" -#include "frontends/Dialogs.h" -#include "lyxlex.h" +#include "dispatchresult.h" +#include "Floating.h" #include "FloatList.h" +#include "funcrequest.h" +#include "FuncStatus.h" +#include "gettext.h" +#include "LaTeXFeatures.h" +#include "LColor.h" +#include "lyxlex.h" +#include "outputparams.h" +#include "paragraph.h" +#include "TocBackend.h" -using std::ostream; -using std::endl; - -namespace { +#include "support/convert.h" -// this should not be hardcoded, but be part of the definition -// of the float (JMarc) -string const caplayout("Caption"); -string floatname(string const & type, BufferParams const & bp) -{ - FloatList const & floats = bp.getLyXTextClass().floats(); - FloatList::const_iterator it = floats[type]; - if (it == floats.end()) - return type; - return _(it->second.name()); -} +namespace lyx { -} // namespace anon +using std::string; +using std::endl; +using std::auto_ptr; +using std::istringstream; +using std::ostream; +using std::ostringstream; InsetWrap::InsetWrap(BufferParams const & bp, string const & type) - : InsetCollapsable(bp), width_(50, LyXLength::PCW) + : InsetCollapsable(bp) { - string lab(_("wrap: ")); - lab += floatname(type, bp); - setLabel(lab); + setLabel(_("wrap: ") + floatName(type, bp)); LyXFont font(LyXFont::ALL_SANE); font.decSize(); font.decSize(); font.setColor(LColor::collapsable); setLabelFont(font); - Type_ = type; - setInsetName(type); - LyXTextClass const & tclass = bp.getLyXTextClass(); - if (tclass.hasLayout(caplayout)) - inset.paragraph()->layout(tclass[caplayout]); + params_.type = type; + params_.width = LyXLength(50, LyXLength::PCW); + setInsetName(from_utf8(type)); } -InsetWrap::InsetWrap(InsetWrap const & in, bool same_id) - : InsetCollapsable(in, same_id), Type_(in.Type_), - Placement_(in.Placement_), width_(in.width_) -{} +InsetWrap::~InsetWrap() +{ + InsetWrapMailer(*this).hideDialog(); +} -InsetWrap::~InsetWrap() +void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd) { - hideDialog(); + switch (cmd.action) { + case LFUN_INSET_MODIFY: { + InsetWrapParams params; + InsetWrapMailer::string2params(to_utf8(cmd.argument()), params); + params_.placement = params.placement; + params_.width = params.width; + break; + } + + case LFUN_INSET_DIALOG_UPDATE: + InsetWrapMailer(*this).updateDialog(&cur.bv()); + break; + + case LFUN_MOUSE_RELEASE: { + if (cmd.button() == mouse_button::button3 && hitButton(cmd)) { + InsetWrapMailer(*this).showDialog(&cur.bv()); + break; + } + InsetCollapsable::doDispatch(cur, cmd); + break; + } + + default: + InsetCollapsable::doDispatch(cur, cmd); + break; + } } -void InsetWrap::write(Buffer const * buf, ostream & os) const +bool InsetWrap::getStatus(LCursor & cur, FuncRequest const & cmd, + FuncStatus & flag) const { - os << "Wrap " // getInsetName() - << Type_ << '\n'; + switch (cmd.action) { + case LFUN_INSET_MODIFY: + case LFUN_INSET_DIALOG_UPDATE: + flag.enabled(true); + return true; - if (!Placement_.empty()) { - os << "placement " << Placement_ << "\n"; + default: + return InsetCollapsable::getStatus(cur, cmd, flag); } - os << "width \"" << width_.asString() << "\"\n"; +} - InsetCollapsable::write(buf, os); + +void InsetWrapParams::write(ostream & os) const +{ + os << "Wrap " << type << '\n'; + + if (!placement.empty()) + os << "placement " << placement << "\n"; + + os << "width \"" << width.asString() << "\"\n"; } -void InsetWrap::read(Buffer const * buf, LyXLex & lex) +void InsetWrapParams::read(LyXLex & lex) { - if (lex.isOK()) { - lex.next(); - string token = lex.getString(); - if (token == "placement") { - lex.next(); - Placement_ = lex.getString(); - } else { - // take countermeasures - lex.pushToken(token); - } + string token; + lex >> token; + if (token == "placement") + lex >> placement; + else { + // take countermeasures + lex.pushToken(token); } - if (lex.isOK()) { + if (!lex) + return; + lex >> token; + if (token == "width") { lex.next(); - string token = lex.getString(); - if (token == "width") { - lex.next(); - width_ = LyXLength(lex.getString()); - } else { - lyxerr << "InsetWrap::Read:: Missing 'width'-tag!" - << endl; - // take countermeasures - lex.pushToken(token); - } + width = LyXLength(lex.getString()); + } else { + lyxerr << "InsetWrap::Read:: Missing 'width'-tag!" + << endl; + // take countermeasures + lex.pushToken(token); } +} + + +void InsetWrap::write(Buffer const & buf, ostream & os) const +{ + params_.write(os); + InsetCollapsable::write(buf, os); +} + + +void InsetWrap::read(Buffer const & buf, LyXLex & lex) +{ + params_.read(lex); InsetCollapsable::read(buf, lex); } @@ -130,45 +167,43 @@ void InsetWrap::validate(LaTeXFeatures & features) const } -Inset * InsetWrap::clone(Buffer const &, bool same_id) const +auto_ptr InsetWrap::doClone() const { - return new InsetWrap(*const_cast(this), same_id); + return auto_ptr(new InsetWrap(*this)); } -string const InsetWrap::editMessage() const +docstring const InsetWrap::editMessage() const { return _("Opened Wrap Inset"); } -int InsetWrap::latex(Buffer const * buf, - ostream & os, bool fragile, bool fp) const +int InsetWrap::latex(Buffer const & buf, odocstream & os, + OutputParams const & runparams) const { - os << "\\begin{floating" << Type_ << '}'; - if (!Placement_.empty()) { - os << '[' << Placement_ << ']'; - } - os << '{' << width_.asLatexString() << "}%\n"; - - int const i = inset.latex(buf, os, fragile, fp); - - os << "\\end{floating" << Type_ << "}%\n"; + os << "\\begin{floating" << from_ascii(params_.type) << '}'; + if (!params_.placement.empty()) + os << '[' << from_ascii(params_.placement) << ']'; + os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n"; + int const i = InsetText::latex(buf, os, runparams); + os << "\\end{floating" << from_ascii(params_.type) << "}%\n"; return i + 2; } -int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const +int InsetWrap::docbook(Buffer const & buf, odocstream & os, + OutputParams const & runparams) const { - os << '<' << Type_ << '>'; - int const i = inset.docbook(buf, os, mixcont); - os << "'; - + // FIXME UNICODE + os << '<' << from_ascii(params_.type) << '>'; + int const i = InsetText::docbook(buf, os, runparams); + os << "'; return i; } -bool InsetWrap::insetAllowed(Inset::Code code) const +bool InsetWrap::insetAllowed(InsetBase::Code code) const { switch(code) { case FLOAT_CODE: @@ -181,89 +216,80 @@ bool InsetWrap::insetAllowed(Inset::Code code) const } -int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset) - const -{ - if (owner() && - static_cast(owner())->getMaxWidth(bv, inset) < 0) { - return -1; - } - if (!width_.zero()) { - int const ww1 = latexTextWidth(bv); - int const ww2 = InsetCollapsable::getMaxWidth(bv, inset); - if (ww2 > 0 && ww2 < ww1) { - return ww2; - } - return ww1; - } - // this should not happen! - return InsetCollapsable::getMaxWidth(bv, inset); -} - - -int InsetWrap::latexTextWidth(BufferView * bv) const +bool InsetWrap::showInsetDialog(BufferView * bv) const { - return width_.inPixels(InsetCollapsable::latexTextWidth(bv)); + if (!InsetText::showInsetDialog(bv)) + InsetWrapMailer(const_cast(*this)).showDialog(bv); + return true; } -string const & InsetWrap::type() const +void InsetWrap::addToToc(TocList & toclist, Buffer const & buf) const { - return Type_; + ParConstIterator pit = par_const_iterator_begin(*this); + ParConstIterator end = par_const_iterator_end(*this); + + // Find a caption layout in one of the (child inset's) pars + for (; pit != end; ++pit) { + if (pit->layout()->labeltype == LABEL_SENSITIVE) { + Toc & toc = toclist[params_.type]; + docstring const str = + convert(toc.size() + 1) + + ". " + pit->asString(buf, false); + toc.push_back(TocItem(pit, 0, str)); + } + } } -LyXLength const & InsetWrap::pageWidth() const -{ - return width_; -} - +string const InsetWrapMailer::name_("wrap"); -void InsetWrap::pageWidth(LyXLength const & ll) -{ - if (ll != width_) { - width_ = ll; - need_update = FULL; - } -} +InsetWrapMailer::InsetWrapMailer(InsetWrap & inset) + : inset_(inset) +{} -void InsetWrap::placement(string const & p) +string const InsetWrapMailer::inset2string(Buffer const &) const { - Placement_ = p; + return params2string(inset_.params()); } -string const & InsetWrap::placement() const +void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params) { - return Placement_; + params = InsetWrapParams(); + if (in.empty()) + return; + + istringstream data(in); + LyXLex lex(0,0); + lex.setStream(data); + + string name; + lex >> name; + if (!lex || name != name_) + return print_mailer_error("InsetWrapMailer", in, 1, name_); + + // This is part of the inset proper that is usually swallowed + // by LyXText::readInset + string id; + lex >> id; + if (!lex || id != "Wrap") + return print_mailer_error("InsetBoxMailer", in, 2, "Wrap"); + + // We have to read the type here! + lex >> params.type; + params.read(lex); } -bool InsetWrap::showInsetDialog(BufferView * bv) const +string const InsetWrapMailer::params2string(InsetWrapParams const & params) { - if (!inset.showInsetDialog(bv)) { - bv->owner()->getDialogs().showWrap(const_cast(this)); - } - return true; + ostringstream data; + data << name_ << ' '; + params.write(data); + return data.str(); } -void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const -{ - // Now find the caption in the float... - // We now tranverse the paragraphs of - // the inset... - Paragraph * tmp = inset.paragraph(); - while (tmp) { - if (tmp->layout()->name() == caplayout) { - string const name = floatname(type(), buf->params); - string const str = - tostr(toclist[name].size() + 1) - + ". " + tmp->asString(buf, false); - toc::TocItem const item(tmp->id(), 0 , str); - toclist[name].push_back(item); - } - tmp = tmp->next(); - } -} +} // namespace lyx