2002-09-25 14:26:13 +00:00
|
|
|
/**
|
2007-04-25 01:24:38 +00:00
|
|
|
* \file InsetLabel.cpp
|
2002-09-25 14:26:13 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:09:55 +00:00
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2002-03-21 17:09:55 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-09-25 14:26:13 +00:00
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
#include "InsetLabel.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
|
2008-03-02 15:27:35 +00:00
|
|
|
#include "InsetRef.h"
|
|
|
|
|
2008-03-02 17:04:10 +00:00
|
|
|
#include "buffer_funcs.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Buffer.h"
|
2010-01-20 19:03:17 +00:00
|
|
|
#include "BufferParams.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
#include "BufferView.h"
|
2008-11-16 19:19:07 +00:00
|
|
|
#include "CutAndPaste.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "DispatchResult.h"
|
|
|
|
#include "FuncRequest.h"
|
2008-11-16 19:19:07 +00:00
|
|
|
#include "FuncStatus.h"
|
2008-03-02 15:27:35 +00:00
|
|
|
#include "InsetIterator.h"
|
2010-01-20 19:03:17 +00:00
|
|
|
#include "Language.h"
|
2010-02-09 16:11:13 +00:00
|
|
|
#include "LyX.h"
|
2009-06-12 16:11:33 +00:00
|
|
|
#include "output_xhtml.h"
|
2008-02-27 15:23:22 +00:00
|
|
|
#include "ParIterator.h"
|
2004-10-29 23:08:04 +00:00
|
|
|
#include "sgml.h"
|
2008-02-27 15:23:22 +00:00
|
|
|
#include "Text.h"
|
2010-01-20 19:03:17 +00:00
|
|
|
#include "TextClass.h"
|
2008-02-27 15:23:22 +00:00
|
|
|
#include "TocBackend.h"
|
2003-02-27 16:24:14 +00:00
|
|
|
|
2010-01-28 17:37:22 +00:00
|
|
|
#include "mathed/InsetMathHull.h"
|
|
|
|
#include "mathed/InsetMathRef.h"
|
|
|
|
|
2008-03-02 15:27:35 +00:00
|
|
|
#include "frontends/alert.h"
|
|
|
|
|
2008-03-02 17:04:10 +00:00
|
|
|
#include "support/convert.h"
|
2008-03-02 15:27:35 +00:00
|
|
|
#include "support/gettext.h"
|
|
|
|
#include "support/lstrings.h"
|
2008-06-18 18:54:31 +00:00
|
|
|
#include "support/lyxalgo.h"
|
2003-10-10 21:08:55 +00:00
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
using namespace std;
|
2007-12-12 19:57:42 +00:00
|
|
|
using namespace lyx::support;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2009-11-08 16:10:34 +00:00
|
|
|
InsetLabel::InsetLabel(Buffer * buf, InsetCommandParams const & p)
|
2010-10-29 00:46:21 +00:00
|
|
|
: InsetCommand(buf, p)
|
2010-03-15 08:55:58 +00:00
|
|
|
{}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2008-03-03 17:58:26 +00:00
|
|
|
void InsetLabel::initView()
|
2008-03-02 18:14:12 +00:00
|
|
|
{
|
2012-09-19 08:14:56 +00:00
|
|
|
// This seems to be used only for inset creation.
|
|
|
|
// Therefore we do not update refs here, since this would
|
|
|
|
// erroneously change refs from existing duplicate labels
|
|
|
|
// (#8141).
|
|
|
|
updateLabel(getParam("name"));
|
2008-03-02 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-06 17:40:51 +00:00
|
|
|
void InsetLabel::uniqueLabel(docstring & label) const
|
2008-03-02 17:04:10 +00:00
|
|
|
{
|
2011-07-06 17:40:51 +00:00
|
|
|
docstring const new_label = label;
|
2008-03-03 08:19:36 +00:00
|
|
|
int i = 1;
|
2008-03-02 17:04:10 +00:00
|
|
|
while (buffer().insetLabel(label)) {
|
|
|
|
label = new_label + '-' + convert<docstring>(i);
|
|
|
|
++i;
|
|
|
|
}
|
2008-03-03 08:19:36 +00:00
|
|
|
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"
|
2008-03-03 13:58:18 +00:00
|
|
|
"it will be changed to %2$s."), new_label, label));
|
2011-07-06 17:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetLabel::updateLabel(docstring const & new_label)
|
|
|
|
{
|
|
|
|
docstring label = new_label;
|
|
|
|
uniqueLabel(label);
|
|
|
|
setParam("name", label);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetLabel::updateLabelAndRefs(docstring const & new_label,
|
|
|
|
Cursor * cursor)
|
|
|
|
{
|
|
|
|
docstring const old_label = getParam("name");
|
|
|
|
docstring label = new_label;
|
|
|
|
uniqueLabel(label);
|
|
|
|
if (label == old_label)
|
2011-06-29 13:42:55 +00:00
|
|
|
return;
|
2008-03-03 08:19:36 +00:00
|
|
|
|
2008-09-17 16:28:33 +00:00
|
|
|
buffer().undo().beginUndoGroup();
|
2011-07-06 18:52:56 +00:00
|
|
|
if (cursor)
|
|
|
|
cursor->recordUndo();
|
2008-03-02 17:04:10 +00:00
|
|
|
setParam("name", label);
|
2011-07-06 17:40:51 +00:00
|
|
|
updateReferences(old_label, label);
|
|
|
|
buffer().undo().endUndoGroup();
|
|
|
|
}
|
|
|
|
|
2008-03-02 17:04:10 +00:00
|
|
|
|
2011-07-06 17:40:51 +00:00
|
|
|
void InsetLabel::updateReferences(docstring const & old_label,
|
|
|
|
docstring const & new_label)
|
|
|
|
{
|
2013-03-18 23:32:30 +00:00
|
|
|
Buffer::References const & refs = buffer().references(old_label);
|
|
|
|
Buffer::References::const_iterator it = refs.begin();
|
|
|
|
Buffer::References::const_iterator end = refs.end();
|
2011-07-06 17:40:51 +00:00
|
|
|
for (; it != end; ++it) {
|
2012-07-15 16:16:09 +00:00
|
|
|
buffer().undo().recordUndo(CursorData(it->second));
|
2011-07-06 17:40:51 +00:00
|
|
|
if (it->first->lyxCode() == MATH_REF_CODE) {
|
2012-03-23 18:37:48 +00:00
|
|
|
InsetMathRef * mi = it->first->asInsetMath()->asRefInset();
|
|
|
|
mi->changeTarget(new_label);
|
2011-07-06 17:40:51 +00:00
|
|
|
} else {
|
|
|
|
InsetCommand * ref = it->first->asInsetCommand();
|
|
|
|
ref->setParam("reference", new_label);
|
2008-09-17 16:28:33 +00:00
|
|
|
}
|
2008-03-03 09:35:26 +00:00
|
|
|
}
|
2008-03-02 17:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-23 22:01:02 +00:00
|
|
|
ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
|
2007-10-25 04:13:56 +00:00
|
|
|
{
|
2008-02-23 22:01:02 +00:00
|
|
|
static ParamInfo param_info_;
|
2008-11-16 16:43:49 +00:00
|
|
|
if (param_info_.empty())
|
2010-02-13 13:08:32 +00:00
|
|
|
param_info_.add("name", ParamInfo::LATEX_REQUIRED,
|
|
|
|
ParamInfo::HANDLING_ESCAPE);
|
2008-02-23 22:01:02 +00:00
|
|
|
return param_info_;
|
2007-10-25 04:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
docstring InsetLabel::screenLabel() const
|
2003-10-12 18:54:12 +00:00
|
|
|
{
|
2008-03-03 13:31:44 +00:00
|
|
|
return screen_label_;
|
2003-10-12 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-03 22:13:45 +00:00
|
|
|
void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
|
2008-03-02 15:27:35 +00:00
|
|
|
{
|
|
|
|
docstring const & label = getParam("name");
|
2008-03-03 13:31:44 +00:00
|
|
|
if (buffer().insetLabel(label)) {
|
2008-03-02 15:27:35 +00:00
|
|
|
// Problem: We already have an InsetLabel with the same name!
|
2008-03-03 13:31:44 +00:00
|
|
|
screen_label_ = _("DUPLICATE: ") + label;
|
2008-03-02 15:27:35 +00:00
|
|
|
return;
|
2008-03-03 13:31:44 +00:00
|
|
|
}
|
2008-03-02 15:27:35 +00:00
|
|
|
buffer().setInsetLabel(label, this);
|
2008-03-03 13:31:44 +00:00
|
|
|
screen_label_ = label;
|
2010-01-20 19:03:17 +00:00
|
|
|
|
2010-11-29 17:14:38 +00:00
|
|
|
if (utype == OutputUpdate) {
|
2010-01-20 19:03:17 +00:00
|
|
|
// save info on the active counter
|
|
|
|
Counters const & cnts =
|
|
|
|
buffer().masterBuffer()->params().documentClass().counters();
|
|
|
|
active_counter_ = cnts.currentCounter();
|
|
|
|
Language const * lang = par->getParLanguage(buffer().params());
|
2010-01-20 20:33:36 +00:00
|
|
|
if (lang && !active_counter_.empty()) {
|
2010-01-20 19:03:17 +00:00
|
|
|
counter_value_ = cnts.theCounter(active_counter_, lang->code());
|
2010-01-20 20:33:36 +00:00
|
|
|
pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
|
|
|
|
} else {
|
2010-11-29 19:11:38 +00:00
|
|
|
counter_value_ = from_ascii("#");
|
|
|
|
pretty_counter_ = from_ascii("#");
|
2010-01-20 20:33:36 +00:00
|
|
|
}
|
2010-01-20 19:03:17 +00:00
|
|
|
}
|
2008-03-02 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-08 21:18:26 +00:00
|
|
|
void InsetLabel::addToToc(DocIterator const & cpit, bool output_active) const
|
2008-02-27 15:23:22 +00:00
|
|
|
{
|
2008-03-02 15:27:35 +00:00
|
|
|
docstring const & label = getParam("name");
|
2008-02-27 20:43:16 +00:00
|
|
|
Toc & toc = buffer().tocBackend().toc("label");
|
2008-03-02 15:27:35 +00:00
|
|
|
if (buffer().insetLabel(label) != this) {
|
2013-03-08 21:18:26 +00:00
|
|
|
toc.push_back(TocItem(cpit, 0, screen_label_, output_active));
|
2008-03-02 15:27:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-03-08 21:18:26 +00:00
|
|
|
toc.push_back(TocItem(cpit, 0, screen_label_, output_active));
|
2008-03-02 15:27:35 +00:00
|
|
|
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) {
|
2008-05-13 08:23:44 +00:00
|
|
|
DocIterator const ref_pit(it->second);
|
2010-01-28 17:37:22 +00:00
|
|
|
if (it->first->lyxCode() == MATH_REF_CODE)
|
|
|
|
toc.push_back(TocItem(ref_pit, 1,
|
2013-03-08 21:18:26 +00:00
|
|
|
it->first->asInsetMath()->asRefInset()->screenLabel(),
|
|
|
|
output_active));
|
2010-01-28 17:37:22 +00:00
|
|
|
else
|
|
|
|
toc.push_back(TocItem(ref_pit, 1,
|
2013-03-08 21:18:26 +00:00
|
|
|
static_cast<InsetRef *>(it->first)->screenLabel(),
|
|
|
|
output_active));
|
2008-03-02 15:27:35 +00:00
|
|
|
}
|
2008-02-27 15:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-16 19:19:07 +00:00
|
|
|
bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
|
|
|
|
FuncStatus & status) const
|
|
|
|
{
|
|
|
|
bool enabled;
|
2010-04-09 19:00:42 +00:00
|
|
|
switch (cmd.action()) {
|
2009-05-15 21:05:19 +00:00
|
|
|
case LFUN_LABEL_INSERT_AS_REF:
|
|
|
|
case LFUN_LABEL_COPY_AS_REF:
|
2008-11-16 19:19:07 +00:00
|
|
|
enabled = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return InsetCommand::getStatus(cur, cmd, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
status.setEnabled(enabled);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 14:56:30 +00:00
|
|
|
void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
|
2003-02-27 16:24:14 +00:00
|
|
|
{
|
2010-04-09 19:00:42 +00:00
|
|
|
switch (cmd.action()) {
|
2003-05-16 07:44:00 +00:00
|
|
|
|
2003-03-07 21:44:48 +00:00
|
|
|
case LFUN_INSET_MODIFY: {
|
2007-10-19 17:22:55 +00:00
|
|
|
InsetCommandParams p(LABEL_CODE);
|
2006-11-02 10:16:21 +00:00
|
|
|
// FIXME UNICODE
|
2010-10-29 00:25:28 +00:00
|
|
|
InsetCommand::string2params(to_utf8(cmd.argument()), p);
|
2004-02-16 11:58:51 +00:00
|
|
|
if (p.getCmdName().empty()) {
|
2010-07-08 20:04:35 +00:00
|
|
|
cur.noScreenUpdate();
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-12-08 09:24:04 +00:00
|
|
|
if (p["name"] != params()["name"]) {
|
2011-07-06 17:40:51 +00:00
|
|
|
// undo is handled in updateLabelAndRefs
|
|
|
|
updateLabelAndRefs(p["name"], &cur);
|
2010-12-08 09:24:04 +00:00
|
|
|
}
|
2010-07-09 14:37:00 +00:00
|
|
|
cur.forceBufferUpdate();
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2003-03-07 21:44:48 +00:00
|
|
|
}
|
2003-03-09 09:38:47 +00:00
|
|
|
|
2009-05-15 21:05:19 +00:00
|
|
|
case LFUN_LABEL_COPY_AS_REF: {
|
2008-11-16 19:19:07 +00:00
|
|
|
InsetCommandParams p(REF_CODE, "ref");
|
|
|
|
p["reference"] = getParam("name");
|
|
|
|
cap::clearSelection();
|
2009-11-08 15:53:21 +00:00
|
|
|
cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
|
2008-11-16 19:19:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-15 21:05:19 +00:00
|
|
|
case LFUN_LABEL_INSERT_AS_REF: {
|
2009-04-09 18:31:17 +00:00
|
|
|
InsetCommandParams p(REF_CODE, "ref");
|
|
|
|
p["reference"] = getParam("name");
|
2010-10-29 00:25:28 +00:00
|
|
|
string const data = InsetCommand::params2string(p);
|
2009-04-09 18:31:17 +00:00
|
|
|
lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-03-07 21:44:48 +00:00
|
|
|
default:
|
2004-11-24 21:58:42 +00:00
|
|
|
InsetCommand::doDispatch(cur, cmd);
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2000-05-19 16:46:01 +00:00
|
|
|
}
|
|
|
|
}
|
1999-12-07 00:44:53 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
|
2013-03-08 19:52:18 +00:00
|
|
|
int InsetLabel::plaintext(odocstringstream & os,
|
|
|
|
OutputParams const &, size_t) const
|
2000-04-24 20:58:23 +00:00
|
|
|
{
|
2007-02-24 14:35:38 +00:00
|
|
|
docstring const str = getParam("name");
|
2007-02-15 23:44:33 +00:00
|
|
|
os << '<' << str << '>';
|
|
|
|
return 2 + str.size();
|
2000-04-24 20:58:23 +00:00
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
|
2000-03-06 02:42:40 +00:00
|
|
|
{
|
2006-10-19 21:00:33 +00:00
|
|
|
os << "<!-- anchor id=\""
|
2008-02-27 20:43:16 +00:00
|
|
|
<< sgml::cleanID(buffer(), runparams, getParam("name"))
|
2007-05-28 22:27:45 +00:00
|
|
|
<< "\" -->";
|
2000-03-06 02:42:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2009-11-28 21:37:47 +00:00
|
|
|
docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
|
2009-06-05 17:48:14 +00:00
|
|
|
{
|
2009-11-28 21:37:47 +00:00
|
|
|
// FIXME XHTML
|
|
|
|
// Unfortunately, the name attribute has been deprecated, so we have to use
|
|
|
|
// id here to get the document to validate as XHTML 1.1. This will cause a
|
|
|
|
// problem with some browsers, though, I'm sure. (Guess which!) So we will
|
|
|
|
// have to figure out what to do about this later.
|
|
|
|
string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
|
2010-01-19 22:08:04 +00:00
|
|
|
xs << html::CompTag("a", attr);
|
2009-06-12 17:23:17 +00:00
|
|
|
return docstring();
|
2009-06-05 17:48:14 +00:00
|
|
|
}
|
|
|
|
|
2009-11-28 21:37:47 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|