]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
c23cee2b06ae45f32efee58f2d94284b2676a9d1
[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.h"
18 #include "BufferView.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "InsetIterator.h"
22 #include "ParIterator.h"
23 #include "sgml.h"
24 #include "Text.h"
25 #include "TocBackend.h"
26
27 #include "frontends/alert.h"
28
29 #include "support/lyxalgo.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32
33 using namespace std;
34 using namespace lyx::support;
35
36 namespace lyx {
37
38
39 InsetLabel::InsetLabel(InsetCommandParams const & p)
40         : InsetCommand(p, "label")
41 {}
42
43
44 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
45 {
46         static ParamInfo param_info_;
47         if (param_info_.empty()) {
48                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
49         }
50         return param_info_;
51 }
52
53
54 void InsetLabel::getLabelList(vector<docstring> & list) const
55 {
56         list.push_back(getParam("name"));
57 }
58
59
60 docstring InsetLabel::screenLabel() const
61 {
62         return getParam("name");
63 }
64
65
66 void InsetLabel::updateLabels(ParIterator const & it)
67 {
68         docstring const & label = getParam("name");
69         if (buffer().insetLabel(label))
70                 // Problem: We already have an InsetLabel with the same name!
71                 return;
72         buffer().setInsetLabel(label, this);
73 }
74
75
76 void InsetLabel::addToToc(ParConstIterator const & cpit) const
77 {
78         docstring const & label = getParam("name");
79         Toc & toc = buffer().tocBackend().toc("label");
80         if (buffer().insetLabel(label) != this) {
81                 toc.push_back(TocItem(cpit, 0, _("DUPLICATE: ") + label));
82                 return;
83         }
84         toc.push_back(TocItem(cpit, 0, label));
85         Buffer::References const & refs = buffer().references(label);
86         Buffer::References::const_iterator it = refs.begin();
87         Buffer::References::const_iterator end = refs.end();
88         for (; it != end; ++it) {
89                 ParConstIterator const ref_pit(it->second);
90                 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
91         }
92 }
93
94
95 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
96 {
97         switch (cmd.action) {
98
99         case LFUN_INSET_MODIFY: {
100                 InsetCommandParams p(LABEL_CODE);
101                 // FIXME UNICODE
102                 InsetCommandMailer::string2params("label", to_utf8(cmd.argument()), p);
103                 if (p.getCmdName().empty()) {
104                         cur.noUpdate();
105                         break;
106                 }
107                 if (p["name"] != params()["name"])
108                         cur.bv().buffer().changeRefsIfUnique(params()["name"],
109                                         p["name"], REF_CODE);
110                 setParams(p);
111                 break;
112         }
113
114         default:
115                 InsetCommand::doDispatch(cur, cmd);
116                 break;
117         }
118 }
119
120
121 int InsetLabel::latex(odocstream & os, OutputParams const &) const
122 {
123         os << escape(getCommand());
124         return 0;
125 }
126
127
128 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
129 {
130         docstring const str = getParam("name");
131         os << '<' << str << '>';
132         return 2 + str.size();
133 }
134
135
136 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
137 {
138         os << "<!-- anchor id=\""
139            << sgml::cleanID(buffer(), runparams, getParam("name"))
140            << "\" -->";
141         return 0;
142 }
143
144
145 } // namespace lyx