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