]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
647347bc8d811d18367a58ba02a83dd7cae1d4b6
[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 "LyXFunc.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         }
66         return param_info_;
67 }
68
69
70 docstring InsetRef::screenLabel() const
71 {
72         return screen_label_;
73 }
74
75
76 int InsetRef::latex(odocstream & os, OutputParams const &) const
77 {
78         // We don't want to output p_["name"], since that is only used 
79         // in docbook. So we construct new params, without it, and use that.
80         InsetCommandParams p(REF_CODE, getCmdName());
81         p["reference"] = getParam("reference");
82         os << escape(p.getCommand());
83         return 0;
84 }
85
86
87 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
88 {
89         docstring const str = getParam("reference");
90         os << '[' << str << ']';
91         return 2 + str.size();
92 }
93
94
95 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
96 {
97         docstring const & name = getParam("name");
98         if (name.empty()) {
99                 if (runparams.flavor == OutputParams::XML) {
100                         os << "<xref linkend=\""
101                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
102                            << "\" />";
103                 } else {
104                         os << "<xref linkend=\""
105                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
106                            << "\">";
107                 }
108         } else {
109                 os << "<link linkend=\""
110                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
111                    << "\">"
112                    << getParam("name")
113                    << "</link>";
114         }
115
116         return 0;
117 }
118
119
120 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
121 {
122         docstring const & ref = getParam("reference");
123         InsetLabel const * il = buffer().insetLabel(ref);
124         string const & cmd = params().getCmdName();
125         docstring display_string;
126
127         if (il && !il->counterValue().empty()) {
128                 // Try to construct a label from the InsetLabel we reference.
129                 docstring const & cntr = il->activeCounter();
130                 docstring const & value = il->counterValue();
131                 if (cmd == "ref")
132                         display_string = value;
133                 else if (cmd == "vref")
134                         display_string = bformat(from_ascii("%1$s on page ##"), value);
135                 else if (cmd == "pageref" || cmd == "vpageref")
136                         display_string = _("on page ##");
137                 else if (cmd == "eqref")
138                         display_string = bformat(from_ascii("equation (%1$s)"), value);
139                 else { // "prettyref"
140                         docstring cntrname = translateIfPossible(cntr);
141                         // FIXME Use the label string, if we have it. Otherwise, do this.
142                         display_string = bformat(from_ascii("%1$s %2$s"), cntrname, value);
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, bool)
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