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")
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));
79 buffer().undo().beginUndoGroup();
80 setParam("name", label);
83 Buffer::References & refs = buffer().references(old_label);
84 Buffer::References::iterator it = refs.begin();
85 Buffer::References::iterator end = refs.end();
86 for (; it != end; ++it) {
87 buffer().undo().recordUndo(it->second);
88 if (it->first->lyxCode() == MATH_REF_CODE) {
90 static_cast<InsetMathHull *>(it->first);
91 mi->asRefInset()->changeTarget(label);
94 static_cast<InsetCommand *>(it->first);
95 ref->setParam("reference", label);
99 buffer().undo().endUndoGroup();
101 // We need an update of the Buffer reference cache. This is achieved by
103 buffer().updateBuffer();
107 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
109 static ParamInfo param_info_;
110 if (param_info_.empty())
111 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
112 ParamInfo::HANDLING_ESCAPE);
117 docstring InsetLabel::screenLabel() const
119 return screen_label_;
123 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
125 docstring const & label = getParam("name");
126 if (buffer().insetLabel(label)) {
127 // Problem: We already have an InsetLabel with the same name!
128 screen_label_ = _("DUPLICATE: ") + label;
131 buffer().setInsetLabel(label, this);
132 screen_label_ = label;
135 // save info on the active counter
136 Counters const & cnts =
137 buffer().masterBuffer()->params().documentClass().counters();
138 active_counter_ = cnts.currentCounter();
139 Language const * lang = par->getParLanguage(buffer().params());
140 if (lang && !active_counter_.empty()) {
141 counter_value_ = cnts.theCounter(active_counter_, lang->code());
142 pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
144 counter_value_ = from_ascii("??");
145 pretty_counter_ = from_ascii("??");
151 void InsetLabel::addToToc(DocIterator const & cpit)
153 docstring const & label = getParam("name");
154 Toc & toc = buffer().tocBackend().toc("label");
155 if (buffer().insetLabel(label) != this) {
156 toc.push_back(TocItem(cpit, 0, screen_label_));
159 toc.push_back(TocItem(cpit, 0, screen_label_));
160 Buffer::References const & refs = buffer().references(label);
161 Buffer::References::const_iterator it = refs.begin();
162 Buffer::References::const_iterator end = refs.end();
163 for (; it != end; ++it) {
164 DocIterator const ref_pit(it->second);
165 if (it->first->lyxCode() == MATH_REF_CODE)
166 toc.push_back(TocItem(ref_pit, 1,
167 static_cast<InsetMathHull *>(it->first)->asRefInset()
170 toc.push_back(TocItem(ref_pit, 1,
171 static_cast<InsetRef *>(it->first)->screenLabel()));
176 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
177 FuncStatus & status) const
180 switch (cmd.action) {
181 case LFUN_LABEL_INSERT_AS_REF:
182 case LFUN_LABEL_COPY_AS_REF:
186 return InsetCommand::getStatus(cur, cmd, status);
189 status.setEnabled(enabled);
194 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
196 switch (cmd.action) {
198 case LFUN_INSET_MODIFY: {
199 InsetCommandParams p(LABEL_CODE);
201 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
202 if (p.getCmdName().empty()) {
206 if (p["name"] != params()["name"])
207 updateCommand(p["name"]);
211 case LFUN_LABEL_COPY_AS_REF: {
212 InsetCommandParams p(REF_CODE, "ref");
213 p["reference"] = getParam("name");
214 cap::clearSelection();
215 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
219 case LFUN_LABEL_INSERT_AS_REF: {
220 InsetCommandParams p(REF_CODE, "ref");
221 p["reference"] = getParam("name");
222 string const data = InsetCommand::params2string("ref", p);
223 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
228 InsetCommand::doDispatch(cur, cmd);
234 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
236 docstring const str = getParam("name");
237 os << '<' << str << '>';
238 return 2 + str.size();
242 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
244 os << "<!-- anchor id=\""
245 << sgml::cleanID(buffer(), runparams, getParam("name"))
251 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
254 // Unfortunately, the name attribute has been deprecated, so we have to use
255 // id here to get the document to validate as XHTML 1.1. This will cause a
256 // problem with some browsers, though, I'm sure. (Guess which!) So we will
257 // have to figure out what to do about this later.
258 string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
259 xs << html::CompTag("a", attr);