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 "BufferParams.h"
17 #include "DispatchResult.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")
42 InsetRef::InsetRef(InsetRef const & ir)
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 string const cmd = getCmdName();
75 docstring const ref = getParam("reference");
76 if (cmd != "formatted") {
77 // We don't want to output p_["name"], since that is only used
78 // in docbook. So we construct new params, without it, and use that.
79 InsetCommandParams p(REF_CODE, cmd);
81 os << p.getCommand(runparams);
85 // so we're doing a formatted reference.
86 // the command may need to be escaped.
87 InsetCommandParams const & p = params();
88 ParamInfo const & pi = p.info();
89 ParamInfo::ParamData const & pd = pi["reference"];
90 docstring const data =
91 p.prepareCommand(runparams, ref, pd.handling());
93 if (!buffer().params().use_refstyle) {
94 os << "\\prettyref{" << data << '}';
98 os << "\\lyxref{" << data << '}';
103 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
105 docstring const str = getParam("reference");
106 os << '[' << str << ']';
107 return 2 + str.size();
111 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
113 docstring const & name = getParam("name");
115 if (runparams.flavor == OutputParams::XML) {
116 os << "<xref linkend=\""
117 << sgml::cleanID(buffer(), runparams, getParam("reference"))
120 os << "<xref linkend=\""
121 << sgml::cleanID(buffer(), runparams, getParam("reference"))
125 os << "<link linkend=\""
126 << sgml::cleanID(buffer(), runparams, getParam("reference"))
136 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
138 docstring const & ref = getParam("reference");
139 InsetLabel const * il = buffer().insetLabel(ref);
140 string const & cmd = params().getCmdName();
141 docstring display_string;
143 if (il && !il->counterValue().empty()) {
144 // Try to construct a label from the InsetLabel we reference.
145 docstring const & value = il->counterValue();
147 display_string = value;
148 else if (cmd == "vref")
149 // normally, would be "ref on page #", but we have no pages
150 display_string = value;
151 else if (cmd == "pageref" || cmd == "vpageref")
152 // normally would be "on page #", but we have no pages
153 display_string = _("elsewhere");
154 else if (cmd == "eqref")
155 display_string = bformat(from_ascii("equation (%1$s)"), value);
156 else if (cmd == "prettyref"
157 // we don't really have the ability to handle these
158 // properly in XHTML output
160 display_string = il->prettyCounter();
162 display_string = ref;
164 // FIXME What we'd really like to do is to be able to output some
165 // appropriate sort of text here. But to do that, we need to associate
166 // some sort of counter with the label, and we don't have that yet.
167 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
168 xs << html::StartTag("a", attr);
169 xs << display_string;
170 xs << html::EndTag("a");
175 void InsetRef::tocString(odocstream & os) const
177 plaintext(os, OutputParams(0));
181 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
183 docstring const & ref = getParam("reference");
184 // register this inset into the buffer reference cache.
185 buffer().references(ref).push_back(make_pair(this, it));
188 for (int i = 0; !types[i].latex_name.empty(); ++i) {
189 if (getCmdName() == types[i].latex_name) {
190 label = _(types[i].short_gui_name);
196 if (!buffer().isLatex() && !getParam("name").empty()) {
198 label += getParam("name");
201 screen_label_ = label;
202 bool shortened = false;
203 unsigned int const maxLabelChars = 24;
204 if (screen_label_.size() > maxLabelChars) {
205 screen_label_.erase(maxLabelChars - 3);
206 screen_label_ += "...";
212 tooltip_ = from_ascii("");
216 void InsetRef::addToToc(DocIterator const & cpit)
218 docstring const & label = getParam("reference");
219 if (buffer().insetLabel(label))
220 // This InsetRef has already been taken care of in InsetLabel::addToToc().
223 // It seems that this reference does not point to any valid label.
224 screen_label_ = _("BROKEN: ") + screen_label_;
225 Toc & toc = buffer().tocBackend().toc("label");
226 toc.push_back(TocItem(cpit, 0, screen_label_));
230 void InsetRef::validate(LaTeXFeatures & features) const
232 string const cmd = getCmdName();
233 if (cmd == "vref" || cmd == "vpageref")
234 features.require("varioref");
235 else if (getCmdName() == "formatted") {
236 if (buffer().params().use_refstyle) {
237 features.require("refstyle");
238 features.require("ifthen");
240 features.require("prettyref");
241 } else if (getCmdName() == "eqref" && !buffer().params().use_refstyle)
242 // refstyle defines its own version
243 features.require("amsmath");
244 else if (cmd == "nameref")
245 features.require("nameref");
249 InsetRef::type_info InsetRef::types[] = {
250 { "ref", N_("Standard"), N_("Ref: ")},
251 { "eqref", N_("Equation"), N_("EqRef: ")},
252 { "pageref", N_("Page Number"), N_("Page: ")},
253 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
254 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
255 { "formatted", N_("Formatted"), N_("Format: ")},
256 { "nameref", N_("Reference to Name"), N_("NameRef:")},
261 int InsetRef::getType(string const & name)
263 for (int i = 0; !types[i].latex_name.empty(); ++i)
264 if (name == types[i].latex_name)
270 string const & InsetRef::getName(int type)
272 return types[type].latex_name;