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