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