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 "CutAndPaste.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "InsetIterator.h"
25 #include "ParIterator.h"
28 #include "TocBackend.h"
30 #include "frontends/alert.h"
32 #include "support/convert.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35 #include "support/lyxalgo.h"
38 using namespace lyx::support;
43 InsetLabel::InsetLabel(InsetCommandParams const & p)
44 : InsetCommand(p, "label")
48 void InsetLabel::initView()
50 updateCommand(getParam("name"));
54 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
56 docstring const old_label = getParam("name");
57 docstring label = new_label;
59 while (buffer().insetLabel(label)) {
60 label = new_label + '-' + convert<docstring>(i);
64 if (label != new_label) {
65 // Warn the user that the label has been changed to something else.
66 frontend::Alert::warning(_("Label names must be unique!"),
67 bformat(_("The label %1$s already exists,\n"
68 "it will be changed to %2$s."), new_label, label));
71 buffer().undo().beginUndoGroup();
72 setParam("name", label);
75 Buffer::References & refs = buffer().references(old_label);
76 Buffer::References::iterator it = refs.begin();
77 Buffer::References::iterator end = refs.end();
78 for (; it != end; ++it) {
79 buffer().undo().recordUndo(it->second);
80 it->first->setParam("reference", label);
83 buffer().undo().endUndoGroup();
85 // We need an update of the Buffer reference cache. This is achieved by
87 buffer().updateLabels();
91 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
93 static ParamInfo param_info_;
94 if (param_info_.empty())
95 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
100 docstring InsetLabel::screenLabel() const
102 return screen_label_;
106 void InsetLabel::updateLabels(ParIterator const &)
108 docstring const & label = getParam("name");
109 if (buffer().insetLabel(label)) {
110 // Problem: We already have an InsetLabel with the same name!
111 screen_label_ = _("DUPLICATE: ") + label;
114 buffer().setInsetLabel(label, this);
115 screen_label_ = label;
119 void InsetLabel::addToToc(DocIterator const & cpit)
121 docstring const & label = getParam("name");
122 Toc & toc = buffer().tocBackend().toc("label");
123 if (buffer().insetLabel(label) != this) {
124 toc.push_back(TocItem(cpit, 0, screen_label_));
127 toc.push_back(TocItem(cpit, 0, screen_label_));
128 Buffer::References const & refs = buffer().references(label);
129 Buffer::References::const_iterator it = refs.begin();
130 Buffer::References::const_iterator end = refs.end();
131 for (; it != end; ++it) {
132 DocIterator const ref_pit(it->second);
133 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
138 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
139 FuncStatus & status) const
142 switch (cmd.action) {
143 case LFUN_COPY_LABEL_AS_REF:
147 return InsetCommand::getStatus(cur, cmd, status);
150 status.setEnabled(enabled);
155 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
157 switch (cmd.action) {
159 case LFUN_INSET_MODIFY: {
160 InsetCommandParams p(LABEL_CODE);
162 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
163 if (p.getCmdName().empty()) {
167 if (p["name"] != params()["name"])
168 updateCommand(p["name"]);
172 case LFUN_COPY_LABEL_AS_REF: {
173 InsetCommandParams p(REF_CODE, "ref");
174 p["reference"] = getParam("name");
175 cap::clearSelection();
176 cap::copyInset(cur, new InsetRef(cur.buffer(), p), getParam("name"));
181 InsetCommand::doDispatch(cur, cmd);
187 int InsetLabel::latex(odocstream & os, OutputParams const &) const
189 os << escape(getCommand());
194 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
196 docstring const str = getParam("name");
197 os << '<' << str << '>';
198 return 2 + str.size();
202 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
204 os << "<!-- anchor id=\""
205 << sgml::cleanID(buffer(), runparams, getParam("name"))