]> git.lyx.org Git - lyx.git/blob - src/insets/insetlabel.C
* insettabular.[Ch]: remove remains of the 'update' mechanism,
[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 void InsetLabel::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
95 {
96         switch (cmd.action) {
97
98         case LFUN_INSET_MODIFY: {
99                 InsetCommandParams p;
100                 InsetCommandMailer::string2params("label", cmd.argument, p);
101                 if (p.getCmdName().empty()) {
102                         cur.notdispatched();
103                         break;
104                 }
105                 if (p.getContents() != params().getContents())
106                         changeRefsIfUnique(cur.bv(), params().getContents(),
107                                                        p.getContents());
108                 setParams(p);
109                 break;
110         }
111
112         default:
113                 InsetCommand::priv_dispatch(cur, cmd);
114                 break;
115         }
116 }
117
118
119 int InsetLabel::latex(Buffer const &, ostream & os,
120                       OutputParams const &) const
121 {
122         os << escape(getCommand());
123         return 0;
124 }
125
126
127 int InsetLabel::plaintext(Buffer const &, ostream & os,
128                       OutputParams const &) const
129 {
130         os << '<' << getContents()  << '>';
131         return 0;
132 }
133
134
135 int InsetLabel::linuxdoc(Buffer const &, ostream & os,
136                          OutputParams const &) const
137 {
138         os << "<label id=\"" << getContents() << "\" >";
139         return 0;
140 }
141
142
143 int InsetLabel::docbook(Buffer const &, ostream & os,
144                         OutputParams const &) const
145 {
146         os << "<anchor id=\"" << getContents() << "\">";
147         return 0;
148 }