]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
Fix trailing whitespace in cpp files.
[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,
173                                                   UpdateType, TocBackend & backend) const
174 {
175         docstring const & label = getParam("name");
176         shared_ptr<Toc> toc = backend.toc("label");
177         if (buffer().insetLabel(label) != this) {
178                 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
179         } else {
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)->getTOCString(),
193                                                 output_active));
194                 }
195         }
196 }
197
198
199 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
200                            FuncStatus & status) const
201 {
202         bool enabled;
203         switch (cmd.action()) {
204         case LFUN_LABEL_INSERT_AS_REFERENCE:
205         case LFUN_LABEL_COPY_AS_REFERENCE:
206                 enabled = true;
207                 break;
208         case LFUN_INSET_MODIFY:
209                 if (cmd.getArg(0) == "changetype") {
210                         // this is handled by InsetCommand,
211                         // but not by InsetLabel.
212                         enabled = false;
213                         break;
214                 }
215         default:
216                 return InsetCommand::getStatus(cur, cmd, status);
217         }
218
219         status.setEnabled(enabled);
220         return true;
221 }
222
223
224 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
225 {
226         switch (cmd.action()) {
227
228         case LFUN_INSET_MODIFY: {
229                 // the only other option here is "changetype", and we
230                 // do not have different types.
231                 if (cmd.getArg(0) != "label") {
232                         cur.undispatched();
233                         return;
234                 }
235                 InsetCommandParams p(LABEL_CODE);
236                 // FIXME UNICODE
237                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
238                 if (p.getCmdName().empty()) {
239                         cur.noScreenUpdate();
240                         break;
241                 }
242                 if (p["name"] != params()["name"]) {
243                         // undo is handled in updateLabelAndRefs
244                         updateLabelAndRefs(p["name"], &cur);
245                 }
246                 cur.forceBufferUpdate();
247                 break;
248         }
249
250         case LFUN_LABEL_COPY_AS_REFERENCE: {
251                 InsetCommandParams p(REF_CODE, "ref");
252                 p["reference"] = getParam("name");
253                 cap::clearSelection();
254                 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
255                 break;
256         }
257
258         case LFUN_LABEL_INSERT_AS_REFERENCE: {
259                 InsetCommandParams p(REF_CODE, "ref");
260                 p["reference"] = getParam("name");
261                 string const data = InsetCommand::params2string(p);
262                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
263                 break;
264         }
265
266         default:
267                 InsetCommand::doDispatch(cur, cmd);
268                 break;
269         }
270 }
271
272
273 int InsetLabel::plaintext(odocstringstream & os,
274         OutputParams const &, size_t) const
275 {
276         docstring const str = getParam("name");
277         os << '<' << str << '>';
278         return 2 + str.size();
279 }
280
281
282 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
283 {
284         os << "<!-- anchor id=\""
285            << sgml::cleanID(buffer(), runparams, getParam("name"))
286            << "\" -->";
287         return 0;
288 }
289
290
291 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
292 {
293         // FIXME XHTML
294         // Unfortunately, the name attribute has been deprecated, so we have to use
295         // id here to get the document to validate as XHTML 1.1. This will cause a
296         // problem with some browsers, though, I'm sure. (Guess which!) So we will
297         // have to figure out what to do about this later.
298         docstring const attr = "id=\"" + html::cleanAttr(getParam("name")) + '"';
299         xs << html::CompTag("a", to_utf8(attr));
300         return docstring();
301 }
302
303
304 } // namespace lyx