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