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 "InsetLabel.h"
19 #include "LaTeXFeatures.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23 #include "ParIterator.h"
25 #include "TocBackend.h"
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
31 using namespace lyx::support;
37 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
38 : InsetCommand(buf, p, "ref"), isLatex(buf->isLatex())
42 InsetRef::InsetRef(InsetRef const & ir)
43 : InsetCommand(ir), isLatex(ir.isLatex)
47 bool InsetRef::isCompatibleCommand(string const & s) {
48 //FIXME This is likely not the best way to handle this.
49 //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 { // "prettyref"
136 display_string = il->prettyCounter();
139 display_string = ref;
141 // FIXME What we'd really like to do is to be able to output some
142 // appropriate sort of text here. But to do that, we need to associate
143 // some sort of counter with the label, and we don't have that yet.
144 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
145 xs << html::StartTag("a", attr);
146 xs << display_string;
147 xs << html::EndTag("a");
152 void InsetRef::tocString(odocstream & os) const
154 plaintext(os, OutputParams(0));
158 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
160 docstring const & ref = getParam("reference");
161 // register this inset into the buffer reference cache.
162 buffer().references(ref).push_back(make_pair(this, it));
165 for (int i = 0; !types[i].latex_name.empty(); ++i) {
166 if (getCmdName() == types[i].latex_name) {
167 label = _(types[i].short_gui_name);
173 if (!isLatex && !getParam("name").empty()) {
175 label += getParam("name");
178 screen_label_ = label;
179 bool shortened = false;
180 unsigned int const maxLabelChars = 24;
181 if (screen_label_.size() > maxLabelChars) {
182 screen_label_.erase(maxLabelChars - 3);
183 screen_label_ += "...";
189 tooltip_ = from_ascii("");
193 void InsetRef::addToToc(DocIterator const & cpit)
195 docstring const & label = getParam("reference");
196 if (buffer().insetLabel(label))
197 // This InsetRef has already been taken care of in InsetLabel::addToToc().
200 // It seems that this reference does not point to any valid label.
201 screen_label_ = _("BROKEN: ") + screen_label_;
202 Toc & toc = buffer().tocBackend().toc("label");
203 toc.push_back(TocItem(cpit, 0, screen_label_));
207 void InsetRef::validate(LaTeXFeatures & features) const
209 if (getCmdName() == "vref" || getCmdName() == "vpageref")
210 features.require("varioref");
211 else if (getCmdName() == "prettyref")
212 features.require("prettyref");
213 else if (getCmdName() == "eqref")
214 features.require("amsmath");
218 InsetRef::type_info InsetRef::types[] = {
219 { "ref", N_("Standard"), N_("Ref: ")},
220 { "eqref", N_("Equation"), N_("EqRef: ")},
221 { "pageref", N_("Page Number"), N_("Page: ")},
222 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
223 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
224 { "prettyref", N_("PrettyRef"), N_("FrmtRef: ")},
229 int InsetRef::getType(string const & name)
231 for (int i = 0; !types[i].latex_name.empty(); ++i)
232 if (name == types[i].latex_name)
238 string const & InsetRef::getName(int type)
240 return types[type].latex_name;