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 "BufferParams.h"
20 #include "BufferView.h"
21 #include "CutAndPaste.h"
22 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
28 #include "output_xhtml.h"
29 #include "ParIterator.h"
32 #include "TextClass.h"
33 #include "TocBackend.h"
35 #include "frontends/alert.h"
37 #include "support/convert.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40 #include "support/lyxalgo.h"
43 using namespace lyx::support;
48 InsetLabel::InsetLabel(Buffer * buf, InsetCommandParams const & p)
49 : InsetCommand(buf, p, "label")
53 void InsetLabel::initView()
55 updateCommand(getParam("name"));
59 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
61 docstring const old_label = getParam("name");
62 docstring label = new_label;
64 while (buffer().insetLabel(label)) {
65 label = new_label + '-' + convert<docstring>(i);
69 if (label != new_label) {
70 // Warn the user that the label has been changed to something else.
71 frontend::Alert::warning(_("Label names must be unique!"),
72 bformat(_("The label %1$s already exists,\n"
73 "it will be changed to %2$s."), new_label, label));
76 buffer().undo().beginUndoGroup();
77 setParam("name", label);
80 Buffer::References & refs = buffer().references(old_label);
81 Buffer::References::iterator it = refs.begin();
82 Buffer::References::iterator end = refs.end();
83 for (; it != end; ++it) {
84 buffer().undo().recordUndo(it->second);
85 it->first->setParam("reference", label);
88 buffer().undo().endUndoGroup();
90 // We need an update of the Buffer reference cache. This is achieved by
92 buffer().updateLabels();
96 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
98 static ParamInfo param_info_;
99 if (param_info_.empty())
100 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
105 docstring InsetLabel::screenLabel() const
107 return screen_label_;
111 void InsetLabel::updateLabels(ParIterator const & par, UpdateType utype)
113 docstring const & label = getParam("name");
114 if (buffer().insetLabel(label)) {
115 // Problem: We already have an InsetLabel with the same name!
116 screen_label_ = _("DUPLICATE: ") + label;
119 buffer().setInsetLabel(label, this);
120 screen_label_ = label;
123 // save info on the active counter
124 Counters const & cnts =
125 buffer().masterBuffer()->params().documentClass().counters();
126 active_counter_ = cnts.currentCounter();
127 Language const * lang = par->getParLanguage(buffer().params());
128 if (lang && !active_counter_.empty()) {
129 counter_value_ = cnts.theCounter(active_counter_, lang->code());
130 pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
132 counter_value_ = from_ascii("??");
133 pretty_counter_ = from_ascii("??");
139 void InsetLabel::addToToc(DocIterator const & cpit)
141 docstring const & label = getParam("name");
142 Toc & toc = buffer().tocBackend().toc("label");
143 if (buffer().insetLabel(label) != this) {
144 toc.push_back(TocItem(cpit, 0, screen_label_));
147 toc.push_back(TocItem(cpit, 0, screen_label_));
148 Buffer::References const & refs = buffer().references(label);
149 Buffer::References::const_iterator it = refs.begin();
150 Buffer::References::const_iterator end = refs.end();
151 for (; it != end; ++it) {
152 DocIterator const ref_pit(it->second);
153 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
158 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
159 FuncStatus & status) const
162 switch (cmd.action) {
163 case LFUN_LABEL_INSERT_AS_REF:
164 case LFUN_LABEL_COPY_AS_REF:
168 return InsetCommand::getStatus(cur, cmd, status);
171 status.setEnabled(enabled);
176 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
178 switch (cmd.action) {
180 case LFUN_INSET_MODIFY: {
181 InsetCommandParams p(LABEL_CODE);
183 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
184 if (p.getCmdName().empty()) {
188 if (p["name"] != params()["name"])
189 updateCommand(p["name"]);
193 case LFUN_LABEL_COPY_AS_REF: {
194 InsetCommandParams p(REF_CODE, "ref");
195 p["reference"] = getParam("name");
196 cap::clearSelection();
197 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
201 case LFUN_LABEL_INSERT_AS_REF: {
202 InsetCommandParams p(REF_CODE, "ref");
203 p["reference"] = getParam("name");
204 string const data = InsetCommand::params2string("ref", p);
205 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
210 InsetCommand::doDispatch(cur, cmd);
216 int InsetLabel::latex(odocstream & os, OutputParams const &) const
218 os << escape(getCommand());
223 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
225 docstring const str = getParam("name");
226 os << '<' << str << '>';
227 return 2 + str.size();
231 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
233 os << "<!-- anchor id=\""
234 << sgml::cleanID(buffer(), runparams, getParam("name"))
240 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
243 // Unfortunately, the name attribute has been deprecated, so we have to use
244 // id here to get the document to validate as XHTML 1.1. This will cause a
245 // problem with some browsers, though, I'm sure. (Guess which!) So we will
246 // have to figure out what to do about this later.
247 string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
248 xs << html::CompTag("a", attr);