]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
3d25aff1020faf4af704d0bda7f47e77df2fef98
[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_LABEL_INSERT_AS_REF:
145         case LFUN_LABEL_COPY_AS_REF:
146                 enabled = true;
147                 break;
148         default:
149                 return InsetCommand::getStatus(cur, cmd, status);
150         }
151
152         status.setEnabled(enabled);
153         return true;
154 }
155
156
157 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
158 {
159         switch (cmd.action) {
160
161         case LFUN_INSET_MODIFY: {
162                 InsetCommandParams p(LABEL_CODE);
163                 // FIXME UNICODE
164                 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
165                 if (p.getCmdName().empty()) {
166                         cur.noUpdate();
167                         break;
168                 }
169                 if (p["name"] != params()["name"])
170                         updateCommand(p["name"]);
171                 break;
172         }
173
174         case LFUN_LABEL_COPY_AS_REF: {
175                 InsetCommandParams p(REF_CODE, "ref");
176                 p["reference"] = getParam("name");
177                 cap::clearSelection();
178                 cap::copyInset(cur, new InsetRef(*cur.buffer(), p), getParam("name"));
179                 break;
180         }
181
182         case LFUN_LABEL_INSERT_AS_REF: {
183                 InsetCommandParams p(REF_CODE, "ref");
184                 p["reference"] = getParam("name");
185                 string const data = InsetCommand::params2string("ref", p);
186                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
187                 break;
188         }
189
190         default:
191                 InsetCommand::doDispatch(cur, cmd);
192                 break;
193         }
194 }
195
196
197 int InsetLabel::latex(odocstream & os, OutputParams const &) const
198 {
199         os << escape(getCommand());
200         return 0;
201 }
202
203
204 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
205 {
206         docstring const str = getParam("name");
207         os << '<' << str << '>';
208         return 2 + str.size();
209 }
210
211
212 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
213 {
214         os << "<!-- anchor id=\""
215            << sgml::cleanID(buffer(), runparams, getParam("name"))
216            << "\" -->";
217         return 0;
218 }
219
220
221 } // namespace lyx