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 // FIXME: This seems to be used only for inset creation so
59 // we probably just need to call updateLabel() here.
60 updateLabelAndRefs(getParam("name"));
64 void InsetLabel::uniqueLabel(docstring & label) const
66 docstring const new_label = label;
68 while (buffer().insetLabel(label)) {
69 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));
81 void InsetLabel::updateLabel(docstring const & new_label)
83 docstring label = new_label;
85 setParam("name", label);
89 void InsetLabel::updateLabelAndRefs(docstring const & new_label,
92 docstring const old_label = getParam("name");
93 docstring label = new_label;
95 if (label == old_label)
98 buffer().undo().beginUndoGroup();
100 cursor->recordUndo();
101 setParam("name", label);
102 updateReferences(old_label, label);
103 buffer().undo().endUndoGroup();
107 void InsetLabel::updateReferences(docstring const & old_label,
108 docstring const & new_label)
110 Buffer::References & refs = buffer().references(old_label);
111 Buffer::References::iterator it = refs.begin();
112 Buffer::References::iterator end = refs.end();
113 for (; it != end; ++it) {
114 buffer().undo().recordUndo(it->second);
115 if (it->first->lyxCode() == MATH_REF_CODE) {
116 InsetMathRef * mi = it->first->asInsetMath()->asRefInset();
117 mi->changeTarget(new_label);
119 InsetCommand * ref = it->first->asInsetCommand();
120 ref->setParam("reference", new_label);
126 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
128 static ParamInfo param_info_;
129 if (param_info_.empty())
130 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
131 ParamInfo::HANDLING_ESCAPE);
136 docstring InsetLabel::screenLabel() const
138 return screen_label_;
142 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
144 docstring const & label = getParam("name");
145 if (buffer().insetLabel(label)) {
146 // Problem: We already have an InsetLabel with the same name!
147 screen_label_ = _("DUPLICATE: ") + label;
150 buffer().setInsetLabel(label, this);
151 screen_label_ = label;
153 if (utype == OutputUpdate) {
154 // save info on the active counter
155 Counters const & cnts =
156 buffer().masterBuffer()->params().documentClass().counters();
157 active_counter_ = cnts.currentCounter();
158 Language const * lang = par->getParLanguage(buffer().params());
159 if (lang && !active_counter_.empty()) {
160 counter_value_ = cnts.theCounter(active_counter_, lang->code());
161 pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
163 counter_value_ = from_ascii("#");
164 pretty_counter_ = from_ascii("#");
170 void InsetLabel::addToToc(DocIterator const & cpit) const
172 docstring const & label = getParam("name");
173 Toc & toc = buffer().tocBackend().toc("label");
174 if (buffer().insetLabel(label) != this) {
175 toc.push_back(TocItem(cpit, 0, screen_label_));
178 toc.push_back(TocItem(cpit, 0, screen_label_));
179 Buffer::References const & refs = buffer().references(label);
180 Buffer::References::const_iterator it = refs.begin();
181 Buffer::References::const_iterator end = refs.end();
182 for (; it != end; ++it) {
183 DocIterator const ref_pit(it->second);
184 if (it->first->lyxCode() == MATH_REF_CODE)
185 toc.push_back(TocItem(ref_pit, 1,
186 it->first->asInsetMath()->asRefInset()->screenLabel()));
188 toc.push_back(TocItem(ref_pit, 1,
189 static_cast<InsetRef *>(it->first)->screenLabel()));
194 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
195 FuncStatus & status) const
198 switch (cmd.action()) {
199 case LFUN_LABEL_INSERT_AS_REF:
200 case LFUN_LABEL_COPY_AS_REF:
204 return InsetCommand::getStatus(cur, cmd, status);
207 status.setEnabled(enabled);
212 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
214 switch (cmd.action()) {
216 case LFUN_INSET_MODIFY: {
217 InsetCommandParams p(LABEL_CODE);
219 InsetCommand::string2params(to_utf8(cmd.argument()), p);
220 if (p.getCmdName().empty()) {
221 cur.noScreenUpdate();
224 if (p["name"] != params()["name"]) {
225 // undo is handled in updateLabelAndRefs
226 updateLabelAndRefs(p["name"], &cur);
228 cur.forceBufferUpdate();
232 case LFUN_LABEL_COPY_AS_REF: {
233 InsetCommandParams p(REF_CODE, "ref");
234 p["reference"] = getParam("name");
235 cap::clearSelection();
236 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
240 case LFUN_LABEL_INSERT_AS_REF: {
241 InsetCommandParams p(REF_CODE, "ref");
242 p["reference"] = getParam("name");
243 string const data = InsetCommand::params2string(p);
244 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
249 InsetCommand::doDispatch(cur, cmd);
255 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
257 docstring const str = getParam("name");
258 os << '<' << str << '>';
259 return 2 + str.size();
263 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
265 os << "<!-- anchor id=\""
266 << sgml::cleanID(buffer(), runparams, getParam("name"))
272 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
275 // Unfortunately, the name attribute has been deprecated, so we have to use
276 // id here to get the document to validate as XHTML 1.1. This will cause a
277 // problem with some browsers, though, I'm sure. (Guess which!) So we will
278 // have to figure out what to do about this later.
279 string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
280 xs << html::CompTag("a", attr);