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 "LaTeXFeatures.h"
20 #include "OutputParams.h"
21 #include "output_xhtml.h"
22 #include "ParIterator.h"
24 #include "TocBackend.h"
26 #include "support/docstream.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
30 using namespace lyx::support;
36 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
37 : InsetCommand(buf, p, "ref"), isLatex(buf->isLatex())
41 InsetRef::InsetRef(InsetRef const & ir)
42 : InsetCommand(ir), isLatex(ir.isLatex)
46 bool InsetRef::isCompatibleCommand(string const & s) {
47 //FIXME This is likely not the best way to handle this.
48 //But this stuff is hardcoded elsewhere already.
58 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
60 static ParamInfo param_info_;
61 if (param_info_.empty()) {
62 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
63 param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
69 docstring InsetRef::screenLabel() const
75 int InsetRef::latex(odocstream & os, OutputParams const &) const
77 // We don't want to output p_["name"], since that is only used
78 // in docbook. So we construct new params, without it, and use that.
79 InsetCommandParams p(REF_CODE, getCmdName());
80 p["reference"] = getParam("reference");
81 os << escape(p.getCommand());
86 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
88 docstring const str = getParam("reference");
89 os << '[' << str << ']';
90 return 2 + str.size();
94 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
96 docstring const & name = getParam("name");
98 if (runparams.flavor == OutputParams::XML) {
99 os << "<xref linkend=\""
100 << sgml::cleanID(buffer(), runparams, getParam("reference"))
103 os << "<xref linkend=\""
104 << sgml::cleanID(buffer(), runparams, getParam("reference"))
108 os << "<link linkend=\""
109 << sgml::cleanID(buffer(), runparams, getParam("reference"))
119 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
121 // FIXME What we'd really like to do is to be able to output some
122 // appropriate sort of text here. But to do that, we need to associate
123 // some sort of counter with the label, and we don't have that yet.
124 docstring const ref = html::cleanAttr(getParam("reference"));
125 string const attr = "href=\"#" + to_utf8(ref) + "\"";
126 xs << StartTag("a", attr);
132 void InsetRef::tocString(odocstream & os) const
134 plaintext(os, OutputParams(0));
138 void InsetRef::updateLabels(ParIterator const & it)
140 docstring const & label = getParam("reference");
141 // register this inset into the buffer reference cache.
142 buffer().references(label).push_back(make_pair(this, it));
144 for (int i = 0; !types[i].latex_name.empty(); ++i) {
145 if (getCmdName() == types[i].latex_name) {
146 screen_label_ = _(types[i].short_gui_name);
150 screen_label_ += label;
152 if (!isLatex && !getParam("name").empty()) {
153 screen_label_ += "||";
154 screen_label_ += getParam("name");
159 void InsetRef::addToToc(DocIterator const & cpit)
161 docstring const & label = getParam("reference");
162 if (buffer().insetLabel(label))
163 // This InsetRef has already been taken care of in InsetLabel::addToToc().
166 // It seems that this reference does not point to any valid label.
167 screen_label_ = _("BROKEN: ") + screen_label_;
168 Toc & toc = buffer().tocBackend().toc("label");
169 toc.push_back(TocItem(cpit, 0, screen_label_));
173 void InsetRef::validate(LaTeXFeatures & features) const
175 if (getCmdName() == "vref" || getCmdName() == "vpageref")
176 features.require("varioref");
177 else if (getCmdName() == "prettyref")
178 features.require("prettyref");
179 else if (getCmdName() == "eqref")
180 features.require("amsmath");
184 InsetRef::type_info InsetRef::types[] = {
185 { "ref", N_("Standard"), N_("Ref: ")},
186 { "eqref", N_("Equation"), N_("EqRef: ")},
187 { "pageref", N_("Page Number"), N_("Page: ")},
188 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
189 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
190 { "prettyref", N_("PrettyRef"), N_("FormatRef: ")},
195 int InsetRef::getType(string const & name)
197 for (int i = 0; !types[i].latex_name.empty(); ++i)
198 if (name == types[i].latex_name)
204 string const & InsetRef::getName(int type)
206 return types[type].latex_name;