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"
16 #include "BufferView.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
19 #include "InsetList.h"
21 #include "paragraph.h"
22 #include "pariterator.h"
25 #include "frontends/LyXView.h"
27 #include "support/lstrings.h"
28 #include "support/lyxalgo.h"
29 #include "support/std_ostream.h"
31 using lyx::support::escape;
38 InsetLabel::InsetLabel(InsetCommandParams const & p)
39 : InsetCommand(p, "label")
43 std::auto_ptr<InsetBase> InsetLabel::doClone() const
45 return std::auto_ptr<InsetBase>(new InsetLabel(params()));
49 void InsetLabel::getLabelList(Buffer const &, std::vector<string> & list) const
51 list.push_back(getContents());
55 string const InsetLabel::getScreenLabel(Buffer const &) const
63 void changeRefsIfUnique(BufferView & bv, string const & from, string const & to)
65 // Check if the label 'from' appears more than once
66 vector<string> labels;
67 bv.buffer()->getLabelList(labels);
69 if (lyx::count(labels.begin(), labels.end(), from) > 1)
72 InsetBase::Code code = InsetBase::REF_CODE;
74 ParIterator it = bv.buffer()->par_iterator_begin();
75 ParIterator end = bv.buffer()->par_iterator_end();
76 for ( ; it != end; ++it) {
77 bool changed_inset = false;
78 for (InsetList::iterator it2 = it->insetlist.begin();
79 it2 != it->insetlist.end(); ++it2) {
80 if (it2->inset->lyxCode() == code) {
81 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
82 if (inset->getContents() == from) {
83 inset->setContents(to);
84 //inset->setButtonLabel();
95 void InsetLabel::doDispatch(LCursor & cur, FuncRequest & cmd)
99 case LFUN_INSET_MODIFY: {
100 InsetCommandParams p;
101 InsetCommandMailer::string2params("label", cmd.argument, p);
102 if (p.getCmdName().empty()) {
106 if (p.getContents() != params().getContents())
107 changeRefsIfUnique(cur.bv(), params().getContents(),
114 InsetCommand::doDispatch(cur, cmd);
120 int InsetLabel::latex(Buffer const &, ostream & os,
121 OutputParams const &) const
123 os << escape(getCommand());
128 int InsetLabel::plaintext(Buffer const &, ostream & os,
129 OutputParams const &) const
131 os << '<' << getContents() << '>';
136 int InsetLabel::linuxdoc(Buffer const & buf, ostream & os,
137 OutputParams const & runparams) const
139 os << "<label id=\"" << sgml::cleanID(buf, runparams, getContents()) << "\" >";
144 int InsetLabel::docbook(Buffer const & buf, ostream & os,
145 OutputParams const & runparams) const
147 os << "<!-- anchor id=\"" << sgml::cleanID(buf, runparams, getContents()) << "\" -->";