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)
56 void InsetLabel::initView()
58 // This seems to be used only for inset creation.
59 // Therefore we do not update refs here, since this would
60 // erroneously change refs from existing duplicate labels
62 updateLabel(getParam("name"));
66 void InsetLabel::uniqueLabel(docstring & label) const
68 docstring const new_label = label;
70 while (buffer().insetLabel(label)) {
71 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));
83 void InsetLabel::updateLabel(docstring const & new_label)
85 docstring label = new_label;
87 setParam("name", label);
91 void InsetLabel::updateLabelAndRefs(docstring const & new_label,
94 docstring const old_label = getParam("name");
95 docstring label = new_label;
97 if (label == old_label)
100 buffer().undo().beginUndoGroup();
102 cursor->recordUndo();
103 setParam("name", label);
104 updateReferences(old_label, label);
105 buffer().undo().endUndoGroup();
109 void InsetLabel::updateReferences(docstring const & old_label,
110 docstring const & new_label)
112 Buffer::References const & refs = buffer().references(old_label);
113 Buffer::References::const_iterator it = refs.begin();
114 Buffer::References::const_iterator end = refs.end();
115 for (; it != end; ++it) {
116 buffer().undo().recordUndo(CursorData(it->second));
117 if (it->first->lyxCode() == MATH_REF_CODE) {
118 InsetMathRef * mi = it->first->asInsetMath()->asRefInset();
119 mi->changeTarget(new_label);
121 InsetCommand * ref = it->first->asInsetCommand();
122 ref->setParam("reference", new_label);
128 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
130 static ParamInfo param_info_;
131 if (param_info_.empty())
132 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
133 ParamInfo::HANDLING_ESCAPE);
138 docstring InsetLabel::screenLabel() const
140 return screen_label_;
144 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
146 docstring const & label = getParam("name");
147 if (buffer().insetLabel(label)) {
148 // Problem: We already have an InsetLabel with the same name!
149 screen_label_ = _("DUPLICATE: ") + label;
152 buffer().setInsetLabel(label, this);
153 screen_label_ = label;
155 if (utype == OutputUpdate) {
156 // save info on the active counter
157 Counters const & cnts =
158 buffer().masterBuffer()->params().documentClass().counters();
159 active_counter_ = cnts.currentCounter();
160 Language const * lang = par->getParLanguage(buffer().params());
161 if (lang && !active_counter_.empty()) {
162 counter_value_ = cnts.theCounter(active_counter_, lang->code());
163 pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
165 counter_value_ = from_ascii("#");
166 pretty_counter_ = from_ascii("#");
172 void InsetLabel::addToToc(DocIterator const & cpit, bool output_active) const
174 docstring const & label = getParam("name");
175 Toc & toc = buffer().tocBackend().toc("label");
176 if (buffer().insetLabel(label) != this) {
177 toc.push_back(TocItem(cpit, 0, screen_label_, output_active));
180 toc.push_back(TocItem(cpit, 0, screen_label_, output_active));
181 Buffer::References const & refs = buffer().references(label);
182 Buffer::References::const_iterator it = refs.begin();
183 Buffer::References::const_iterator end = refs.end();
184 for (; it != end; ++it) {
185 DocIterator const ref_pit(it->second);
186 if (it->first->lyxCode() == MATH_REF_CODE)
187 toc.push_back(TocItem(ref_pit, 1,
188 it->first->asInsetMath()->asRefInset()->screenLabel(),
191 toc.push_back(TocItem(ref_pit, 1,
192 static_cast<InsetRef *>(it->first)->screenLabel(),
198 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
199 FuncStatus & status) const
202 switch (cmd.action()) {
203 case LFUN_LABEL_INSERT_AS_REF:
204 case LFUN_LABEL_COPY_AS_REF:
208 return InsetCommand::getStatus(cur, cmd, status);
211 status.setEnabled(enabled);
216 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
218 switch (cmd.action()) {
220 case LFUN_INSET_MODIFY: {
221 InsetCommandParams p(LABEL_CODE);
223 InsetCommand::string2params(to_utf8(cmd.argument()), p);
224 if (p.getCmdName().empty()) {
225 cur.noScreenUpdate();
228 if (p["name"] != params()["name"]) {
229 // undo is handled in updateLabelAndRefs
230 updateLabelAndRefs(p["name"], &cur);
232 cur.forceBufferUpdate();
236 case LFUN_LABEL_COPY_AS_REF: {
237 InsetCommandParams p(REF_CODE, "ref");
238 p["reference"] = getParam("name");
239 cap::clearSelection();
240 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
244 case LFUN_LABEL_INSERT_AS_REF: {
245 InsetCommandParams p(REF_CODE, "ref");
246 p["reference"] = getParam("name");
247 string const data = InsetCommand::params2string(p);
248 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
253 InsetCommand::doDispatch(cur, cmd);
259 int InsetLabel::plaintext(odocstringstream & os,
260 OutputParams const &, size_t) const
262 docstring const str = getParam("name");
263 os << '<' << str << '>';
264 return 2 + str.size();
268 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
270 os << "<!-- anchor id=\""
271 << sgml::cleanID(buffer(), runparams, getParam("name"))
277 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
280 // Unfortunately, the name attribute has been deprecated, so we have to use
281 // id here to get the document to validate as XHTML 1.1. This will cause a
282 // problem with some browsers, though, I'm sure. (Guess which!) So we will
283 // have to figure out what to do about this later.
284 string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
285 xs << html::CompTag("a", attr);