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