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"), 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 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 if (!buffer().params().use_refstyle) {
87 os << "\\prettyref{" << ref << '}';
91 os << "\\lyxref{" << ref << '}';
96 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
98 docstring const str = getParam("reference");
99 os << '[' << str << ']';
100 return 2 + str.size();
104 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
106 docstring const & name = getParam("name");
108 if (runparams.flavor == OutputParams::XML) {
109 os << "<xref linkend=\""
110 << sgml::cleanID(buffer(), runparams, getParam("reference"))
113 os << "<xref linkend=\""
114 << sgml::cleanID(buffer(), runparams, getParam("reference"))
118 os << "<link linkend=\""
119 << sgml::cleanID(buffer(), runparams, getParam("reference"))
129 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
131 docstring const & ref = getParam("reference");
132 InsetLabel const * il = buffer().insetLabel(ref);
133 string const & cmd = params().getCmdName();
134 docstring display_string;
136 if (il && !il->counterValue().empty()) {
137 // Try to construct a label from the InsetLabel we reference.
138 docstring const & value = il->counterValue();
140 display_string = value;
141 else if (cmd == "vref")
142 // normally, would be "ref on page #", but we have no pages
143 display_string = value;
144 else if (cmd == "pageref" || cmd == "vpageref")
145 // normally would be "on page #", but we have no pages
146 display_string = _("elsewhere");
147 else if (cmd == "eqref")
148 display_string = bformat(from_ascii("equation (%1$s)"), value);
149 else if (cmd == "prettyref"
150 // we don't really have the ability to handle these
151 // properly in XHTML output
153 display_string = il->prettyCounter();
155 display_string = ref;
157 // FIXME What we'd really like to do is to be able to output some
158 // appropriate sort of text here. But to do that, we need to associate
159 // some sort of counter with the label, and we don't have that yet.
160 string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
161 xs << html::StartTag("a", attr);
162 xs << display_string;
163 xs << html::EndTag("a");
168 void InsetRef::tocString(odocstream & os) const
170 plaintext(os, OutputParams(0));
174 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
176 docstring const & ref = getParam("reference");
177 // register this inset into the buffer reference cache.
178 buffer().references(ref).push_back(make_pair(this, it));
181 for (int i = 0; !types[i].latex_name.empty(); ++i) {
182 if (getCmdName() == types[i].latex_name) {
183 label = _(types[i].short_gui_name);
189 if (!isLatex && !getParam("name").empty()) {
191 label += getParam("name");
194 screen_label_ = label;
195 bool shortened = false;
196 unsigned int const maxLabelChars = 24;
197 if (screen_label_.size() > maxLabelChars) {
198 screen_label_.erase(maxLabelChars - 3);
199 screen_label_ += "...";
205 tooltip_ = from_ascii("");
209 void InsetRef::addToToc(DocIterator const & cpit)
211 docstring const & label = getParam("reference");
212 if (buffer().insetLabel(label))
213 // This InsetRef has already been taken care of in InsetLabel::addToToc().
216 // It seems that this reference does not point to any valid label.
217 screen_label_ = _("BROKEN: ") + screen_label_;
218 Toc & toc = buffer().tocBackend().toc("label");
219 toc.push_back(TocItem(cpit, 0, screen_label_));
223 void InsetRef::validate(LaTeXFeatures & features) const
225 string const cmd = getCmdName();
226 if (cmd == "vref" || cmd == "vpageref")
227 features.require("varioref");
228 else if (getCmdName() == "formatted") {
229 if (buffer().params().use_refstyle) {
230 features.require("refstyle");
231 features.require("ifthen");
233 features.require("prettyref");
234 } else if (getCmdName() == "eqref" && !buffer().params().use_refstyle)
235 // refstyle defines its own version
236 features.require("amsmath");
237 else if (cmd == "nameref")
238 features.require("nameref");
242 InsetRef::type_info InsetRef::types[] = {
243 { "ref", N_("Standard"), N_("Ref: ")},
244 { "eqref", N_("Equation"), N_("EqRef: ")},
245 { "pageref", N_("Page Number"), N_("Page: ")},
246 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
247 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
248 { "formatted", N_("Formatted"), N_("Format: ")},
249 { "nameref", N_("Reference to Name"), N_("NameRef:")},
254 int InsetRef::getType(string const & name)
256 for (int i = 0; !types[i].latex_name.empty(); ++i)
257 if (name == types[i].latex_name)
263 string const & InsetRef::getName(int type)
265 return types[type].latex_name;