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"
20 #include "iterators.h"
22 #include "paragraph.h"
24 #include "frontends/LyXView.h"
26 #include "support/lstrings.h"
27 #include "support/lyxalgo.h"
28 #include "support/std_ostream.h"
30 using lyx::support::escape;
37 InsetLabel::InsetLabel(InsetCommandParams const & p)
38 : InsetCommand(p, "label")
42 std::auto_ptr<InsetBase> InsetLabel::clone() const
44 return std::auto_ptr<InsetBase>(new InsetLabel(params()));
48 void InsetLabel::getLabelList(Buffer const &, std::vector<string> & list) const
50 list.push_back(getContents());
54 string const InsetLabel::getScreenLabel(Buffer const &) const
62 void changeRefsIfUnique(BufferView & bv, string const & from, string const & to)
64 // Check if the label 'from' appears more than once
65 vector<string> labels;
66 bv.buffer()->getLabelList(labels);
68 if (lyx::count(labels.begin(), labels.end(), from) > 1)
71 InsetBase::Code code = InsetBase::REF_CODE;
73 ParIterator it = bv.buffer()->par_iterator_begin();
74 ParIterator end = bv.buffer()->par_iterator_end();
75 for ( ; it != end; ++it) {
76 bool changed_inset = false;
77 for (InsetList::iterator it2 = it->insetlist.begin();
78 it2 != it->insetlist.end(); ++it2) {
79 if (it2->inset->lyxCode() == code) {
80 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
81 if (inset->getContents() == from) {
82 inset->setContents(to);
83 //inset->setButtonLabel();
95 InsetLabel::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
99 case LFUN_INSET_MODIFY: {
100 InsetCommandParams p;
101 InsetCommandMailer::string2params("label", cmd.argument, p);
102 if (p.getCmdName().empty())
103 return DispatchResult(false);
104 if (p.getContents() != params().getContents())
105 changeRefsIfUnique(cur.bv(), params().getContents(),
108 return DispatchResult(true, true);
112 return InsetCommand::priv_dispatch(cur, cmd);
117 int InsetLabel::latex(Buffer const &, ostream & os,
118 OutputParams const &) const
120 os << escape(getCommand());
125 int InsetLabel::plaintext(Buffer const &, ostream & os,
126 OutputParams const &) const
128 os << '<' << getContents() << '>';
133 int InsetLabel::linuxdoc(Buffer const &, ostream & os,
134 OutputParams const &) const
136 os << "<label id=\"" << getContents() << "\" >";
141 int InsetLabel::docbook(Buffer const &, ostream & os,
142 OutputParams const &) const
144 os << "<anchor id=\"" << getContents() << "\">";