]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLabel.cpp
c15beea4db82da2316a719abfe6243d907110f41
[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 "Text.h"
20 #include "sgml.h"
21
22 #include "support/lstrings.h"
23 #include "support/lyxalgo.h"
24
25
26 namespace lyx {
27
28
29 InsetLabel::InsetLabel(InsetCommandParams const & p)
30         : InsetCommand(p, "label")
31 {}
32
33
34 CommandInfo const * InsetLabel::findInfo(std::string const & /* cmdName */)
35 {
36         static const char * const paramnames[] = {"name", ""};
37         static const bool isoptional[] = {false};
38         static const CommandInfo info = {1, paramnames, isoptional};
39         return &info;
40 }
41
42
43 Inset * InsetLabel::clone() const
44 {
45         return new InsetLabel(params());
46 }
47
48
49 void InsetLabel::getLabelList(Buffer const &, std::vector<docstring> & list) const
50 {
51         list.push_back(getParam("name"));
52 }
53
54
55 docstring const InsetLabel::getScreenLabel(Buffer const &) const
56 {
57         return getParam("name");
58 }
59
60
61 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
62 {
63         switch (cmd.action) {
64
65         case LFUN_INSET_MODIFY: {
66                 InsetCommandParams p(LABEL_CODE);
67                 // FIXME UNICODE
68                 InsetCommandMailer::string2params("label", to_utf8(cmd.argument()), p);
69                 if (p.getCmdName().empty()) {
70                         cur.noUpdate();
71                         break;
72                 }
73                 if (p["name"] != params()["name"])
74                         cur.bv().buffer().changeRefsIfUnique(params()["name"],
75                                         p["name"], REF_CODE);
76                 setParams(p);
77                 break;
78         }
79
80         default:
81                 InsetCommand::doDispatch(cur, cmd);
82                 break;
83         }
84 }
85
86
87 int InsetLabel::latex(Buffer const &, odocstream & os,
88                       OutputParams const &) const
89 {
90         os << support::escape(getCommand());
91         return 0;
92 }
93
94
95 int InsetLabel::plaintext(Buffer const &, odocstream & os,
96                           OutputParams const &) const
97 {
98         docstring const str = getParam("name");
99         os << '<' << str << '>';
100         return 2 + str.size();
101 }
102
103
104 int InsetLabel::docbook(Buffer const & buf, odocstream & os,
105                         OutputParams const & runparams) const
106 {
107         os << "<!-- anchor id=\""
108            << sgml::cleanID(buf, runparams, getParam("name"))
109            << "\" -->";
110         return 0;
111 }
112
113
114 } // namespace lyx