]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
840a6341d5df21f03e3112a57d9d22a23acc4b15
[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 "BufferParams.h"
20 #include "BufferView.h"
21 #include "CutAndPaste.h"
22 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
26 #include "Language.h"
27 #include "LyXFunc.h"
28 #include "output_xhtml.h"
29 #include "ParIterator.h"
30 #include "sgml.h"
31 #include "Text.h"
32 #include "TextClass.h"
33 #include "TocBackend.h"
34
35 #include "frontends/alert.h"
36
37 #include "support/convert.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40 #include "support/lyxalgo.h"
41
42 using namespace std;
43 using namespace lyx::support;
44
45 namespace lyx {
46
47
48 InsetLabel::InsetLabel(Buffer * buf, InsetCommandParams const & p)
49         : InsetCommand(buf, p, "label")
50 {}
51
52
53 void InsetLabel::initView()
54 {
55         updateCommand(getParam("name"));
56 }
57
58
59 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
60 {
61         docstring const old_label = getParam("name");
62         docstring label = new_label;
63         int i = 1;
64         while (buffer().insetLabel(label)) {
65                 label = new_label + '-' + convert<docstring>(i);
66                 ++i;
67         }
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 be changed to %2$s."), new_label, label));
74         }
75
76         buffer().undo().beginUndoGroup();
77         setParam("name", label);
78
79         if (updaterefs) {
80                 Buffer::References & refs = buffer().references(old_label);
81                 Buffer::References::iterator it = refs.begin();
82                 Buffer::References::iterator end = refs.end();
83                 for (; it != end; ++it) {
84                         buffer().undo().recordUndo(it->second);
85                         it->first->setParam("reference", label);
86                 }
87         }
88         buffer().undo().endUndoGroup();
89
90         // We need an update of the Buffer reference cache. This is achieved by
91         // updateLabels().
92         buffer().updateLabels();
93 }
94
95
96 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
97 {
98         static ParamInfo param_info_;
99         if (param_info_.empty())
100                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
101         return param_info_;
102 }
103
104
105 docstring InsetLabel::screenLabel() const
106 {
107         return screen_label_;
108 }
109
110
111 void InsetLabel::updateLabels(ParIterator const & par, bool out)
112 {
113         docstring const & label = getParam("name");
114         if (buffer().insetLabel(label)) {
115                 // Problem: We already have an InsetLabel with the same name!
116                 screen_label_ = _("DUPLICATE: ") + label;
117                 return;
118         }
119         buffer().setInsetLabel(label, this);
120         screen_label_ = label;
121
122         if (out) {
123                 // save info on the active counter
124                 Counters const & cnts = 
125                         buffer().masterBuffer()->params().documentClass().counters();
126                 active_counter_ = cnts.currentCounter();
127                 Language const * lang = par->getParLanguage(buffer().params());
128                 if (lang && !active_counter_.empty())
129                         counter_value_ = cnts.theCounter(active_counter_, lang->code());
130                 else 
131                         counter_value_ = _("(unknown)");
132         }
133 }
134
135
136 void InsetLabel::addToToc(DocIterator const & cpit)
137 {
138         docstring const & label = getParam("name");
139         Toc & toc = buffer().tocBackend().toc("label");
140         if (buffer().insetLabel(label) != this) {
141                 toc.push_back(TocItem(cpit, 0, screen_label_));
142                 return;
143         }
144         toc.push_back(TocItem(cpit, 0, screen_label_));
145         Buffer::References const & refs = buffer().references(label);
146         Buffer::References::const_iterator it = refs.begin();
147         Buffer::References::const_iterator end = refs.end();
148         for (; it != end; ++it) {
149                 DocIterator const ref_pit(it->second);
150                 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
151         }
152 }
153
154
155 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
156                            FuncStatus & status) const
157 {
158         bool enabled;
159         switch (cmd.action) {
160         case LFUN_LABEL_INSERT_AS_REF:
161         case LFUN_LABEL_COPY_AS_REF:
162                 enabled = true;
163                 break;
164         default:
165                 return InsetCommand::getStatus(cur, cmd, status);
166         }
167
168         status.setEnabled(enabled);
169         return true;
170 }
171
172
173 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
174 {
175         switch (cmd.action) {
176
177         case LFUN_INSET_MODIFY: {
178                 InsetCommandParams p(LABEL_CODE);
179                 // FIXME UNICODE
180                 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
181                 if (p.getCmdName().empty()) {
182                         cur.noUpdate();
183                         break;
184                 }
185                 if (p["name"] != params()["name"])
186                         updateCommand(p["name"]);
187                 break;
188         }
189
190         case LFUN_LABEL_COPY_AS_REF: {
191                 InsetCommandParams p(REF_CODE, "ref");
192                 p["reference"] = getParam("name");
193                 cap::clearSelection();
194                 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
195                 break;
196         }
197
198         case LFUN_LABEL_INSERT_AS_REF: {
199                 InsetCommandParams p(REF_CODE, "ref");
200                 p["reference"] = getParam("name");
201                 string const data = InsetCommand::params2string("ref", p);
202                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
203                 break;
204         }
205
206         default:
207                 InsetCommand::doDispatch(cur, cmd);
208                 break;
209         }
210 }
211
212
213 int InsetLabel::latex(odocstream & os, OutputParams const &) const
214 {
215         os << escape(getCommand());
216         return 0;
217 }
218
219
220 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
221 {
222         docstring const str = getParam("name");
223         os << '<' << str << '>';
224         return 2 + str.size();
225 }
226
227
228 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
229 {
230         os << "<!-- anchor id=\""
231            << sgml::cleanID(buffer(), runparams, getParam("name"))
232            << "\" -->";
233         return 0;
234 }
235
236
237 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
238 {
239         // FIXME XHTML
240         // Unfortunately, the name attribute has been deprecated, so we have to use
241         // id here to get the document to validate as XHTML 1.1. This will cause a 
242         // problem with some browsers, though, I'm sure. (Guess which!) So we will
243         // have to figure out what to do about this later. 
244         string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
245         xs << html::CompTag("a", attr);
246         return docstring();
247 }
248
249
250 } // namespace lyx