* src/insets/InsetLabel.[cpp,h}:

* src/insets/InsetCommand{.cpp,h}:
	- rename update to updateCommand

* src/CutAndPaste.cpp:
* src/insets/InsetBibitem{cpp,h}:
	- add duplicate check. There's a remaining glitch: the warning message after setting a bibitem
	  key in the dialog to a duplicate pops up twice.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23420 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Jürgen Spitzmüller 2008-03-04 06:41:47 +00:00
parent cbb7b9a0a4
commit 5fce07a02b
6 changed files with 78 additions and 11 deletions

View File

@ -225,7 +225,7 @@ pasteSelectionHelper(Cursor & cur, ParagraphList const & parlist,
// check for duplicates
InsetCommand & lab = static_cast<InsetCommand &>(*it);
docstring const oldname = lab.getParam("name");
lab.update(oldname, false);
lab.updateCommand(oldname, false);
docstring const newname = lab.getParam("name");
if (oldname != newname) {
// adapt the references
@ -240,6 +240,25 @@ pasteSelectionHelper(Cursor & cur, ParagraphList const & parlist,
break;
}
case BIBITEM_CODE: {
// check for duplicates
InsetCommand & bib = static_cast<InsetCommand &>(*it);
docstring const oldkey = bib.getParam("key");
bib.updateCommand(oldkey, false);
docstring const newkey = bib.getParam("key");
if (oldkey != newkey) {
// adapt the references
for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) {
if (itt->lyxCode() == CITE_CODE) {
InsetCommand & ref = dynamic_cast<InsetCommand &>(*itt);
if (ref.getParam("key") == oldkey)
ref.setParam("key", newkey);
}
}
}
break;
}
default:
break; // nothing
}

View File

@ -12,7 +12,9 @@
#include "InsetBibitem.h"
#include "BiblioInfo.h"
#include "Buffer.h"
#include "buffer_funcs.h"
#include "BufferParams.h"
#include "BufferView.h"
#include "Counters.h"
@ -25,8 +27,11 @@
#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 +54,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 +110,12 @@ 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);
}
default:

View File

@ -28,6 +28,11 @@ class InsetBibitem : public InsetCommand {
public:
///
InsetBibitem(InsetCommandParams const &);
/// verify label and update references.
/**
* Overloaded from Inset::initView.
**/
void initView();
///
void read(Lexer & lex);
///
@ -51,6 +56,8 @@ public:
///
static bool isCompatibleCommand(std::string const & s)
{ return s == "bibitem"; }
///
void updateCommand(docstring const & new_key, bool dummy = false);
protected:
///
virtual void doDispatch(Cursor & cur, FuncRequest & cmd);

View File

@ -82,8 +82,8 @@ public:
/// Whether this is a command this inset can represent.
/// Not implemented here. Must be implemented in derived class.
static bool isCompatibleCommand(std::string const & cmd);
/// update label and references. Currently used by InsetLabel.
virtual void update(docstring const &, bool) {};
/// update label and references.
virtual void updateCommand(docstring const &, bool) {};
protected:
///

View File

@ -45,11 +45,11 @@ InsetLabel::InsetLabel(InsetCommandParams const & p)
void InsetLabel::initView()
{
update(getParam("name"));
updateCommand(getParam("name"));
}
void InsetLabel::update(docstring const & new_label, bool updaterefs)
void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
{
docstring const old_label = getParam("name");
docstring label = new_label;
@ -148,7 +148,7 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
cur.noUpdate();
break;
}
update(p["name"]);
updateCommand(p["name"]);
break;
}

View File

@ -54,7 +54,7 @@ public:
///
void addToToc(ParConstIterator const &) const;
///
void update(docstring const & new_label, bool updaterefs = true);
void updateCommand(docstring const & new_label, bool updaterefs = true);
protected:
///
void doDispatch(Cursor & cur, FuncRequest & cmd);