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