]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
bbbff3a06f4b7b5c78020e1dd8367379ad5f9f05
[lyx.git] / src / insets / InsetLabel.cpp
1 /**
2  * \file InsetLabel.cpp
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 "InsetRef.h"
16
17 #include "buffer_funcs.h"
18 #include "Buffer.h"
19 #include "BufferView.h"
20 #include "CutAndPaste.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "InsetIterator.h"
25 #include "LyXFunc.h"
26 #include "ParIterator.h"
27 #include "sgml.h"
28 #include "Text.h"
29 #include "TocBackend.h"
30
31 #include "frontends/alert.h"
32
33 #include "support/convert.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/lyxalgo.h"
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43
44 InsetLabel::InsetLabel(InsetCommandParams const & p)
45         : InsetCommand(p, "label")
46 {}
47
48
49 void InsetLabel::initView()
50 {
51         updateCommand(getParam("name"));
52 }
53
54
55 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
56 {
57         docstring const old_label = getParam("name");
58         docstring label = new_label;
59         int i = 1;
60         while (buffer().insetLabel(label)) {
61                 label = new_label + '-' + convert<docstring>(i);
62                 ++i;
63         }
64
65         if (label != new_label) {
66                 // Warn the user that the label has been changed to something else.
67                 frontend::Alert::warning(_("Label names must be unique!"),
68                         bformat(_("The label %1$s already exists,\n"
69                         "it will be changed to %2$s."), new_label, label));
70         }
71
72         buffer().undo().beginUndoGroup();
73         setParam("name", label);
74
75         if (updaterefs) {
76                 Buffer::References & refs = buffer().references(old_label);
77                 Buffer::References::iterator it = refs.begin();
78                 Buffer::References::iterator end = refs.end();
79                 for (; it != end; ++it) {
80                         buffer().undo().recordUndo(it->second);
81                         it->first->setParam("reference", label);
82                 }
83         }
84         buffer().undo().endUndoGroup();
85
86         // We need an update of the Buffer reference cache. This is achieved by
87         // updateLabels().
88         buffer().updateLabels();
89 }
90
91
92 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
93 {
94         static ParamInfo param_info_;
95         if (param_info_.empty())
96                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
97         return param_info_;
98 }
99
100
101 docstring InsetLabel::screenLabel() const
102 {
103         return screen_label_;
104 }
105
106
107 void InsetLabel::updateLabels(ParIterator const &)
108 {
109         docstring const & label = getParam("name");
110         if (buffer().insetLabel(label)) {
111                 // Problem: We already have an InsetLabel with the same name!
112                 screen_label_ = _("DUPLICATE: ") + label;
113                 return;
114         }
115         buffer().setInsetLabel(label, this);
116         screen_label_ = label;
117 }
118
119
120 void InsetLabel::addToToc(DocIterator const & cpit)
121 {
122         docstring const & label = getParam("name");
123         Toc & toc = buffer().tocBackend().toc("label");
124         if (buffer().insetLabel(label) != this) {
125                 toc.push_back(TocItem(cpit, 0, screen_label_));
126                 return;
127         }
128         toc.push_back(TocItem(cpit, 0, screen_label_));
129         Buffer::References const & refs = buffer().references(label);
130         Buffer::References::const_iterator it = refs.begin();
131         Buffer::References::const_iterator end = refs.end();
132         for (; it != end; ++it) {
133                 DocIterator const ref_pit(it->second);
134                 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
135         }
136 }
137
138
139 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
140                            FuncStatus & status) const
141 {
142         bool enabled;
143         switch (cmd.action) {
144         case LFUN_COPY_LABEL_AS_REF:
145                 enabled = true;
146                 break;
147         default:
148                 return InsetCommand::getStatus(cur, cmd, status);
149         }
150
151         status.setEnabled(enabled);
152         return true;
153 }
154
155
156 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
157 {
158         switch (cmd.action) {
159
160         case LFUN_INSET_MODIFY: {
161                 InsetCommandParams p(LABEL_CODE);
162                 // FIXME UNICODE
163                 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
164                 if (p.getCmdName().empty()) {
165                         cur.noUpdate();
166                         break;
167                 }
168                 if (p["name"] != params()["name"])
169                         updateCommand(p["name"]);
170                 break;
171         }
172
173         case LFUN_COPY_LABEL_AS_REF: {
174                 InsetCommandParams p(REF_CODE, "ref");
175                 p["reference"] = getParam("name");
176                 cap::clearSelection();
177                 cap::copyInset(cur, new InsetRef(*cur.buffer(), p), getParam("name"));
178                 break;
179         }
180
181         case LFUN_INSERT_LABEL_AS_REF: {
182                 InsetCommandParams p(REF_CODE, "ref");
183                 p["reference"] = getParam("name");
184                 string const data = InsetCommand::params2string("ref", p);
185                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
186                 break;
187         }
188
189         default:
190                 InsetCommand::doDispatch(cur, cmd);
191                 break;
192         }
193 }
194
195
196 int InsetLabel::latex(odocstream & os, OutputParams const &) const
197 {
198         os << escape(getCommand());
199         return 0;
200 }
201
202
203 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
204 {
205         docstring const str = getParam("name");
206         os << '<' << str << '>';
207         return 2 + str.size();
208 }
209
210
211 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
212 {
213         os << "<!-- anchor id=\""
214            << sgml::cleanID(buffer(), runparams, getParam("name"))
215            << "\" -->";
216         return 0;
217 }
218
219
220 } // namespace lyx