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