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