]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
Enable dissolve in undefined Flex inset context menu
[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         bool ambiguous = false;
71         while (buffer().activeLabel(label)) {
72                 label = new_label + '-' + convert<docstring>(i);
73                 ++i;
74                 ambiguous = true;
75         }
76         if (ambiguous) {
77                 // Warn the user that the label has been changed to something else.
78                 frontend::Alert::warning(_("Label names must be unique!"),
79                         bformat(_("The label %1$s already exists,\n"
80                         "it will be changed to %2$s."), new_label, label));
81         }
82 }
83
84
85 void InsetLabel::updateLabel(docstring const & new_label)
86 {
87         docstring label = new_label;
88         uniqueLabel(label);
89         setParam("name", label);
90 }
91
92
93 void InsetLabel::updateLabelAndRefs(docstring const & new_label,
94                 Cursor * cursor)
95 {
96         docstring const old_label = getParam("name");
97         docstring label = new_label;
98         uniqueLabel(label);
99         if (label == old_label)
100                 return;
101
102         // This handles undo groups automagically
103         UndoGroupHelper ugh(&buffer());
104         if (cursor)
105                 cursor->recordUndo();
106         setParam("name", label);
107         updateReferences(old_label, label);
108 }
109
110
111 void InsetLabel::updateReferences(docstring const & old_label,
112                 docstring const & new_label)
113 {
114         UndoGroupHelper ugh;
115         Buffer::References const & refs = buffer().references(old_label);
116         Buffer::References::const_iterator it = refs.begin();
117         Buffer::References::const_iterator end = refs.end();
118         for (; it != end; ++it) {
119                 ugh.resetBuffer(it->second.buffer());
120                 it->second.buffer()->undo().recordUndo(CursorData(it->second));
121                 if (it->first->lyxCode() == MATH_REF_CODE) {
122                         InsetMathRef * mi = it->first->asInsetMath()->asRefInset();
123                         mi->changeTarget(new_label);
124                 } else {
125                         InsetCommand * ref = it->first->asInsetCommand();
126                         ref->setParam("reference", new_label);
127                 }
128         }
129 }
130
131
132 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
133 {
134         static ParamInfo param_info_;
135         if (param_info_.empty())
136                 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
137                                 ParamInfo::HANDLING_ESCAPE);
138         return param_info_;
139 }
140
141
142 docstring InsetLabel::screenLabel() const
143 {
144         return screen_label_;
145 }
146
147
148 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
149 {
150         docstring const & label = getParam("name");
151
152         // Check if this one is deleted (ct)
153         Paragraph const & para = par.paragraph();
154         bool active = !para.isDeleted(par.pos());
155         // If not, check whether we are in a deleted inset
156         if (active) {
157                 for (size_type sl = 0 ; sl < par.depth() ; ++sl) {
158                         Paragraph const & outer_par = par[sl].paragraph();
159                         if (outer_par.isDeleted(par[sl].pos())) {
160                                 active = false;
161                                 break;
162                         }
163                 }
164         }
165
166         if (buffer().activeLabel(label) && active) {
167                 // Problem: We already have an active InsetLabel with the same name!
168                 screen_label_ = _("DUPLICATE: ") + label;
169                 return;
170         }
171         buffer().setInsetLabel(label, this, active);
172         screen_label_ = label;
173
174         if (utype == OutputUpdate) {
175                 // save info on the active counter
176                 Counters const & cnts =
177                         buffer().masterBuffer()->params().documentClass().counters();
178                 active_counter_ = cnts.currentCounter();
179                 Language const * lang = par->getParLanguage(buffer().params());
180                 if (lang && !active_counter_.empty()) {
181                         counter_value_ = cnts.theCounter(active_counter_, lang->code());
182                         pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
183                 } else {
184                         counter_value_ = from_ascii("#");
185                         pretty_counter_ = from_ascii("#");
186                 }
187         }
188 }
189
190
191 void InsetLabel::addToToc(DocIterator const & cpit, bool output_active,
192                                                   UpdateType, TocBackend & backend) const
193 {
194         docstring const & label = getParam("name");
195         // inactive labels get a cross mark
196         if (buffer().insetLabel(label, true) != this)
197                 output_active = false;
198
199         // We put both  active and inactive labels to the outliner
200         shared_ptr<Toc> toc = backend.toc("label");
201         toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
202         // The refs get assigned only to the active label. If no active one exists,
203         // assign the (BROKEN) refs to the first inactive one.
204         if (buffer().insetLabel(label, true) == this || !buffer().activeLabel(label)) {
205                 for (auto const & p : buffer().references(label)) {
206                         DocIterator const ref_pit(p.second);
207                         if (p.first->lyxCode() == MATH_REF_CODE)
208                                 toc->push_back(TocItem(ref_pit, 1,
209                                                 p.first->asInsetMath()->asRefInset()->screenLabel(),
210                                                 output_active));
211                         else
212                                 toc->push_back(TocItem(ref_pit, 1,
213                                                 static_cast<InsetRef *>(p.first)->getTOCString(),
214                                                 output_active));
215                 }
216         }
217 }
218
219
220 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
221                            FuncStatus & status) const
222 {
223         bool enabled;
224         switch (cmd.action()) {
225         case LFUN_LABEL_INSERT_AS_REFERENCE:
226         case LFUN_LABEL_COPY_AS_REFERENCE:
227                 enabled = true;
228                 break;
229         case LFUN_INSET_MODIFY:
230                 if (cmd.getArg(0) == "changetype") {
231                         // this is handled by InsetCommand,
232                         // but not by InsetLabel.
233                         enabled = false;
234                         break;
235                 }
236                 // no "changetype":
237                 // fall through
238         default:
239                 return InsetCommand::getStatus(cur, cmd, status);
240         }
241
242         status.setEnabled(enabled);
243         return true;
244 }
245
246
247 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
248 {
249         switch (cmd.action()) {
250
251         case LFUN_INSET_MODIFY: {
252                 // the only other option here is "changetype", and we
253                 // do not have different types.
254                 if (cmd.getArg(0) != "label") {
255                         cur.undispatched();
256                         return;
257                 }
258                 InsetCommandParams p(LABEL_CODE);
259                 // FIXME UNICODE
260                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
261                 if (p.getCmdName().empty()) {
262                         cur.noScreenUpdate();
263                         break;
264                 }
265                 if (p["name"] != params()["name"]) {
266                         // undo is handled in updateLabelAndRefs
267                         updateLabelAndRefs(p["name"], &cur);
268                 }
269                 cur.forceBufferUpdate();
270                 break;
271         }
272
273         case LFUN_LABEL_COPY_AS_REFERENCE: {
274                 InsetCommandParams p(REF_CODE, "ref");
275                 p["reference"] = getParam("name");
276                 cap::clearSelection();
277                 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
278                 break;
279         }
280
281         case LFUN_LABEL_INSERT_AS_REFERENCE: {
282                 InsetCommandParams p(REF_CODE, "ref");
283                 p["reference"] = getParam("name");
284                 string const data = InsetCommand::params2string(p);
285                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
286                 break;
287         }
288
289         default:
290                 InsetCommand::doDispatch(cur, cmd);
291                 break;
292         }
293 }
294
295
296 int InsetLabel::plaintext(odocstringstream & os,
297         OutputParams const &, size_t) const
298 {
299         docstring const str = getParam("name");
300         os << '<' << str << '>';
301         return 2 + str.size();
302 }
303
304
305 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
306 {
307         os << "<!-- anchor id=\""
308            << sgml::cleanID(buffer(), runparams, getParam("name"))
309            << "\" -->";
310         return 0;
311 }
312
313
314 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
315 {
316         // FIXME XHTML
317         // Unfortunately, the name attribute has been deprecated, so we have to use
318         // id here to get the document to validate as XHTML 1.1. This will cause a
319         // problem with some browsers, though, I'm sure. (Guess which!) So we will
320         // have to figure out what to do about this later.
321         docstring const attr = "id=\"" + html::cleanAttr(getParam("name")) + '"';
322         xs << html::CompTag("a", to_utf8(attr));
323         return docstring();
324 }
325
326
327 } // namespace lyx