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.
15 #include "buffer_funcs.h"
17 #include "DispatchResult.h"
18 #include "FuncRequest.h"
19 #include "LaTeXFeatures.h"
21 #include "OutputParams.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 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 void InsetRef::initView()
48 // We need an update of the Buffer reference cache. This is achieved by
50 lyx::updateLabels(buffer());
54 bool InsetRef::isCompatibleCommand(string const & s) {
55 //FIXME This is likely not the best way to handle this.
56 //But this stuff is hardcoded elsewhere already.
66 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
68 static ParamInfo param_info_;
69 if (param_info_.empty()) {
70 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
71 param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
77 docstring InsetRef::screenLabel() const
83 int InsetRef::latex(odocstream & os, OutputParams const &) const
85 // We don't want to output p_["name"], since that is only used
86 // in docbook. So we construct new params, without it, and use that.
87 InsetCommandParams p(REF_CODE, getCmdName());
88 p["reference"] = getParam("reference");
89 os << escape(p.getCommand());
94 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
96 docstring const str = getParam("reference");
97 os << '[' << str << ']';
98 return 2 + str.size();
102 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
104 docstring const & name = getParam("name");
106 if (runparams.flavor == OutputParams::XML) {
107 os << "<xref linkend=\""
108 << sgml::cleanID(buffer(), runparams, getParam("reference"))
111 os << "<xref linkend=\""
112 << sgml::cleanID(buffer(), runparams, getParam("reference"))
116 os << "<link linkend=\""
117 << sgml::cleanID(buffer(), runparams, getParam("reference"))
127 void InsetRef::textString(odocstream & os) const
129 plaintext(os, OutputParams(0));
133 void InsetRef::updateLabels(ParIterator const & it)
135 docstring const & label = getParam("reference");
136 // register this inset into the buffer reference cache.
137 buffer().references(label).push_back(make_pair(this, it));
139 for (int i = 0; !types[i].latex_name.empty(); ++i) {
140 if (getCmdName() == types[i].latex_name) {
141 screen_label_ = _(types[i].short_gui_name);
145 screen_label_ += getParam("reference");
147 if (!isLatex && !getParam("name").empty()) {
148 screen_label_ += "||";
149 screen_label_ += getParam("name");
154 void InsetRef::addToToc(ParConstIterator const & cpit) const
156 docstring const & label = getParam("reference");
157 if (buffer().insetLabel(label))
158 // This InsetRef has already been taken care of in InsetLabel::addToToc().
161 // It seems that this reference does not point to any valid label.
162 screen_label_ = _("BROKEN: ") + screen_label_;
163 Toc & toc = buffer().tocBackend().toc("label");
164 toc.push_back(TocItem(cpit, 0, screen_label_));
168 void InsetRef::validate(LaTeXFeatures & features) const
170 if (getCmdName() == "vref" || getCmdName() == "vpageref")
171 features.require("varioref");
172 else if (getCmdName() == "prettyref")
173 features.require("prettyref");
174 else if (getCmdName() == "eqref")
175 features.require("amsmath");
179 InsetRef::type_info InsetRef::types[] = {
180 { "ref", N_("Standard"), N_("Ref: ")},
181 { "eqref", N_("Equation"), N_("EqRef: ")},
182 { "pageref", N_("Page Number"), N_("Page: ")},
183 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
184 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
185 { "prettyref", N_("PrettyRef"), N_("FormatRef: ")},
190 int InsetRef::getType(string const & name)
192 for (int i = 0; !types[i].latex_name.empty(); ++i)
193 if (name == types[i].latex_name)
199 string const & InsetRef::getName(int type)
201 return types[type].latex_name;