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)
113 Buffer::References const & refs = buffer().references(old_label);
114 Buffer::References::const_iterator it = refs.begin();
115 Buffer::References::const_iterator end = refs.end();
116 for (; it != end; ++it) {
117 ugh.resetBuffer(it->second.buffer());
118 it->second.buffer()->undo().recordUndo(CursorData(it->second));
119 if (it->first->lyxCode() == MATH_REF_CODE) {
120 InsetMathRef * mi = it->first->asInsetMath()->asRefInset();
121 mi->changeTarget(new_label);
123 InsetCommand * ref = it->first->asInsetCommand();
124 ref->setParam("reference", new_label);
130 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
132 static ParamInfo param_info_;
133 if (param_info_.empty())
134 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
135 ParamInfo::HANDLING_ESCAPE);
140 docstring InsetLabel::screenLabel() const
142 return screen_label_;
146 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
148 docstring const & label = getParam("name");
149 if (buffer().insetLabel(label)) {
150 // Problem: We already have an InsetLabel with the same name!
151 screen_label_ = _("DUPLICATE: ") + label;
154 buffer().setInsetLabel(label, this);
155 screen_label_ = label;
157 if (utype == OutputUpdate) {
158 // save info on the active counter
159 Counters const & cnts =
160 buffer().masterBuffer()->params().documentClass().counters();
161 active_counter_ = cnts.currentCounter();
162 Language const * lang = par->getParLanguage(buffer().params());
163 if (lang && !active_counter_.empty()) {
164 counter_value_ = cnts.theCounter(active_counter_, lang->code());
165 pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
167 counter_value_ = from_ascii("#");
168 pretty_counter_ = from_ascii("#");
174 void InsetLabel::addToToc(DocIterator const & cpit, bool output_active,
175 UpdateType, TocBackend & backend) const
177 docstring const & label = getParam("name");
178 shared_ptr<Toc> toc = backend.toc("label");
179 if (buffer().insetLabel(label) != this) {
180 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
182 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
183 Buffer::References const & refs = buffer().references(label);
184 Buffer::References::const_iterator it = refs.begin();
185 Buffer::References::const_iterator end = refs.end();
186 for (; it != end; ++it) {
187 DocIterator const ref_pit(it->second);
188 if (it->first->lyxCode() == MATH_REF_CODE)
189 toc->push_back(TocItem(ref_pit, 1,
190 it->first->asInsetMath()->asRefInset()->screenLabel(),
193 toc->push_back(TocItem(ref_pit, 1,
194 static_cast<InsetRef *>(it->first)->getTOCString(),
201 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
202 FuncStatus & status) const
205 switch (cmd.action()) {
206 case LFUN_LABEL_INSERT_AS_REFERENCE:
207 case LFUN_LABEL_COPY_AS_REFERENCE:
210 case LFUN_INSET_MODIFY:
211 if (cmd.getArg(0) == "changetype") {
212 // this is handled by InsetCommand,
213 // but not by InsetLabel.
218 return InsetCommand::getStatus(cur, cmd, status);
221 status.setEnabled(enabled);
226 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
228 switch (cmd.action()) {
230 case LFUN_INSET_MODIFY: {
231 // the only other option here is "changetype", and we
232 // do not have different types.
233 if (cmd.getArg(0) != "label") {
237 InsetCommandParams p(LABEL_CODE);
239 InsetCommand::string2params(to_utf8(cmd.argument()), p);
240 if (p.getCmdName().empty()) {
241 cur.noScreenUpdate();
244 if (p["name"] != params()["name"]) {
245 // undo is handled in updateLabelAndRefs
246 updateLabelAndRefs(p["name"], &cur);
248 cur.forceBufferUpdate();
252 case LFUN_LABEL_COPY_AS_REFERENCE: {
253 InsetCommandParams p(REF_CODE, "ref");
254 p["reference"] = getParam("name");
255 cap::clearSelection();
256 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
260 case LFUN_LABEL_INSERT_AS_REFERENCE: {
261 InsetCommandParams p(REF_CODE, "ref");
262 p["reference"] = getParam("name");
263 string const data = InsetCommand::params2string(p);
264 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
269 InsetCommand::doDispatch(cur, cmd);
275 int InsetLabel::plaintext(odocstringstream & os,
276 OutputParams const &, size_t) const
278 docstring const str = getParam("name");
279 os << '<' << str << '>';
280 return 2 + str.size();
284 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
286 os << "<!-- anchor id=\""
287 << sgml::cleanID(buffer(), runparams, getParam("name"))
293 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
296 // Unfortunately, the name attribute has been deprecated, so we have to use
297 // id here to get the document to validate as XHTML 1.1. This will cause a
298 // problem with some browsers, though, I'm sure. (Guess which!) So we will
299 // have to figure out what to do about this later.
300 docstring const attr = "id=\"" + html::cleanAttr(getParam("name")) + '"';
301 xs << html::CompTag("a", to_utf8(attr));