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