3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
16 #include "DispatchResult.h"
17 #include "FuncRequest.h"
18 #include "InsetLabel.h"
19 #include "LaTeXFeatures.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23 #include "ParIterator.h"
25 #include "TocBackend.h"
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
31 using namespace lyx::support;
37 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
38 : InsetCommand(buf, p, "ref"), isLatex(buf->isLatex())
42 InsetRef::InsetRef(InsetRef const & ir)
43 : InsetCommand(ir), isLatex(ir.isLatex)
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.
59 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
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);
71 docstring InsetRef::screenLabel() const
77 int InsetRef::latex(odocstream & os, OutputParams const & runparams) const
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);
88 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
90 docstring const str = getParam("reference");
91 os << '[' << str << ']';
92 return 2 + str.size();
96 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
98 docstring const & name = getParam("name");
100 if (runparams.flavor == OutputParams::XML) {
101 os << "<xref linkend=\""
102 << sgml::cleanID(buffer(), runparams, getParam("reference"))
105 os << "<xref linkend=\""
106 << sgml::cleanID(buffer(), runparams, getParam("reference"))
110 os << "<link linkend=\""
111 << sgml::cleanID(buffer(), runparams, getParam("reference"))
121 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
123 docstring const & ref = getParam("reference");
124 InsetLabel const * il = buffer().insetLabel(ref);
125 string const & cmd = params().getCmdName();
126 docstring display_string;
128 if (il && !il->counterValue().empty()) {
129 // Try to construct a label from the InsetLabel we reference.
130 docstring const & value = il->counterValue();
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();
145 display_string = ref;
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");
158 void InsetRef::tocString(odocstream & os) const
160 plaintext(os, OutputParams(0));
164 void InsetRef::updateLabels(ParIterator const & it, UpdateType)
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));
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);
176 screen_label_ += label;
178 if (!isLatex && !getParam("name").empty()) {
179 screen_label_ += "||";
180 screen_label_ += getParam("name");
185 void InsetRef::addToToc(DocIterator const & cpit)
187 docstring const & label = getParam("reference");
188 if (buffer().insetLabel(label))
189 // This InsetRef has already been taken care of in InsetLabel::addToToc().
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_));
199 void InsetRef::validate(LaTeXFeatures & features) const
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");
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: ")},
221 int InsetRef::getType(string const & name)
223 for (int i = 0; !types[i].latex_name.empty(); ++i)
224 if (name == types[i].latex_name)
230 string const & InsetRef::getName(int type)
232 return types[type].latex_name;