Avoid duplicate labels when the user change an InsetLabel.

* InsetLabel:
- update(): new method for changing the label. Uses the Buffer reference cache instead of a lookup.
- LFUN_INSET_MODIFY: use update() instead of Buffer::changeRefIfUnique().




git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23393 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2008-03-02 17:04:10 +00:00
parent ce2a68ce8d
commit 132003aac5
2 changed files with 37 additions and 4 deletions

View File

@ -14,6 +14,7 @@
#include "InsetRef.h"
#include "buffer_funcs.h"
#include "Buffer.h"
#include "BufferView.h"
#include "DispatchResult.h"
@ -26,6 +27,7 @@
#include "frontends/alert.h"
#include "support/convert.h"
#include "support/lyxalgo.h"
#include "support/gettext.h"
#include "support/lstrings.h"
@ -41,6 +43,38 @@ InsetLabel::InsetLabel(InsetCommandParams const & p)
{}
void InsetLabel::update(docstring const & new_label)
{
docstring const old_label = getParam("name");
if (old_label == new_label)
return;
docstring label = new_label;
int i = 0;
while (buffer().insetLabel(label)) {
label = new_label + '-' + convert<docstring>(i);
++i;
}
setParam("name", label);
Buffer::References const & refs = buffer().references(old_label);
Buffer::References::const_iterator it = refs.begin();
Buffer::References::const_iterator end = refs.end();
for (; it != end; ++it)
it->first->setParam("reference", label);
if (label != new_label) {
// Warn the user that the label has been changed to something else.
frontend::Alert::warning(_("Label names must be unique!"),
bformat(_("The label %1$s already exists,\n"
"it will been changed to %2$s."), new_label, label));
}
// We need an update of the Buffer reference cache. This is achieved by
// updateLabel().
lyx::updateLabels(buffer());
}
ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
{
static ParamInfo param_info_;
@ -104,10 +138,7 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
cur.noUpdate();
break;
}
if (p["name"] != params()["name"])
cur.bv().buffer().changeRefsIfUnique(params()["name"],
p["name"], REF_CODE);
setParams(p);
update(p["name"]);
break;
}

View File

@ -52,6 +52,8 @@ protected:
private:
///
Inset * clone() const { return new InsetLabel(*this); }
///
void update(docstring const & new_label);
};