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 "mathed/InsetMathHull.h"
36 #include "mathed/InsetMathRef.h"
38 #include "frontends/alert.h"
40 #include "support/convert.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
43 #include "support/lyxalgo.h"
46 using namespace lyx::support;
51 InsetLabel::InsetLabel(Buffer * buf, InsetCommandParams const & p)
52 : InsetCommand(buf, p, "label")
54 screen_label_ = p["name"];
58 void InsetLabel::initView()
60 updateCommand(getParam("name"));
64 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
66 docstring const old_label = getParam("name");
67 docstring label = new_label;
69 while (buffer().insetLabel(label)) {
70 label = new_label + '-' + convert<docstring>(i);
74 if (label != new_label) {
75 // Warn the user that the label has been changed to something else.
76 frontend::Alert::warning(_("Label names must be unique!"),
77 bformat(_("The label %1$s already exists,\n"
78 "it will be changed to %2$s."), new_label, label));
81 buffer().undo().beginUndoGroup();
82 setParam("name", label);
85 Buffer::References & refs = buffer().references(old_label);
86 Buffer::References::iterator it = refs.begin();
87 Buffer::References::iterator end = refs.end();
88 for (; it != end; ++it) {
89 buffer().undo().recordUndo(it->second);
90 if (it->first->lyxCode() == MATH_REF_CODE) {
92 static_cast<InsetMathHull *>(it->first);
93 mi->asRefInset()->changeTarget(label);
96 static_cast<InsetCommand *>(it->first);
97 ref->setParam("reference", label);
101 buffer().undo().endUndoGroup();
103 // We need an update of the Buffer reference cache. This is achieved by
105 buffer().updateBuffer();
106 screen_label_ = label;
110 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
112 static ParamInfo param_info_;
113 if (param_info_.empty())
114 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
115 ParamInfo::HANDLING_ESCAPE);
120 docstring InsetLabel::screenLabel() const
122 return screen_label_;
126 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
128 docstring const & label = getParam("name");
129 if (buffer().insetLabel(label)) {
130 // Problem: We already have an InsetLabel with the same name!
131 screen_label_ = _("DUPLICATE: ") + label;
134 buffer().setInsetLabel(label, this);
135 screen_label_ = label;
138 // save info on the active counter
139 Counters const & cnts =
140 buffer().masterBuffer()->params().documentClass().counters();
141 active_counter_ = cnts.currentCounter();
142 Language const * lang = par->getParLanguage(buffer().params());
143 if (lang && !active_counter_.empty()) {
144 counter_value_ = cnts.theCounter(active_counter_, lang->code());
145 pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
147 counter_value_ = from_ascii("??");
148 pretty_counter_ = from_ascii("??");
154 void InsetLabel::addToToc(DocIterator const & cpit)
156 docstring const & label = getParam("name");
157 Toc & toc = buffer().tocBackend().toc("label");
158 if (buffer().insetLabel(label) != this) {
159 toc.push_back(TocItem(cpit, 0, screen_label_));
162 toc.push_back(TocItem(cpit, 0, screen_label_));
163 Buffer::References const & refs = buffer().references(label);
164 Buffer::References::const_iterator it = refs.begin();
165 Buffer::References::const_iterator end = refs.end();
166 for (; it != end; ++it) {
167 DocIterator const ref_pit(it->second);
168 if (it->first->lyxCode() == MATH_REF_CODE)
169 toc.push_back(TocItem(ref_pit, 1,
170 static_cast<InsetMathHull *>(it->first)->asRefInset()
173 toc.push_back(TocItem(ref_pit, 1,
174 static_cast<InsetRef *>(it->first)->screenLabel()));
179 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
180 FuncStatus & status) const
183 switch (cmd.action) {
184 case LFUN_LABEL_INSERT_AS_REF:
185 case LFUN_LABEL_COPY_AS_REF:
189 return InsetCommand::getStatus(cur, cmd, status);
192 status.setEnabled(enabled);
197 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
199 switch (cmd.action) {
201 case LFUN_INSET_MODIFY: {
202 InsetCommandParams p(LABEL_CODE);
204 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
205 if (p.getCmdName().empty()) {
209 if (p["name"] != params()["name"])
210 updateCommand(p["name"]);
214 case LFUN_LABEL_COPY_AS_REF: {
215 InsetCommandParams p(REF_CODE, "ref");
216 p["reference"] = getParam("name");
217 cap::clearSelection();
218 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
222 case LFUN_LABEL_INSERT_AS_REF: {
223 InsetCommandParams p(REF_CODE, "ref");
224 p["reference"] = getParam("name");
225 string const data = InsetCommand::params2string("ref", p);
226 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
231 InsetCommand::doDispatch(cur, cmd);
237 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
239 docstring const str = getParam("name");
240 os << '<' << str << '>';
241 return 2 + str.size();
245 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
247 os << "<!-- anchor id=\""
248 << sgml::cleanID(buffer(), runparams, getParam("name"))
254 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
257 // Unfortunately, the name attribute has been deprecated, so we have to use
258 // id here to get the document to validate as XHTML 1.1. This will cause a
259 // problem with some browsers, though, I'm sure. (Guess which!) So we will
260 // have to figure out what to do about this later.
261 string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
262 xs << html::CompTag("a", attr);