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.
61 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
63 static ParamInfo param_info_;
64 if (param_info_.empty()) {
65 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
66 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
67 ParamInfo::HANDLING_ESCAPE);
73 int InsetRef::latex(odocstream & os, OutputParams const & runparams) const
75 // We don't want to output p_["name"], since that is only used
76 // in docbook. So we construct new params, without it, and use that.
77 InsetCommandParams p(REF_CODE, getCmdName());
78 p["reference"] = getParam("reference");
79 os << p.getCommand(runparams);
84 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
86 docstring const str = getParam("reference");
87 os << '[' << str << ']';
88 return 2 + str.size();
92 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
94 docstring const & name = getParam("name");
96 if (runparams.flavor == OutputParams::XML) {
97 os << "<xref linkend=\""
98 << sgml::cleanID(buffer(), runparams, getParam("reference"))
101 os << "<xref linkend=\""
102 << sgml::cleanID(buffer(), runparams, getParam("reference"))
106 os << "<link linkend=\""
107 << sgml::cleanID(buffer(), runparams, getParam("reference"))
117 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
119 docstring const & ref = getParam("reference");
120 InsetLabel const * il = buffer().insetLabel(ref);
121 string const & cmd = params().getCmdName();
122 docstring display_string;
124 if (il && !il->counterValue().empty()) {
125 // Try to construct a label from the InsetLabel we reference.
126 docstring const & value = il->counterValue();
128 display_string = value;
129 else if (cmd == "vref")
130 // normally, would be "ref on page #", but we have no pages
131 display_string = value;
132 else if (cmd == "pageref" || cmd == "vpageref")
133 // normally would be "on page #", but we have no pages
134 display_string = _("elsewhere");
135 else if (cmd == "eqref")
136 display_string = bformat(from_ascii("equation (%1$s)"), value);
137 else if (cmd == "prettyref"
138 // we don't really have the ability to handle these
139 // properly in XHTML output
140 || cmd == "nameref" || cmd == "Nameref")
141 display_string = il->prettyCounter();
143 display_string = ref;
145 // FIXME What we'd really like to do is to be able to output some
146 // appropriate sort of text here. But to do that, we need to associate
147 // some sort of counter with the label, and we don't have that yet.
148 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
149 xs << html::StartTag("a", attr);
150 xs << display_string;
151 xs << html::EndTag("a");
156 void InsetRef::tocString(odocstream & os) const
158 plaintext(os, OutputParams(0));
162 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
164 docstring const & ref = getParam("reference");
165 // register this inset into the buffer reference cache.
166 buffer().references(ref).push_back(make_pair(this, it));
169 for (int i = 0; !types[i].latex_name.empty(); ++i) {
170 if (getCmdName() == types[i].latex_name) {
171 label = _(types[i].short_gui_name);
177 if (!isLatex && !getParam("name").empty()) {
179 label += getParam("name");
182 screen_label_ = label;
183 bool shortened = false;
184 unsigned int const maxLabelChars = 24;
185 if (screen_label_.size() > maxLabelChars) {
186 screen_label_.erase(maxLabelChars - 3);
187 screen_label_ += "...";
193 tooltip_ = from_ascii("");
197 void InsetRef::addToToc(DocIterator const & cpit)
199 docstring const & label = getParam("reference");
200 if (buffer().insetLabel(label))
201 // This InsetRef has already been taken care of in InsetLabel::addToToc().
204 // It seems that this reference does not point to any valid label.
205 screen_label_ = _("BROKEN: ") + screen_label_;
206 Toc & toc = buffer().tocBackend().toc("label");
207 toc.push_back(TocItem(cpit, 0, screen_label_));
211 void InsetRef::validate(LaTeXFeatures & features) const
213 string const cmd = getCmdName();
214 if (cmd == "vref" || cmd == "vpageref")
215 features.require("varioref");
216 else if (cmd == "prettyref")
217 features.require("prettyref");
218 else if (cmd == "eqref")
219 features.require("amsmath");
220 else if (cmd == "nameref" || cmd == "Nameref")
221 features.require("nameref");
225 InsetRef::type_info InsetRef::types[] = {
226 { "ref", N_("Standard"), N_("Ref: ")},
227 { "eqref", N_("Equation"), N_("EqRef: ")},
228 { "pageref", N_("Page Number"), N_("Page: ")},
229 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
230 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
231 { "prettyref", N_("PrettyRef"), N_("FrmtRef: ")},
232 { "nameref", N_("Reference to Name"), N_("NameRef:")},
233 { "Nameref", N_("Name+Textual Page"), N_("NamePgRef:")},
238 int InsetRef::getType(string const & name)
240 for (int i = 0; !types[i].latex_name.empty(); ++i)
241 if (name == types[i].latex_name)
247 string const & InsetRef::getName(int type)
249 return types[type].latex_name;