3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "InsetLabel.h"
17 #include "buffer_funcs.h"
19 #include "BufferView.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "InsetIterator.h"
23 #include "ParIterator.h"
26 #include "TocBackend.h"
28 #include "frontends/alert.h"
30 #include "support/convert.h"
31 #include "support/lyxalgo.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
36 using namespace lyx::support;
41 InsetLabel::InsetLabel(InsetCommandParams const & p)
42 : InsetCommand(p, "label")
46 void InsetLabel::initView()
48 update(getParam("name"));
52 void InsetLabel::update(docstring const & new_label, bool updaterefs)
54 docstring const old_label = getParam("name");
55 docstring label = new_label;
57 while (buffer().insetLabel(label)) {
58 label = new_label + '-' + convert<docstring>(i);
62 if (label != new_label) {
63 // Warn the user that the label has been changed to something else.
64 frontend::Alert::warning(_("Label names must be unique!"),
65 bformat(_("The label %1$s already exists,\n"
66 "it will be changed to %2$s."), new_label, label));
69 setParam("name", label);
72 Buffer::References & refs = buffer().references(old_label);
73 Buffer::References::iterator it = refs.begin();
74 Buffer::References::iterator end = refs.end();
75 for (; it != end; ++it)
76 it->first->setParam("reference", label);
79 // We need an update of the Buffer reference cache. This is achieved by
81 lyx::updateLabels(buffer());
85 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
87 static ParamInfo param_info_;
88 if (param_info_.empty()) {
89 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
95 void InsetLabel::getLabelList(vector<docstring> & list) const
97 list.push_back(getParam("name"));
101 docstring InsetLabel::screenLabel() const
103 return screen_label_;
107 void InsetLabel::updateLabels(ParIterator const &)
109 docstring const & label = getParam("name");
110 if (buffer().insetLabel(label)) {
111 // Problem: We already have an InsetLabel with the same name!
112 screen_label_ = _("DUPLICATE: ") + label;
115 buffer().setInsetLabel(label, this);
116 screen_label_ = label;
120 void InsetLabel::addToToc(ParConstIterator const & cpit) const
122 docstring const & label = getParam("name");
123 Toc & toc = buffer().tocBackend().toc("label");
124 if (buffer().insetLabel(label) != this) {
125 toc.push_back(TocItem(cpit, 0, screen_label_));
128 toc.push_back(TocItem(cpit, 0, screen_label_));
129 Buffer::References const & refs = buffer().references(label);
130 Buffer::References::const_iterator it = refs.begin();
131 Buffer::References::const_iterator end = refs.end();
132 for (; it != end; ++it) {
133 ParConstIterator const ref_pit(it->second);
134 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
139 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
141 switch (cmd.action) {
143 case LFUN_INSET_MODIFY: {
144 InsetCommandParams p(LABEL_CODE);
146 InsetCommandMailer::string2params("label", to_utf8(cmd.argument()), p);
147 if (p.getCmdName().empty()) {
156 InsetCommand::doDispatch(cur, cmd);
162 int InsetLabel::latex(odocstream & os, OutputParams const &) const
164 os << escape(getCommand());
169 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
171 docstring const str = getParam("name");
172 os << '<' << str << '>';
173 return 2 + str.size();
177 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
179 os << "<!-- anchor id=\""
180 << sgml::cleanID(buffer(), runparams, getParam("name"))