]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
e0d00c6fc988d56ff1f67d0af2f802bffe92d2e2
[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 "Buffer.h"
16 #include "BufferView.h"
17 #include "DispatchResult.h"
18 #include "FuncRequest.h"
19 #include "ParIterator.h"
20 #include "sgml.h"
21 #include "Text.h"
22 #include "TocBackend.h"
23
24 #include "support/lstrings.h"
25 #include "support/lyxalgo.h"
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32
33 InsetLabel::InsetLabel(InsetCommandParams const & p)
34         : InsetCommand(p, "label")
35 {}
36
37
38 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
39 {
40         static ParamInfo param_info_;
41         if (param_info_.empty()) {
42                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
43         }
44         return param_info_;
45 }
46
47
48 Inset * InsetLabel::clone() const
49 {
50         return new InsetLabel(params());
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::addToToc(ParConstIterator const & cpit) const
67 {
68         Toc & toc = buffer().tocBackend().toc("label");
69         toc.push_back(TocItem(cpit, 0, screenLabel()));
70 }
71
72
73 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
74 {
75         switch (cmd.action) {
76
77         case LFUN_INSET_MODIFY: {
78                 InsetCommandParams p(LABEL_CODE);
79                 // FIXME UNICODE
80                 InsetCommandMailer::string2params("label", to_utf8(cmd.argument()), p);
81                 if (p.getCmdName().empty()) {
82                         cur.noUpdate();
83                         break;
84                 }
85                 if (p["name"] != params()["name"])
86                         cur.bv().buffer().changeRefsIfUnique(params()["name"],
87                                         p["name"], REF_CODE);
88                 setParams(p);
89                 break;
90         }
91
92         default:
93                 InsetCommand::doDispatch(cur, cmd);
94                 break;
95         }
96 }
97
98
99 int InsetLabel::latex(odocstream & os, OutputParams const &) const
100 {
101         os << escape(getCommand());
102         return 0;
103 }
104
105
106 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
107 {
108         docstring const str = getParam("name");
109         os << '<' << str << '>';
110         return 2 + str.size();
111 }
112
113
114 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
115 {
116         os << "<!-- anchor id=\""
117            << sgml::cleanID(buffer(), runparams, getParam("name"))
118            << "\" -->";
119         return 0;
120 }
121
122
123 } // namespace lyx