]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetBibitem.cpp
Fix GRAPHICS_EDIT of InsetGraphics
[lyx.git] / src / insets / InsetBibitem.cpp
index 576cece165b49120c0dbef4c0374c4c5810f0a77..22bb43d55f74ccec17c7da3273c7f104a358b8fa 100644 (file)
@@ -9,10 +9,13 @@
  */
 
 #include <config.h>
+#include <algorithm>
 
 #include "InsetBibitem.h"
 
+#include "BiblioInfo.h"
 #include "Buffer.h"
+#include "buffer_funcs.h"
 #include "BufferParams.h"
 #include "BufferView.h"
 #include "Counters.h"
 #include "ParagraphList.h"
 #include "TextClass.h"
 
+#include "frontends/alert.h"
+
 #include "support/lstrings.h"
 #include "support/docstream.h"
+#include "support/gettext.h"
 #include "support/convert.h"
 
 #include <ostream>
@@ -49,6 +55,40 @@ InsetBibitem::InsetBibitem(InsetCommandParams const & p)
 }
 
 
+void InsetBibitem::initView()
+{
+       updateCommand(getParam("key"));
+}
+
+
+void InsetBibitem::updateCommand(docstring const & new_key, bool)
+{
+       docstring const old_key = getParam("key");
+       docstring key = new_key;
+
+       BiblioInfo keys;
+       keys.fillWithBibKeys(&buffer());
+       vector<docstring> bibkeys = keys.getKeys();
+
+       int i = 1;
+
+       if (find(bibkeys.begin(), bibkeys.end(), key) != bibkeys.end()) {
+               // generate unique label
+               key = new_key + '-' + convert<docstring>(i);
+               while (find(bibkeys.begin(), bibkeys.end(), key) != bibkeys.end()) {
+                       ++i;
+                       key = new_key + '-' + convert<docstring>(i);
+               }
+               frontend::Alert::warning(_("Keys must be unique!"),
+                       bformat(_("The key %1$s already exists,\n"
+                       "it will be changed to %2$s."), new_key, key));
+       }
+       setParam("key", key);
+
+       lyx::updateLabels(buffer());
+}
+
+
 ParamInfo const & InsetBibitem::findInfo(string const & /* cmdName */)
 {
        static ParamInfo param_info_;
@@ -71,10 +111,13 @@ void InsetBibitem::doDispatch(Cursor & cur, FuncRequest & cmd)
                        cur.noUpdate();
                        break;
                }
-               if (p["key"] != params()["key"])
-                       cur.bv().buffer().changeRefsIfUnique(params()["key"],
-                                                      p["key"], CITE_CODE);
-               setParams(p);
+               docstring old_key = params()["key"];
+               setParam("label", p["label"]);
+               updateCommand(p["key"]);
+               if (params()["key"] != old_key)
+                       cur.bv().buffer().changeRefsIfUnique(old_key,
+                               params()["key"], CITE_CODE);
+               break;
        }
 
        default:
@@ -208,7 +251,6 @@ void InsetBibitem::updateLabels(ParIterator const &)
        } else {
                autolabel_ = from_ascii("??");
        }
-       refresh();
 }