From 8998dac1a7e910355ebf276d1fce6fd2a0ae1a75 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Fri, 7 Mar 2008 13:33:33 +0000 Subject: [PATCH] InsetInclude: use InsetLabel instead of handling "label" parameter manually. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23536 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/insets/InsetInclude.cpp | 72 +++++++++++++++++++++++++++---------- src/insets/InsetInclude.h | 4 +++ 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index c815f49d77..2a5297ab5f 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -41,8 +41,9 @@ #include "graphics/PreviewImage.h" #include "graphics/PreviewLoader.h" -#include "insets/RenderPreview.h" +#include "insets/InsetLabel.h" #include "insets/InsetListingsParams.h" +#include "insets/RenderPreview.h" #include "support/debug.h" #include "support/docstream.h" @@ -148,22 +149,39 @@ EmbeddedFile const includedFilename(Buffer const & buffer, return file; } +InsetLabel * createLabel(docstring const & label_str) +{ + if (label_str.empty()) + return 0; + InsetCommandParams icp(LABEL_CODE); + icp["name"] = label_str; + return new InsetLabel(icp); +} + } // namespace anon InsetInclude::InsetInclude(InsetCommandParams const & p) : InsetCommand(p, "include"), include_label(uniqueID()), - preview_(new RenderMonitoredPreview(this)), set_label_(false) + preview_(new RenderMonitoredPreview(this)), set_label_(false), label_(0) { preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this)); + + if (isListings(params())) { + InsetListingsParams listing_params(to_utf8(p["lstparams"])); + label_ = createLabel(from_utf8(listing_params.getParamValue("label"))); + } } InsetInclude::InsetInclude(InsetInclude const & other) : InsetCommand(other), include_label(other.include_label), - preview_(new RenderMonitoredPreview(this)), set_label_(false) + preview_(new RenderMonitoredPreview(this)), set_label_(false), label_(0) { preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this)); + + if (other.label_) + label_ = new InsetLabel(*other.label_); } @@ -194,6 +212,15 @@ InsetInclude::~InsetInclude() } +void InsetInclude::setBuffer(Buffer & buffer) +{ + buffer_ = &buffer; + if (label_) + label_->setBuffer(buffer); + +} + + ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */) { // FIXME @@ -225,15 +252,17 @@ void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd) InsetCommandMailer::string2params("include", to_utf8(cmd.argument()), p); if (!p.getCmdName().empty()) { if (isListings(p)){ - InsetListingsParams par_old(to_utf8(params()["lstparams"])); - InsetListingsParams par_new(to_utf8(p["lstparams"])); - if (par_old.getParamValue("label") != - par_new.getParamValue("label") - && !par_new.getParamValue("label").empty()) - cur.bv().buffer().changeRefsIfUnique( - from_utf8(par_old.getParamValue("label")), - from_utf8(par_new.getParamValue("label")), - REF_CODE); + InsetListingsParams new_params(to_utf8(p["lstparams"])); + docstring const label_str = from_utf8(new_params.getParamValue("label")); + if (label_str.empty()) + delete label_; + else if (label_) + label_->updateCommand(label_str); + else { + label_ = createLabel(label_str); + label_->setBuffer(buffer()); + label_->initView(); + } } try { // the embed parameter passed back from the dialog @@ -676,13 +705,12 @@ void InsetInclude::validate(LaTeXFeatures & features) const void InsetInclude::getLabelList(vector & list) const { - if (isListings(params())) { - InsetListingsParams p(to_utf8(params()["lstparams"])); - string label = p.getParamValue("label"); - if (!label.empty()) - list.push_back(from_utf8(label)); + if (isListings(params()) && label_) { + label_->getLabelList(list); + return; } - else if (loadIfNeeded(buffer(), params())) { + + if (loadIfNeeded(buffer(), params())) { string const included_file = includedFilename(buffer(), params()).absFilename(); Buffer * tmp = theBufferList().getBuffer(included_file); tmp->setParent(0); @@ -852,6 +880,9 @@ void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const void InsetInclude::addToToc(ParConstIterator const & cpit) const { if (isListings(params())) { + if (label_) + label_->addToToc(cpit); + InsetListingsParams p(to_utf8(params()["lstparams"])); string caption = p.getParamValue("caption"); if (caption.empty()) @@ -878,7 +909,7 @@ void InsetInclude::addToToc(ParConstIterator const & cpit) const } -void InsetInclude::updateLabels(ParIterator const &) +void InsetInclude::updateLabels(ParIterator const & it) { Buffer const * const childbuffer = getChildBuffer(buffer(), params()); if (childbuffer) { @@ -888,6 +919,9 @@ void InsetInclude::updateLabels(ParIterator const &) if (!isListings(params())) return; + if (label_) + label_->updateLabels(it); + InsetListingsParams const par(to_utf8(params()["lstparams"])); if (par.getParamValue("caption").empty()) { listings_label_.clear(); diff --git a/src/insets/InsetInclude.h b/src/insets/InsetInclude.h index 4159752cb3..a12c114b2e 100644 --- a/src/insets/InsetInclude.h +++ b/src/insets/InsetInclude.h @@ -27,6 +27,7 @@ namespace lyx { class Buffer; class Dimension; +class InsetLabel; class LaTeXFeatures; class RenderMonitoredPreview; @@ -37,6 +38,8 @@ public: InsetInclude(InsetCommandParams const &); ~InsetInclude(); + void setBuffer(Buffer & buffer); + /// Override these InsetButton methods if Previewing void metrics(MetricsInfo & mi, Dimension & dim) const; /// @@ -123,6 +126,7 @@ private: mutable bool set_label_; mutable RenderButton button_; mutable docstring listings_label_; + InsetLabel * label_; }; /// return loaded Buffer or zero if the file loading did not proceed. -- 2.39.2