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"
28 #include "support/FileName.h"
30 using namespace lyx::support;
36 InsetRef::InsetRef(Buffer const & buf, InsetCommandParams const & p)
37 : InsetCommand(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 void InsetRef::tocString(odocstream & os) const
121 plaintext(os, OutputParams(0));
125 void InsetRef::updateLabels(ParIterator const & it)
127 docstring const & label = getParam("reference");
128 // register this inset into the buffer reference cache.
129 buffer().references(label).push_back(make_pair(this, it));
131 for (int i = 0; !types[i].latex_name.empty(); ++i) {
132 if (getCmdName() == types[i].latex_name) {
133 screen_label_ = _(types[i].short_gui_name);
137 screen_label_ += getParam("reference");
139 if (!isLatex && !getParam("name").empty()) {
140 screen_label_ += "||";
141 screen_label_ += getParam("name");
147 ** If inset belongs to an Embedded WorkArea's Buffer, ref is likely
148 ** to point to some documentBuffer's label. Don't know how to
149 ** retrieve the corresponding documentBuffer (so as to check if the
150 ** label exists or not), so the "BROKEN:" indication is disabled in
151 ** Embedded WorkArea(s).
153 void InsetRef::addToToc(DocIterator const & cpit)
155 docstring const & label = getParam("reference");
156 if (buffer().fileName().extension() == "internal")
159 if (buffer().insetLabel(label))
160 // This InsetRef has already been taken care of in InsetLabel::addToToc().
163 // It seems that this reference does not point to any valid label.
164 screen_label_ = _("BROKEN: ") + screen_label_;
165 Toc & toc = buffer().tocBackend().toc("label");
166 toc.push_back(TocItem(cpit, 0, screen_label_));
170 void InsetRef::validate(LaTeXFeatures & features) const
172 if (getCmdName() == "vref" || getCmdName() == "vpageref")
173 features.require("varioref");
174 else if (getCmdName() == "prettyref")
175 features.require("prettyref");
176 else if (getCmdName() == "eqref")
177 features.require("amsmath");
181 InsetRef::type_info InsetRef::types[] = {
182 { "ref", N_("Standard"), N_("Ref: ")},
183 { "eqref", N_("Equation"), N_("EqRef: ")},
184 { "pageref", N_("Page Number"), N_("Page: ")},
185 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
186 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
187 { "prettyref", N_("PrettyRef"), N_("FormatRef: ")},
192 int InsetRef::getType(string const & name)
194 for (int i = 0; !types[i].latex_name.empty(); ++i)
195 if (name == types[i].latex_name)
201 string const & InsetRef::getName(int type)
203 return types[type].latex_name;