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);
70 docstring InsetRef::screenLabel() const
76 int InsetRef::latex(odocstream & os, OutputParams const &) const
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());
87 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
89 docstring const str = getParam("reference");
90 os << '[' << str << ']';
91 return 2 + str.size();
95 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
97 docstring const & name = getParam("name");
99 if (runparams.flavor == OutputParams::XML) {
100 os << "<xref linkend=\""
101 << sgml::cleanID(buffer(), runparams, getParam("reference"))
104 os << "<xref linkend=\""
105 << sgml::cleanID(buffer(), runparams, getParam("reference"))
109 os << "<link linkend=\""
110 << sgml::cleanID(buffer(), runparams, getParam("reference"))
120 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
122 docstring const & ref = getParam("reference");
123 InsetLabel const * il = buffer().insetLabel(ref);
124 string const & cmd = params().getCmdName();
125 docstring display_string;
127 if (il && !il->counterValue().empty()) {
128 // Try to construct a label from the InsetLabel we reference.
129 docstring const & value = il->counterValue();
131 display_string = value;
132 else if (cmd == "vref")
133 // normally, would be "ref on page #", but we have no pages
134 display_string = value;
135 else if (cmd == "pageref" || cmd == "vpageref")
136 // normally would be "on page #", but we have no pages
137 display_string = _("elsewhere");
138 else if (cmd == "eqref")
139 display_string = bformat(from_ascii("equation (%1$s)"), value);
140 else { // "prettyref"
141 display_string = il->prettyCounter();
144 display_string = ref;
146 // FIXME What we'd really like to do is to be able to output some
147 // appropriate sort of text here. But to do that, we need to associate
148 // some sort of counter with the label, and we don't have that yet.
149 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
150 xs << html::StartTag("a", attr);
151 xs << display_string;
152 xs << html::EndTag("a");
157 void InsetRef::tocString(odocstream & os) const
159 plaintext(os, OutputParams(0));
163 void InsetRef::updateLabels(ParIterator const & it, UpdateType)
165 docstring const & label = getParam("reference");
166 // register this inset into the buffer reference cache.
167 buffer().references(label).push_back(make_pair(this, it));
169 for (int i = 0; !types[i].latex_name.empty(); ++i) {
170 if (getCmdName() == types[i].latex_name) {
171 screen_label_ = _(types[i].short_gui_name);
175 screen_label_ += label;
177 if (!isLatex && !getParam("name").empty()) {
178 screen_label_ += "||";
179 screen_label_ += getParam("name");
184 void InsetRef::addToToc(DocIterator const & cpit)
186 docstring const & label = getParam("reference");
187 if (buffer().insetLabel(label))
188 // This InsetRef has already been taken care of in InsetLabel::addToToc().
191 // It seems that this reference does not point to any valid label.
192 screen_label_ = _("BROKEN: ") + screen_label_;
193 Toc & toc = buffer().tocBackend().toc("label");
194 toc.push_back(TocItem(cpit, 0, screen_label_));
198 void InsetRef::validate(LaTeXFeatures & features) const
200 if (getCmdName() == "vref" || getCmdName() == "vpageref")
201 features.require("varioref");
202 else if (getCmdName() == "prettyref")
203 features.require("prettyref");
204 else if (getCmdName() == "eqref")
205 features.require("amsmath");
209 InsetRef::type_info InsetRef::types[] = {
210 { "ref", N_("Standard"), N_("Ref: ")},
211 { "eqref", N_("Equation"), N_("EqRef: ")},
212 { "pageref", N_("Page Number"), N_("Page: ")},
213 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
214 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
215 { "prettyref", N_("PrettyRef"), N_("FormatRef: ")},
220 int InsetRef::getType(string const & name)
222 for (int i = 0; !types[i].latex_name.empty(); ++i)
223 if (name == types[i].latex_name)
229 string const & InsetRef::getName(int type)
231 return types[type].latex_name;