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