]> git.lyx.org Git - lyx.git/blob - src/insets/insetlabel.C
7b2332cc0d04060c0aa71b0046293067ca9ae5d4
[lyx.git] / src / insets / insetlabel.C
1 /**
2  * \file insetlabel.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetlabel.h"
14
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
19 #include "InsetList.h"
20 #include "iterators.h"
21 #include "lyxtext.h"
22 #include "paragraph.h"
23
24 #include "frontends/LyXView.h"
25
26 #include "support/lstrings.h"
27 #include "support/lyxalgo.h"
28 #include "support/std_ostream.h"
29
30 using lyx::support::escape;
31
32 using std::string;
33 using std::ostream;
34 using std::vector;
35
36
37 InsetLabel::InsetLabel(InsetCommandParams const & p)
38         : InsetCommand(p, "label")
39 {}
40
41
42 std::auto_ptr<InsetBase> InsetLabel::clone() const
43 {
44         return std::auto_ptr<InsetBase>(new InsetLabel(params()));
45 }
46
47
48 void InsetLabel::getLabelList(Buffer const &, std::vector<string> & list) const
49 {
50         list.push_back(getContents());
51 }
52
53
54 string const InsetLabel::getScreenLabel(Buffer const &) const
55 {
56         return getContents();
57 }
58
59
60 namespace {
61
62 void changeRefsIfUnique(BufferView & bv, string const & from, string const & to)
63 {
64         // Check if the label 'from' appears more than once
65         vector<string> labels;
66         bv.buffer()->getLabelList(labels);
67
68         if (lyx::count(labels.begin(), labels.end(), from) > 1)
69                 return;
70
71         InsetBase::Code code = InsetBase::REF_CODE;
72
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();
84                                         changed_inset = true;
85                                 }
86                         }
87                 }
88         }
89 }
90
91 } // namespace anon
92
93
94 DispatchResult
95 InsetLabel::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
96 {
97         switch (cmd.action) {
98
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(),
106                                                        p.getContents());
107                 setParams(p);
108                 return DispatchResult(true, true);
109         }
110
111         default:
112                 return InsetCommand::priv_dispatch(cur, cmd);
113         }
114 }
115
116
117 int InsetLabel::latex(Buffer const &, ostream & os,
118                       OutputParams const &) const
119 {
120         os << escape(getCommand());
121         return 0;
122 }
123
124
125 int InsetLabel::plaintext(Buffer const &, ostream & os,
126                       OutputParams const &) const
127 {
128         os << '<' << getContents()  << '>';
129         return 0;
130 }
131
132
133 int InsetLabel::linuxdoc(Buffer const &, ostream & os,
134                          OutputParams const &) const
135 {
136         os << "<label id=\"" << getContents() << "\" >";
137         return 0;
138 }
139
140
141 int InsetLabel::docbook(Buffer const &, ostream & os,
142                         OutputParams const &) const
143 {
144         os << "<anchor id=\"" << getContents() << "\">";
145         return 0;
146 }