X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2Finsetwrap.C;h=570802c50d70b6371f71452d6cf531b577cd2b32;hb=e28331ed63062dea10d0a21b9ec12034b4b17b9a;hp=ae39aac22b5df21c5b6c355cc4e0af3140eed660;hpb=bc56bb9ffadbdf457153547851986cf867d22aa1;p=lyx.git diff --git a/src/insets/insetwrap.C b/src/insets/insetwrap.C index ae39aac22b..570802c50d 100644 --- a/src/insets/insetwrap.C +++ b/src/insets/insetwrap.C @@ -5,102 +5,157 @@ * * \author Dekel Tsur * - * Full author contact details are available in file CREDITS + * Full author contact details are available in file CREDITS. */ #include -#ifdef __GNUG__ -#pragma implementation -#endif - #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 "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; +#include "support/convert.h" + + +namespace lyx { + +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 += type; - 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); + 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); } @@ -112,120 +167,129 @@ 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: case FOOT_CODE: case MARGIN_CODE: - return false; + return false; default: return InsetCollapsable::insetAllowed(code); } } -int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset) - const -{ - if (owner() && - static_cast(owner())->getMaxWidth(bv, inset) < 0) { - return -1; - } - if (!width_.zero()) { - int ww1 = latexTextWidth(bv); - int 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), - bv->text->defaultHeight()); + 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"); + +InsetWrapMailer::InsetWrapMailer(InsetWrap & inset) + : inset_(inset) +{} -void InsetWrap::pageWidth(LyXLength const & ll) +string const InsetWrapMailer::inset2string(Buffer const &) const { - if (ll != width_) { - width_ = ll; - need_update = FULL; - } + return params2string(inset_.params()); } -void InsetWrap::placement(string const & p) +void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params) { - Placement_ = p; + 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); } -string const & InsetWrap::placement() const +string const InsetWrapMailer::params2string(InsetWrapParams const & params) { - return Placement_; + ostringstream data; + data << name_ << ' '; + params.write(data); + return data.str(); } -bool InsetWrap::showInsetDialog(BufferView * bv) const -{ - if (!inset.showInsetDialog(bv)) { - bv->owner()->getDialogs().showWrap(const_cast(this)); - } - return true; -} +} // namespace lyx