]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetLabel.cpp
Introduce a labels&references cache at buffer level. This cache uses the already...
[features.git] / src / insets / InsetLabel.cpp
index 2a6663561bf91e42a576e5387681ed52b2b38c7c..c23cee2b06ae45f32efee58f2d94284b2676a9d1 100644 (file)
 
 #include "InsetLabel.h"
 
+#include "InsetRef.h"
+
 #include "Buffer.h"
 #include "BufferView.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
-#include "InsetList.h"
-#include "Text.h"
-#include "Paragraph.h"
+#include "InsetIterator.h"
 #include "ParIterator.h"
 #include "sgml.h"
+#include "Text.h"
+#include "TocBackend.h"
+
+#include "frontends/alert.h"
 
-#include "support/lstrings.h"
 #include "support/lyxalgo.h"
-#include "support/std_ostream.h"
+#include "support/gettext.h"
+#include "support/lstrings.h"
 
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
-using support::escape;
-
-using std::string;
-using std::ostream;
-using std::vector;
-
 
 InsetLabel::InsetLabel(InsetCommandParams const & p)
        : InsetCommand(p, "label")
 {}
 
 
-std::auto_ptr<Inset> InsetLabel::doClone() const
+ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
 {
-       return std::auto_ptr<Inset>(new InsetLabel(params()));
+       static ParamInfo param_info_;
+       if (param_info_.empty()) {
+               param_info_.add("name", ParamInfo::LATEX_REQUIRED);
+       }
+       return param_info_;
 }
 
 
-void InsetLabel::getLabelList(Buffer const &, std::vector<docstring> & list) const
+void InsetLabel::getLabelList(vector<docstring> & list) const
 {
        list.push_back(getParam("name"));
 }
 
 
-docstring const InsetLabel::getScreenLabel(Buffer const &) const
+docstring InsetLabel::screenLabel() const
 {
        return getParam("name");
 }
 
 
+void InsetLabel::updateLabels(ParIterator const & it)
+{
+       docstring const & label = getParam("name");
+       if (buffer().insetLabel(label))
+               // Problem: We already have an InsetLabel with the same name!
+               return;
+       buffer().setInsetLabel(label, this);
+}
+
+
+void InsetLabel::addToToc(ParConstIterator const & cpit) const
+{
+       docstring const & label = getParam("name");
+       Toc & toc = buffer().tocBackend().toc("label");
+       if (buffer().insetLabel(label) != this) {
+               toc.push_back(TocItem(cpit, 0, _("DUPLICATE: ") + label));
+               return;
+       }
+       toc.push_back(TocItem(cpit, 0, label));
+       Buffer::References const & refs = buffer().references(label);
+       Buffer::References::const_iterator it = refs.begin();
+       Buffer::References::const_iterator end = refs.end();
+       for (; it != end; ++it) {
+               ParConstIterator const ref_pit(it->second);
+               toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
+       }
+}
+
+
 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
 
        case LFUN_INSET_MODIFY: {
-               InsetCommandParams p("label");
+               InsetCommandParams p(LABEL_CODE);
                // FIXME UNICODE
                InsetCommandMailer::string2params("label", to_utf8(cmd.argument()), p);
                if (p.getCmdName().empty()) {
@@ -72,8 +105,8 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
                        break;
                }
                if (p["name"] != params()["name"])
-                       cur.bv().buffer()->changeRefsIfUnique(params()["name"],
-                                       p["name"], Inset::REF_CODE);
+                       cur.bv().buffer().changeRefsIfUnique(params()["name"],
+                                       p["name"], REF_CODE);
                setParams(p);
                break;
        }
@@ -85,16 +118,14 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
 }
 
 
-int InsetLabel::latex(Buffer const &, odocstream & os,
-                      OutputParams const &) const
+int InsetLabel::latex(odocstream & os, OutputParams const &) const
 {
        os << escape(getCommand());
        return 0;
 }
 
 
-int InsetLabel::plaintext(Buffer const &, odocstream & os,
-                          OutputParams const &) const
+int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
 {
        docstring const str = getParam("name");
        os << '<' << str << '>';
@@ -102,12 +133,11 @@ int InsetLabel::plaintext(Buffer const &, odocstream & os,
 }
 
 
-int InsetLabel::docbook(Buffer const & buf, odocstream & os,
-                        OutputParams const & runparams) const
+int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
 {
        os << "<!-- anchor id=\""
-           << sgml::cleanID(buf, runparams, getParam("name"))
-           << "\" -->";
+          << sgml::cleanID(buffer(), runparams, getParam("name"))
+          << "\" -->";
        return 0;
 }