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 updateCommand(getParam("name"));
62 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
64 docstring const old_label = getParam("name");
65 docstring label = new_label;
67 while (buffer().insetLabel(label)) {
68 label = new_label + '-' + convert<docstring>(i);
72 if (label != new_label) {
73 // Warn the user that the label has been changed to something else.
74 frontend::Alert::warning(_("Label names must be unique!"),
75 bformat(_("The label %1$s already exists,\n"
76 "it will be changed to %2$s."), new_label, label));
77 } else if (label == old_label)
78 // Label was not changed.
81 buffer().undo().beginUndoGroup();
83 setParam("name", label);
86 Buffer::References & refs = buffer().references(old_label);
87 Buffer::References::iterator it = refs.begin();
88 Buffer::References::iterator end = refs.end();
89 for (; it != end; ++it) {
90 buffer().undo().recordUndo(it->second);
91 if (it->first->lyxCode() == MATH_REF_CODE) {
92 InsetMathHull * mi = it->first->asInsetMath()->asHullInset();
93 mi->asRefInset()->changeTarget(label);
95 InsetCommand * ref = it->first->asInsetCommand();
96 ref->setParam("reference", label);
100 buffer().undo().endUndoGroup();
104 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
106 static ParamInfo param_info_;
107 if (param_info_.empty())
108 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
109 ParamInfo::HANDLING_ESCAPE);
114 docstring InsetLabel::screenLabel() const
116 return screen_label_;
120 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
122 docstring const & label = getParam("name");
123 if (buffer().insetLabel(label)) {
124 // Problem: We already have an InsetLabel with the same name!
125 screen_label_ = _("DUPLICATE: ") + label;
128 buffer().setInsetLabel(label, this);
129 screen_label_ = label;
131 if (utype == OutputUpdate) {
132 // save info on the active counter
133 Counters const & cnts =
134 buffer().masterBuffer()->params().documentClass().counters();
135 active_counter_ = cnts.currentCounter();
136 Language const * lang = par->getParLanguage(buffer().params());
137 if (lang && !active_counter_.empty()) {
138 counter_value_ = cnts.theCounter(active_counter_, lang->code());
139 pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
141 counter_value_ = from_ascii("#");
142 pretty_counter_ = from_ascii("#");
148 void InsetLabel::addToToc(DocIterator const & cpit) const
150 docstring const & label = getParam("name");
151 Toc & toc = buffer().tocBackend().toc("label");
152 if (buffer().insetLabel(label) != this) {
153 toc.push_back(TocItem(cpit, 0, screen_label_));
156 toc.push_back(TocItem(cpit, 0, screen_label_));
157 Buffer::References const & refs = buffer().references(label);
158 Buffer::References::const_iterator it = refs.begin();
159 Buffer::References::const_iterator end = refs.end();
160 for (; it != end; ++it) {
161 DocIterator const ref_pit(it->second);
162 if (it->first->lyxCode() == MATH_REF_CODE)
163 toc.push_back(TocItem(ref_pit, 1,
164 it->first->asInsetMath()->asRefInset()->screenLabel()));
166 toc.push_back(TocItem(ref_pit, 1,
167 static_cast<InsetRef *>(it->first)->screenLabel()));
172 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
173 FuncStatus & status) const
176 switch (cmd.action()) {
177 case LFUN_LABEL_INSERT_AS_REF:
178 case LFUN_LABEL_COPY_AS_REF:
182 return InsetCommand::getStatus(cur, cmd, status);
185 status.setEnabled(enabled);
190 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
192 switch (cmd.action()) {
194 case LFUN_INSET_MODIFY: {
195 InsetCommandParams p(LABEL_CODE);
197 InsetCommand::string2params(to_utf8(cmd.argument()), p);
198 if (p.getCmdName().empty()) {
199 cur.noScreenUpdate();
202 if (p["name"] != params()["name"]) {
203 // undo is handled in updateCommand
204 updateCommand(p["name"]);
206 cur.forceBufferUpdate();
210 case LFUN_LABEL_COPY_AS_REF: {
211 InsetCommandParams p(REF_CODE, "ref");
212 p["reference"] = getParam("name");
213 cap::clearSelection();
214 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
218 case LFUN_LABEL_INSERT_AS_REF: {
219 InsetCommandParams p(REF_CODE, "ref");
220 p["reference"] = getParam("name");
221 string const data = InsetCommand::params2string(p);
222 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
227 InsetCommand::doDispatch(cur, cmd);
233 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
235 docstring const str = getParam("name");
236 os << '<' << str << '>';
237 return 2 + str.size();
241 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
243 os << "<!-- anchor id=\""
244 << sgml::cleanID(buffer(), runparams, getParam("name"))
250 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
253 // Unfortunately, the name attribute has been deprecated, so we have to use
254 // id here to get the document to validate as XHTML 1.1. This will cause a
255 // problem with some browsers, though, I'm sure. (Guess which!) So we will
256 // have to figure out what to do about this later.
257 string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
258 xs << html::CompTag("a", attr);