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 "InsetLabel.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.
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,
65 ParamInfo::HANDLING_ESCAPE);
71 int InsetRef::latex(odocstream & os, OutputParams const & runparams) const
73 // We don't want to output p_["name"], since that is only used
74 // in docbook. So we construct new params, without it, and use that.
75 InsetCommandParams p(REF_CODE, getCmdName());
76 p["reference"] = getParam("reference");
77 os << p.getCommand(runparams);
82 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
84 docstring const str = getParam("reference");
85 os << '[' << str << ']';
86 return 2 + str.size();
90 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
92 docstring const & name = getParam("name");
94 if (runparams.flavor == OutputParams::XML) {
95 os << "<xref linkend=\""
96 << sgml::cleanID(buffer(), runparams, getParam("reference"))
99 os << "<xref linkend=\""
100 << sgml::cleanID(buffer(), runparams, getParam("reference"))
104 os << "<link linkend=\""
105 << sgml::cleanID(buffer(), runparams, getParam("reference"))
115 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
117 docstring const & ref = getParam("reference");
118 InsetLabel const * il = buffer().insetLabel(ref);
119 string const & cmd = params().getCmdName();
120 docstring display_string;
122 if (il && !il->counterValue().empty()) {
123 // Try to construct a label from the InsetLabel we reference.
124 docstring const & value = il->counterValue();
126 display_string = value;
127 else if (cmd == "vref")
128 // normally, would be "ref on page #", but we have no pages
129 display_string = value;
130 else if (cmd == "pageref" || cmd == "vpageref")
131 // normally would be "on page #", but we have no pages
132 display_string = _("elsewhere");
133 else if (cmd == "eqref")
134 display_string = bformat(from_ascii("equation (%1$s)"), value);
135 else if (cmd == "prettyref"
136 // we don't really have the ability to handle these
137 // properly in XHTML output
139 display_string = il->prettyCounter();
141 display_string = ref;
143 // FIXME What we'd really like to do is to be able to output some
144 // appropriate sort of text here. But to do that, we need to associate
145 // some sort of counter with the label, and we don't have that yet.
146 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
147 xs << html::StartTag("a", attr);
148 xs << display_string;
149 xs << html::EndTag("a");
154 void InsetRef::tocString(odocstream & os) const
156 plaintext(os, OutputParams(0));
160 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
162 docstring const & ref = getParam("reference");
163 // register this inset into the buffer reference cache.
164 buffer().references(ref).push_back(make_pair(this, it));
167 for (int i = 0; !types[i].latex_name.empty(); ++i) {
168 if (getCmdName() == types[i].latex_name) {
169 label = _(types[i].short_gui_name);
175 if (!isLatex && !getParam("name").empty()) {
177 label += getParam("name");
180 screen_label_ = label;
181 bool shortened = false;
182 unsigned int const maxLabelChars = 24;
183 if (screen_label_.size() > maxLabelChars) {
184 screen_label_.erase(maxLabelChars - 3);
185 screen_label_ += "...";
191 tooltip_ = from_ascii("");
195 void InsetRef::addToToc(DocIterator const & cpit)
197 docstring const & label = getParam("reference");
198 if (buffer().insetLabel(label))
199 // This InsetRef has already been taken care of in InsetLabel::addToToc().
202 // It seems that this reference does not point to any valid label.
203 screen_label_ = _("BROKEN: ") + screen_label_;
204 Toc & toc = buffer().tocBackend().toc("label");
205 toc.push_back(TocItem(cpit, 0, screen_label_));
209 void InsetRef::validate(LaTeXFeatures & features) const
211 string const cmd = getCmdName();
212 if (cmd == "vref" || cmd == "vpageref")
213 features.require("varioref");
214 else if (cmd == "prettyref")
215 features.require("prettyref");
216 else if (cmd == "eqref")
217 features.require("amsmath");
218 else if (cmd == "nameref")
219 features.require("nameref");
223 InsetRef::type_info InsetRef::types[] = {
224 { "ref", N_("Standard"), N_("Ref: ")},
225 { "eqref", N_("Equation"), N_("EqRef: ")},
226 { "pageref", N_("Page Number"), N_("Page: ")},
227 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
228 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
229 { "prettyref", N_("PrettyRef"), N_("FrmtRef: ")},
230 { "nameref", N_("Reference to Name"), N_("NameRef:")},
235 int InsetRef::getType(string const & name)
237 for (int i = 0; !types[i].latex_name.empty(); ++i)
238 if (name == types[i].latex_name)
244 string const & InsetRef::getName(int type)
246 return types[type].latex_name;