]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
ce97f3a84c4dc144c70620ea0341b066095cbe25
[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 = it->first->asInsetMath()->asHullInset();
90                                 mi->asRefInset()->changeTarget(label);
91                         } else {
92                                 InsetCommand * ref = it->first->asInsetCommand();
93                                 ref->setParam("reference", label);
94                         }
95                 }
96         }
97         buffer().undo().endUndoGroup();
98 }
99
100
101 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
102 {
103         static ParamInfo param_info_;
104         if (param_info_.empty())
105                 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
106                                 ParamInfo::HANDLING_ESCAPE);
107         return param_info_;
108 }
109
110
111 docstring InsetLabel::screenLabel() const
112 {
113         return screen_label_;
114 }
115
116
117 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
118 {
119         docstring const & label = getParam("name");
120         if (buffer().insetLabel(label)) {
121                 // Problem: We already have an InsetLabel with the same name!
122                 screen_label_ = _("DUPLICATE: ") + label;
123                 return;
124         }
125         buffer().setInsetLabel(label, this);
126         screen_label_ = label;
127
128         if (utype) {
129                 // save info on the active counter
130                 Counters const & cnts = 
131                         buffer().masterBuffer()->params().documentClass().counters();
132                 active_counter_ = cnts.currentCounter();
133                 Language const * lang = par->getParLanguage(buffer().params());
134                 if (lang && !active_counter_.empty()) {
135                         counter_value_ = cnts.theCounter(active_counter_, lang->code());
136                         pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
137                 } else {
138                         counter_value_ = from_ascii("??");
139                         pretty_counter_ = from_ascii("??");
140                 }
141         }
142 }
143
144
145 void InsetLabel::addToToc(DocIterator const & cpit)
146 {
147         docstring const & label = getParam("name");
148         Toc & toc = buffer().tocBackend().toc("label");
149         if (buffer().insetLabel(label) != this) {
150                 toc.push_back(TocItem(cpit, 0, screen_label_));
151                 return;
152         }
153         toc.push_back(TocItem(cpit, 0, screen_label_));
154         Buffer::References const & refs = buffer().references(label);
155         Buffer::References::const_iterator it = refs.begin();
156         Buffer::References::const_iterator end = refs.end();
157         for (; it != end; ++it) {
158                 DocIterator const ref_pit(it->second);
159                 if (it->first->lyxCode() == MATH_REF_CODE)
160                         toc.push_back(TocItem(ref_pit, 1,
161                                 it->first->asInsetMath()->asHullInset()->asRefInset()
162                                         ->screenLabel()));
163                 else
164                         toc.push_back(TocItem(ref_pit, 1,
165                                 static_cast<InsetRef *>(it->first)->screenLabel()));
166         }
167 }
168
169
170 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
171                            FuncStatus & status) const
172 {
173         bool enabled;
174         switch (cmd.action()) {
175         case LFUN_LABEL_INSERT_AS_REF:
176         case LFUN_LABEL_COPY_AS_REF:
177                 enabled = true;
178                 break;
179         default:
180                 return InsetCommand::getStatus(cur, cmd, status);
181         }
182
183         status.setEnabled(enabled);
184         return true;
185 }
186
187
188 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
189 {
190         switch (cmd.action()) {
191
192         case LFUN_INSET_MODIFY: {
193                 InsetCommandParams p(LABEL_CODE);
194                 // FIXME UNICODE
195                 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
196                 if (p.getCmdName().empty()) {
197                         cur.noScreenUpdate();
198                         break;
199                 }
200                 if (p["name"] != params()["name"])
201                         updateCommand(p["name"]);
202                 cur.forceBufferUpdate();
203                 break;
204         }
205
206         case LFUN_LABEL_COPY_AS_REF: {
207                 InsetCommandParams p(REF_CODE, "ref");
208                 p["reference"] = getParam("name");
209                 cap::clearSelection();
210                 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
211                 break;
212         }
213
214         case LFUN_LABEL_INSERT_AS_REF: {
215                 InsetCommandParams p(REF_CODE, "ref");
216                 p["reference"] = getParam("name");
217                 string const data = InsetCommand::params2string("ref", p);
218                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
219                 break;
220         }
221
222         default:
223                 InsetCommand::doDispatch(cur, cmd);
224                 break;
225         }
226 }
227
228
229 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
230 {
231         docstring const str = getParam("name");
232         os << '<' << str << '>';
233         return 2 + str.size();
234 }
235
236
237 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
238 {
239         os << "<!-- anchor id=\""
240            << sgml::cleanID(buffer(), runparams, getParam("name"))
241            << "\" -->";
242         return 0;
243 }
244
245
246 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
247 {
248         // FIXME XHTML
249         // Unfortunately, the name attribute has been deprecated, so we have to use
250         // id here to get the document to validate as XHTML 1.1. This will cause a 
251         // problem with some browsers, though, I'm sure. (Guess which!) So we will
252         // have to figure out what to do about this later. 
253         string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
254         xs << html::CompTag("a", attr);
255         return docstring();
256 }
257
258
259 } // namespace lyx