]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
use more flexible default for now...
[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, "label")
53 {
54         screen_label_ = p["name"];
55 }
56
57
58 void InsetLabel::initView()
59 {
60         updateCommand(getParam("name"));
61 }
62
63
64 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
65 {
66         docstring const old_label = getParam("name");
67         docstring label = new_label;
68         int i = 1;
69         while (buffer().insetLabel(label)) {
70                 label = new_label + '-' + convert<docstring>(i);
71                 ++i;
72         }
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         buffer().undo().beginUndoGroup();
82         setParam("name", label);
83
84         if (updaterefs) {
85                 Buffer::References & refs = buffer().references(old_label);
86                 Buffer::References::iterator it = refs.begin();
87                 Buffer::References::iterator end = refs.end();
88                 for (; it != end; ++it) {
89                         buffer().undo().recordUndo(it->second);
90                         if (it->first->lyxCode() == MATH_REF_CODE) {
91                                 InsetMathHull * mi =
92                                         static_cast<InsetMathHull *>(it->first);
93                                 mi->asRefInset()->changeTarget(label);
94                         } else {
95                                 InsetCommand * ref =
96                                         static_cast<InsetCommand *>(it->first);
97                                 ref->setParam("reference", label);
98                         }
99                 }
100         }
101         buffer().undo().endUndoGroup();
102
103         // We need an update of the Buffer reference cache. This is achieved by
104         // updateBuffer().
105         buffer().updateBuffer();
106         screen_label_ = label;
107 }
108
109
110 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
111 {
112         static ParamInfo param_info_;
113         if (param_info_.empty())
114                 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
115                                 ParamInfo::HANDLING_ESCAPE);
116         return param_info_;
117 }
118
119
120 docstring InsetLabel::screenLabel() const
121 {
122         return screen_label_;
123 }
124
125
126 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
127 {
128         docstring const & label = getParam("name");
129         if (buffer().insetLabel(label)) {
130                 // Problem: We already have an InsetLabel with the same name!
131                 screen_label_ = _("DUPLICATE: ") + label;
132                 return;
133         }
134         buffer().setInsetLabel(label, this);
135         screen_label_ = label;
136
137         if (utype) {
138                 // save info on the active counter
139                 Counters const & cnts = 
140                         buffer().masterBuffer()->params().documentClass().counters();
141                 active_counter_ = cnts.currentCounter();
142                 Language const * lang = par->getParLanguage(buffer().params());
143                 if (lang && !active_counter_.empty()) {
144                         counter_value_ = cnts.theCounter(active_counter_, lang->code());
145                         pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
146                 } else {
147                         counter_value_ = from_ascii("??");
148                         pretty_counter_ = from_ascii("??");
149                 }
150         }
151 }
152
153
154 void InsetLabel::addToToc(DocIterator const & cpit)
155 {
156         docstring const & label = getParam("name");
157         Toc & toc = buffer().tocBackend().toc("label");
158         if (buffer().insetLabel(label) != this) {
159                 toc.push_back(TocItem(cpit, 0, screen_label_));
160                 return;
161         }
162         toc.push_back(TocItem(cpit, 0, screen_label_));
163         Buffer::References const & refs = buffer().references(label);
164         Buffer::References::const_iterator it = refs.begin();
165         Buffer::References::const_iterator end = refs.end();
166         for (; it != end; ++it) {
167                 DocIterator const ref_pit(it->second);
168                 if (it->first->lyxCode() == MATH_REF_CODE)
169                         toc.push_back(TocItem(ref_pit, 1,
170                                 static_cast<InsetMathHull *>(it->first)->asRefInset()
171                                         ->screenLabel()));
172                 else
173                         toc.push_back(TocItem(ref_pit, 1,
174                                 static_cast<InsetRef *>(it->first)->screenLabel()));
175         }
176 }
177
178
179 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
180                            FuncStatus & status) const
181 {
182         bool enabled;
183         switch (cmd.action) {
184         case LFUN_LABEL_INSERT_AS_REF:
185         case LFUN_LABEL_COPY_AS_REF:
186                 enabled = true;
187                 break;
188         default:
189                 return InsetCommand::getStatus(cur, cmd, status);
190         }
191
192         status.setEnabled(enabled);
193         return true;
194 }
195
196
197 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
198 {
199         switch (cmd.action) {
200
201         case LFUN_INSET_MODIFY: {
202                 InsetCommandParams p(LABEL_CODE);
203                 // FIXME UNICODE
204                 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
205                 if (p.getCmdName().empty()) {
206                         cur.noUpdate();
207                         break;
208                 }
209                 if (p["name"] != params()["name"])
210                         updateCommand(p["name"]);
211                 break;
212         }
213
214         case LFUN_LABEL_COPY_AS_REF: {
215                 InsetCommandParams p(REF_CODE, "ref");
216                 p["reference"] = getParam("name");
217                 cap::clearSelection();
218                 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
219                 break;
220         }
221
222         case LFUN_LABEL_INSERT_AS_REF: {
223                 InsetCommandParams p(REF_CODE, "ref");
224                 p["reference"] = getParam("name");
225                 string const data = InsetCommand::params2string("ref", p);
226                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
227                 break;
228         }
229
230         default:
231                 InsetCommand::doDispatch(cur, cmd);
232                 break;
233         }
234 }
235
236
237 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
238 {
239         docstring const str = getParam("name");
240         os << '<' << str << '>';
241         return 2 + str.size();
242 }
243
244
245 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
246 {
247         os << "<!-- anchor id=\""
248            << sgml::cleanID(buffer(), runparams, getParam("name"))
249            << "\" -->";
250         return 0;
251 }
252
253
254 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
255 {
256         // FIXME XHTML
257         // Unfortunately, the name attribute has been deprecated, so we have to use
258         // id here to get the document to validate as XHTML 1.1. This will cause a 
259         // problem with some browsers, though, I'm sure. (Guess which!) So we will
260         // have to figure out what to do about this later. 
261         string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
262         xs << html::CompTag("a", attr);
263         return docstring();
264 }
265
266
267 } // namespace lyx