]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
InsetCommand ctor: Pass 'Buffer *'
[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 "BufferView.h"
20 #include "CutAndPaste.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "InsetIterator.h"
25 #include "LyXFunc.h"
26 #include "output_xhtml.h"
27 #include "ParIterator.h"
28 #include "sgml.h"
29 #include "Text.h"
30 #include "TocBackend.h"
31
32 #include "frontends/alert.h"
33
34 #include "support/convert.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
37 #include "support/lyxalgo.h"
38
39 using namespace std;
40 using namespace lyx::support;
41
42 namespace lyx {
43
44
45 InsetLabel::InsetLabel(Buffer * buf, InsetCommandParams const & p)
46         : InsetCommand(buf, p, "label")
47 {}
48
49
50 void InsetLabel::initView()
51 {
52         updateCommand(getParam("name"));
53 }
54
55
56 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
57 {
58         docstring const old_label = getParam("name");
59         docstring label = new_label;
60         int i = 1;
61         while (buffer().insetLabel(label)) {
62                 label = new_label + '-' + convert<docstring>(i);
63                 ++i;
64         }
65
66         if (label != new_label) {
67                 // Warn the user that the label has been changed to something else.
68                 frontend::Alert::warning(_("Label names must be unique!"),
69                         bformat(_("The label %1$s already exists,\n"
70                         "it will be changed to %2$s."), new_label, label));
71         }
72
73         buffer().undo().beginUndoGroup();
74         setParam("name", label);
75
76         if (updaterefs) {
77                 Buffer::References & refs = buffer().references(old_label);
78                 Buffer::References::iterator it = refs.begin();
79                 Buffer::References::iterator end = refs.end();
80                 for (; it != end; ++it) {
81                         buffer().undo().recordUndo(it->second);
82                         it->first->setParam("reference", label);
83                 }
84         }
85         buffer().undo().endUndoGroup();
86
87         // We need an update of the Buffer reference cache. This is achieved by
88         // updateLabels().
89         buffer().updateLabels();
90 }
91
92
93 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
94 {
95         static ParamInfo param_info_;
96         if (param_info_.empty())
97                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
98         return param_info_;
99 }
100
101
102 docstring InsetLabel::screenLabel() const
103 {
104         return screen_label_;
105 }
106
107
108 void InsetLabel::updateLabels(ParIterator const &)
109 {
110         docstring const & label = getParam("name");
111         if (buffer().insetLabel(label)) {
112                 // Problem: We already have an InsetLabel with the same name!
113                 screen_label_ = _("DUPLICATE: ") + label;
114                 return;
115         }
116         buffer().setInsetLabel(label, this);
117         screen_label_ = label;
118 }
119
120
121 void InsetLabel::addToToc(DocIterator const & cpit)
122 {
123         docstring const & label = getParam("name");
124         Toc & toc = buffer().tocBackend().toc("label");
125         if (buffer().insetLabel(label) != this) {
126                 toc.push_back(TocItem(cpit, 0, screen_label_));
127                 return;
128         }
129         toc.push_back(TocItem(cpit, 0, screen_label_));
130         Buffer::References const & refs = buffer().references(label);
131         Buffer::References::const_iterator it = refs.begin();
132         Buffer::References::const_iterator end = refs.end();
133         for (; it != end; ++it) {
134                 DocIterator const ref_pit(it->second);
135                 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
136         }
137 }
138
139
140 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
141                            FuncStatus & status) const
142 {
143         bool enabled;
144         switch (cmd.action) {
145         case LFUN_LABEL_INSERT_AS_REF:
146         case LFUN_LABEL_COPY_AS_REF:
147                 enabled = true;
148                 break;
149         default:
150                 return InsetCommand::getStatus(cur, cmd, status);
151         }
152
153         status.setEnabled(enabled);
154         return true;
155 }
156
157
158 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
159 {
160         switch (cmd.action) {
161
162         case LFUN_INSET_MODIFY: {
163                 InsetCommandParams p(LABEL_CODE);
164                 // FIXME UNICODE
165                 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
166                 if (p.getCmdName().empty()) {
167                         cur.noUpdate();
168                         break;
169                 }
170                 if (p["name"] != params()["name"])
171                         updateCommand(p["name"]);
172                 break;
173         }
174
175         case LFUN_LABEL_COPY_AS_REF: {
176                 InsetCommandParams p(REF_CODE, "ref");
177                 p["reference"] = getParam("name");
178                 cap::clearSelection();
179                 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
180                 break;
181         }
182
183         case LFUN_LABEL_INSERT_AS_REF: {
184                 InsetCommandParams p(REF_CODE, "ref");
185                 p["reference"] = getParam("name");
186                 string const data = InsetCommand::params2string("ref", p);
187                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
188                 break;
189         }
190
191         default:
192                 InsetCommand::doDispatch(cur, cmd);
193                 break;
194         }
195 }
196
197
198 int InsetLabel::latex(odocstream & os, OutputParams const &) const
199 {
200         os << escape(getCommand());
201         return 0;
202 }
203
204
205 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
206 {
207         docstring const str = getParam("name");
208         os << '<' << str << '>';
209         return 2 + str.size();
210 }
211
212
213 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
214 {
215         os << "<!-- anchor id=\""
216            << sgml::cleanID(buffer(), runparams, getParam("name"))
217            << "\" -->";
218         return 0;
219 }
220
221
222 docstring InsetLabel::xhtml(odocstream & os, OutputParams const &) const
223 {
224         os << "<a name=\"" << html::htmlize(getParam("name")) << "\"></a>";
225         return docstring();
226 }
227
228 } // namespace lyx