]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
bit of clean up
[lyx.git] / src / insets / InsetRef.cpp
1 /**
2  * \file InsetRef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include <config.h>
11
12 #include "InsetRef.h"
13
14 #include "Buffer.h"
15 #include "Cursor.h"
16 #include "DispatchResult.h"
17 #include "FuncRequest.h"
18 #include "InsetLabel.h"
19 #include "LaTeXFeatures.h"
20 #include "LyX.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23 #include "ParIterator.h"
24 #include "sgml.h"
25 #include "TocBackend.h"
26
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 using namespace lyx::support;
32 using namespace std;
33
34 namespace lyx {
35
36
37 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
38         : InsetCommand(buf, p, "ref"), isLatex(buf->isLatex())
39 {}
40
41
42 InsetRef::InsetRef(InsetRef const & ir)
43         : InsetCommand(ir), isLatex(ir.isLatex)
44 {}
45
46
47 bool InsetRef::isCompatibleCommand(string const & s) {
48         //FIXME This is likely not the best way to handle this.
49         //But this stuff is hardcoded elsewhere already.
50         return s == "ref" 
51                 || s == "pageref"
52                 || s == "vref" 
53                 || s == "vpageref"
54                 || s == "prettyref"
55                 || s == "eqref";
56 }
57
58
59 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
60 {
61         static ParamInfo param_info_;
62         if (param_info_.empty()) {
63                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
64                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
65                                 ParamInfo::HANDLING_ESCAPE);
66         }
67         return param_info_;
68 }
69
70
71 docstring InsetRef::screenLabel() const
72 {
73         return screen_label_;
74 }
75
76
77 int InsetRef::latex(odocstream & os, OutputParams const & runparams) const
78 {
79         // We don't want to output p_["name"], since that is only used 
80         // in docbook. So we construct new params, without it, and use that.
81         InsetCommandParams p(REF_CODE, getCmdName());
82         p["reference"] = getParam("reference");
83         os << p.getCommand(runparams);
84         return 0;
85 }
86
87
88 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
89 {
90         docstring const str = getParam("reference");
91         os << '[' << str << ']';
92         return 2 + str.size();
93 }
94
95
96 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
97 {
98         docstring const & name = getParam("name");
99         if (name.empty()) {
100                 if (runparams.flavor == OutputParams::XML) {
101                         os << "<xref linkend=\""
102                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
103                            << "\" />";
104                 } else {
105                         os << "<xref linkend=\""
106                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
107                            << "\">";
108                 }
109         } else {
110                 os << "<link linkend=\""
111                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
112                    << "\">"
113                    << getParam("name")
114                    << "</link>";
115         }
116
117         return 0;
118 }
119
120
121 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
122 {
123         docstring const & ref = getParam("reference");
124         InsetLabel const * il = buffer().insetLabel(ref);
125         string const & cmd = params().getCmdName();
126         docstring display_string;
127
128         if (il && !il->counterValue().empty()) {
129                 // Try to construct a label from the InsetLabel we reference.
130                 docstring const & value = il->counterValue();
131                 if (cmd == "ref")
132                         display_string = value;
133                 else if (cmd == "vref")
134                         // normally, would be "ref on page #", but we have no pages
135                         display_string = value;
136                 else if (cmd == "pageref" || cmd == "vpageref")
137                         // normally would be "on page #", but we have no pages
138                         display_string = _("elsewhere");
139                 else if (cmd == "eqref")
140                         display_string = bformat(from_ascii("equation (%1$s)"), value);
141                 else { // "prettyref"
142                         display_string = il->prettyCounter();
143                 }
144         } else 
145                         display_string = ref;
146
147         // FIXME What we'd really like to do is to be able to output some
148         // appropriate sort of text here. But to do that, we need to associate
149         // some sort of counter with the label, and we don't have that yet.
150         string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
151         xs << html::StartTag("a", attr);
152         xs << display_string;
153         xs << html::EndTag("a");
154         return docstring();
155 }
156
157
158 void InsetRef::tocString(odocstream & os) const
159 {
160         plaintext(os, OutputParams(0));
161 }
162
163
164 void InsetRef::updateLabels(ParIterator const & it, UpdateType)
165 {
166         docstring const & label = getParam("reference");
167         // register this inset into the buffer reference cache.
168         buffer().references(label).push_back(make_pair(this, it));
169
170         for (int i = 0; !types[i].latex_name.empty(); ++i) {
171                 if (getCmdName() == types[i].latex_name) {
172                         screen_label_ = _(types[i].short_gui_name);
173                         break;
174                 }
175         }
176         screen_label_ += label;
177
178         if (!isLatex && !getParam("name").empty()) {
179                 screen_label_ += "||";
180                 screen_label_ += getParam("name");
181         }
182 }
183
184
185 void InsetRef::addToToc(DocIterator const & cpit)
186 {
187         docstring const & label = getParam("reference");
188         if (buffer().insetLabel(label))
189                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
190                 return;
191
192         // It seems that this reference does not point to any valid label.
193         screen_label_ = _("BROKEN: ") + screen_label_;
194         Toc & toc = buffer().tocBackend().toc("label");
195         toc.push_back(TocItem(cpit, 0, screen_label_));
196 }
197
198
199 void InsetRef::validate(LaTeXFeatures & features) const
200 {
201         if (getCmdName() == "vref" || getCmdName() == "vpageref")
202                 features.require("varioref");
203         else if (getCmdName() == "prettyref")
204                 features.require("prettyref");
205         else if (getCmdName() == "eqref")
206                 features.require("amsmath");
207 }
208
209
210 InsetRef::type_info InsetRef::types[] = {
211         { "ref",       N_("Standard"),              N_("Ref: ")},
212         { "eqref",     N_("Equation"),              N_("EqRef: ")},
213         { "pageref",   N_("Page Number"),           N_("Page: ")},
214         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
215         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
216         { "prettyref", N_("PrettyRef"),             N_("FormatRef: ")},
217         { "", "", "" }
218 };
219
220
221 int InsetRef::getType(string const & name)
222 {
223         for (int i = 0; !types[i].latex_name.empty(); ++i)
224                 if (name == types[i].latex_name)
225                         return i;
226         return 0;
227 }
228
229
230 string const & InsetRef::getName(int type)
231 {
232         return types[type].latex_name;
233 }
234
235
236 } // namespace lyx