]> git.lyx.org Git - features.git/blob - src/insets/InsetLabel.cpp
move updateLables to buffer
[features.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 "BufferView.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "InsetIterator.h"
23 #include "ParIterator.h"
24 #include "sgml.h"
25 #include "Text.h"
26 #include "TocBackend.h"
27
28 #include "frontends/alert.h"
29
30 #include "support/convert.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33 #include "support/lyxalgo.h"
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39
40
41 InsetLabel::InsetLabel(InsetCommandParams const & p)
42         : InsetCommand(p, "label")
43 {}
44
45
46 void InsetLabel::initView()
47 {
48         updateCommand(getParam("name"));
49 }
50
51
52 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
53 {
54         docstring const old_label = getParam("name");
55         docstring label = new_label;
56         int i = 1;
57         while (buffer().insetLabel(label)) {
58                 label = new_label + '-' + convert<docstring>(i);
59                 ++i;
60         }
61
62         if (label != new_label) {
63                 // Warn the user that the label has been changed to something else.
64                 frontend::Alert::warning(_("Label names must be unique!"),
65                         bformat(_("The label %1$s already exists,\n"
66                         "it will be changed to %2$s."), new_label, label));
67         }
68
69         buffer().undo().beginUndoGroup();
70         setParam("name", label);
71
72         if (updaterefs) {
73                 Buffer::References & refs = buffer().references(old_label);
74                 Buffer::References::iterator it = refs.begin();
75                 Buffer::References::iterator end = refs.end();
76                 for (; it != end; ++it) {
77                         buffer().undo().recordUndo(it->second);
78                         it->first->setParam("reference", label);
79                 }
80         }
81         buffer().undo().endUndoGroup();
82
83         // We need an update of the Buffer reference cache. This is achieved by
84         // updateLabels().
85         buffer().updateLabels();
86 }
87
88
89 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
90 {
91         static ParamInfo param_info_;
92         if (param_info_.empty())
93                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
94         return param_info_;
95 }
96
97
98 docstring InsetLabel::screenLabel() const
99 {
100         return screen_label_;
101 }
102
103
104 void InsetLabel::updateLabels(ParIterator const &)
105 {
106         docstring const & label = getParam("name");
107         if (buffer().insetLabel(label)) {
108                 // Problem: We already have an InsetLabel with the same name!
109                 screen_label_ = _("DUPLICATE: ") + label;
110                 return;
111         }
112         buffer().setInsetLabel(label, this);
113         screen_label_ = label;
114 }
115
116
117 void InsetLabel::addToToc(DocIterator const & cpit)
118 {
119         docstring const & label = getParam("name");
120         Toc & toc = buffer().tocBackend().toc("label");
121         if (buffer().insetLabel(label) != this) {
122                 toc.push_back(TocItem(cpit, 0, screen_label_));
123                 return;
124         }
125         toc.push_back(TocItem(cpit, 0, screen_label_));
126         Buffer::References const & refs = buffer().references(label);
127         Buffer::References::const_iterator it = refs.begin();
128         Buffer::References::const_iterator end = refs.end();
129         for (; it != end; ++it) {
130                 DocIterator const ref_pit(it->second);
131                 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
132         }
133 }
134
135
136 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
137 {
138         switch (cmd.action) {
139
140         case LFUN_INSET_MODIFY: {
141                 InsetCommandParams p(LABEL_CODE);
142                 // FIXME UNICODE
143                 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
144                 if (p.getCmdName().empty()) {
145                         cur.noUpdate();
146                         break;
147                 }
148                 if (p["name"] != params()["name"])
149                         updateCommand(p["name"]);
150                 break;
151         }
152
153         default:
154                 InsetCommand::doDispatch(cur, cmd);
155                 break;
156         }
157 }
158
159
160 int InsetLabel::latex(odocstream & os, OutputParams const &) const
161 {
162         os << escape(getCommand());
163         return 0;
164 }
165
166
167 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
168 {
169         docstring const str = getParam("name");
170         os << '<' << str << '>';
171         return 2 + str.size();
172 }
173
174
175 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
176 {
177         os << "<!-- anchor id=\""
178            << sgml::cleanID(buffer(), runparams, getParam("name"))
179            << "\" -->";
180         return 0;
181 }
182
183
184 } // namespace lyx