]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
Add suffixed lyxconvert to cmake build.
[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         UndoGroupHelper ugh;
113         Buffer::References const & refs = buffer().references(old_label);
114         Buffer::References::const_iterator it = refs.begin();
115         Buffer::References::const_iterator end = refs.end();
116         for (; it != end; ++it) {
117                 ugh.resetBuffer(it->second.buffer());
118                 it->second.buffer()->undo().recordUndo(CursorData(it->second));
119                 if (it->first->lyxCode() == MATH_REF_CODE) {
120                         InsetMathRef * mi = it->first->asInsetMath()->asRefInset();
121                         mi->changeTarget(new_label);
122                 } else {
123                         InsetCommand * ref = it->first->asInsetCommand();
124                         ref->setParam("reference", new_label);
125                 }
126         }
127 }
128
129
130 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
131 {
132         static ParamInfo param_info_;
133         if (param_info_.empty())
134                 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
135                                 ParamInfo::HANDLING_ESCAPE);
136         return param_info_;
137 }
138
139
140 docstring InsetLabel::screenLabel() const
141 {
142         return screen_label_;
143 }
144
145
146 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
147 {
148         docstring const & label = getParam("name");
149         if (buffer().insetLabel(label)) {
150                 // Problem: We already have an InsetLabel with the same name!
151                 screen_label_ = _("DUPLICATE: ") + label;
152                 return;
153         }
154         buffer().setInsetLabel(label, this);
155         screen_label_ = label;
156
157         if (utype == OutputUpdate) {
158                 // save info on the active counter
159                 Counters const & cnts =
160                         buffer().masterBuffer()->params().documentClass().counters();
161                 active_counter_ = cnts.currentCounter();
162                 Language const * lang = par->getParLanguage(buffer().params());
163                 if (lang && !active_counter_.empty()) {
164                         counter_value_ = cnts.theCounter(active_counter_, lang->code());
165                         pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
166                 } else {
167                         counter_value_ = from_ascii("#");
168                         pretty_counter_ = from_ascii("#");
169                 }
170         }
171 }
172
173
174 void InsetLabel::addToToc(DocIterator const & cpit, bool output_active,
175                                                   UpdateType, TocBackend & backend) const
176 {
177         docstring const & label = getParam("name");
178         shared_ptr<Toc> toc = backend.toc("label");
179         if (buffer().insetLabel(label) != this) {
180                 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
181         } else {
182                 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
183                 Buffer::References const & refs = buffer().references(label);
184                 Buffer::References::const_iterator it = refs.begin();
185                 Buffer::References::const_iterator end = refs.end();
186                 for (; it != end; ++it) {
187                         DocIterator const ref_pit(it->second);
188                         if (it->first->lyxCode() == MATH_REF_CODE)
189                                 toc->push_back(TocItem(ref_pit, 1,
190                                                 it->first->asInsetMath()->asRefInset()->screenLabel(),
191                                                 output_active));
192                         else
193                                 toc->push_back(TocItem(ref_pit, 1,
194                                                 static_cast<InsetRef *>(it->first)->getTOCString(),
195                                                 output_active));
196                 }
197         }
198 }
199
200
201 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
202                            FuncStatus & status) const
203 {
204         bool enabled;
205         switch (cmd.action()) {
206         case LFUN_LABEL_INSERT_AS_REFERENCE:
207         case LFUN_LABEL_COPY_AS_REFERENCE:
208                 enabled = true;
209                 break;
210         case LFUN_INSET_MODIFY:
211                 if (cmd.getArg(0) == "changetype") {
212                         // this is handled by InsetCommand,
213                         // but not by InsetLabel.
214                         enabled = false;
215                         break;
216                 }
217                 // no "changetype":
218                 // fall through
219         default:
220                 return InsetCommand::getStatus(cur, cmd, status);
221         }
222
223         status.setEnabled(enabled);
224         return true;
225 }
226
227
228 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
229 {
230         switch (cmd.action()) {
231
232         case LFUN_INSET_MODIFY: {
233                 // the only other option here is "changetype", and we
234                 // do not have different types.
235                 if (cmd.getArg(0) != "label") {
236                         cur.undispatched();
237                         return;
238                 }
239                 InsetCommandParams p(LABEL_CODE);
240                 // FIXME UNICODE
241                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
242                 if (p.getCmdName().empty()) {
243                         cur.noScreenUpdate();
244                         break;
245                 }
246                 if (p["name"] != params()["name"]) {
247                         // undo is handled in updateLabelAndRefs
248                         updateLabelAndRefs(p["name"], &cur);
249                 }
250                 cur.forceBufferUpdate();
251                 break;
252         }
253
254         case LFUN_LABEL_COPY_AS_REFERENCE: {
255                 InsetCommandParams p(REF_CODE, "ref");
256                 p["reference"] = getParam("name");
257                 cap::clearSelection();
258                 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
259                 break;
260         }
261
262         case LFUN_LABEL_INSERT_AS_REFERENCE: {
263                 InsetCommandParams p(REF_CODE, "ref");
264                 p["reference"] = getParam("name");
265                 string const data = InsetCommand::params2string(p);
266                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
267                 break;
268         }
269
270         default:
271                 InsetCommand::doDispatch(cur, cmd);
272                 break;
273         }
274 }
275
276
277 int InsetLabel::plaintext(odocstringstream & os,
278         OutputParams const &, size_t) const
279 {
280         docstring const str = getParam("name");
281         os << '<' << str << '>';
282         return 2 + str.size();
283 }
284
285
286 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
287 {
288         os << "<!-- anchor id=\""
289            << sgml::cleanID(buffer(), runparams, getParam("name"))
290            << "\" -->";
291         return 0;
292 }
293
294
295 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
296 {
297         // FIXME XHTML
298         // Unfortunately, the name attribute has been deprecated, so we have to use
299         // id here to get the document to validate as XHTML 1.1. This will cause a
300         // problem with some browsers, though, I'm sure. (Guess which!) So we will
301         // have to figure out what to do about this later.
302         docstring const attr = "id=\"" + html::cleanAttr(getParam("name")) + '"';
303         xs << html::CompTag("a", to_utf8(attr));
304         return docstring();
305 }
306
307
308 } // namespace lyx