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 "ParIterator.h"
23 #include "TocBackend.h"
25 #include "support/docstream.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
29 using namespace lyx::support;
35 InsetRef::InsetRef(Buffer const & buf, InsetCommandParams const & p)
36 : InsetCommand(p, "ref"), isLatex(buf.isLatex())
40 InsetRef::InsetRef(InsetRef const & ir)
41 : InsetCommand(ir), isLatex(ir.isLatex)
45 bool InsetRef::isCompatibleCommand(string const & s) {
46 //FIXME This is likely not the best way to handle this.
47 //But this stuff is hardcoded elsewhere already.
57 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
59 static ParamInfo param_info_;
60 if (param_info_.empty()) {
61 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
62 param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
68 docstring InsetRef::screenLabel() const
74 int InsetRef::latex(odocstream & os, OutputParams const &) const
76 // We don't want to output p_["name"], since that is only used
77 // in docbook. So we construct new params, without it, and use that.
78 InsetCommandParams p(REF_CODE, getCmdName());
79 p["reference"] = getParam("reference");
80 os << escape(p.getCommand());
85 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
87 docstring const str = getParam("reference");
88 os << '[' << str << ']';
89 return 2 + str.size();
93 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
95 docstring const & name = getParam("name");
97 if (runparams.flavor == OutputParams::XML) {
98 os << "<xref linkend=\""
99 << sgml::cleanID(buffer(), runparams, getParam("reference"))
102 os << "<xref linkend=\""
103 << sgml::cleanID(buffer(), runparams, getParam("reference"))
107 os << "<link linkend=\""
108 << sgml::cleanID(buffer(), runparams, getParam("reference"))
118 void InsetRef::textString(odocstream & os) const
120 plaintext(os, OutputParams(0));
124 void InsetRef::updateLabels(ParIterator const & it)
126 docstring const & label = getParam("reference");
127 // register this inset into the buffer reference cache.
128 buffer().references(label).push_back(make_pair(this, it));
130 for (int i = 0; !types[i].latex_name.empty(); ++i) {
131 if (getCmdName() == types[i].latex_name) {
132 screen_label_ = _(types[i].short_gui_name);
136 screen_label_ += getParam("reference");
138 if (!isLatex && !getParam("name").empty()) {
139 screen_label_ += "||";
140 screen_label_ += getParam("name");
145 void InsetRef::addToToc(ParConstIterator const & cpit) const
147 docstring const & label = getParam("reference");
148 if (buffer().insetLabel(label))
149 // This InsetRef has already been taken care of in InsetLabel::addToToc().
152 // It seems that this reference does not point to any valid label.
153 screen_label_ = _("BROKEN: ") + screen_label_;
154 Toc & toc = buffer().tocBackend().toc("label");
155 toc.push_back(TocItem(cpit, 0, screen_label_));
159 void InsetRef::validate(LaTeXFeatures & features) const
161 if (getCmdName() == "vref" || getCmdName() == "vpageref")
162 features.require("varioref");
163 else if (getCmdName() == "prettyref")
164 features.require("prettyref");
165 else if (getCmdName() == "eqref")
166 features.require("amsmath");
170 InsetRef::type_info InsetRef::types[] = {
171 { "ref", N_("Standard"), N_("Ref: ")},
172 { "eqref", N_("Equation"), N_("EqRef: ")},
173 { "pageref", N_("Page Number"), N_("Page: ")},
174 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
175 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
176 { "prettyref", N_("PrettyRef"), N_("FormatRef: ")},
181 int InsetRef::getType(string const & name)
183 for (int i = 0; !types[i].latex_name.empty(); ++i)
184 if (name == types[i].latex_name)
190 string const & InsetRef::getName(int type)
192 return types[type].latex_name;