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 "LaTeXFeatures.h"
20 #include "OutputParams.h"
21 #include "ParIterator.h"
23 #include "TocBackend.h"
25 #include "support/docstream.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
29 using namespace lyx::support;
35 InsetRef::InsetRef(InsetCommandParams const & p, Buffer const & buf)
36 : InsetCommand(p, "ref"), isLatex(buf.isLatex())
40 InsetRef::InsetRef(InsetRef const & ir)
41 : InsetCommand(ir), isLatex(ir.isLatex)
45 bool InsetRef::isCompatibleCommand(string const & s) {
46 //FIXME This is likely not the best way to handle this.
47 //But this stuff is hardcoded elsewhere already.
57 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
59 static ParamInfo param_info_;
60 if (param_info_.empty()) {
61 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
62 param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
68 void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
71 case LFUN_MOUSE_RELEASE:
72 // Eventually trigger dialog with button 3 not 1
73 if (cmd.button() == mouse_button::button3)
74 lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO,
75 getParam("reference")));
77 InsetCommand::doDispatch(cur, cmd);
81 InsetCommand::doDispatch(cur, cmd);
86 docstring InsetRef::screenLabel() const
89 for (int i = 0; !types[i].latex_name.empty(); ++i) {
90 if (getCmdName() == types[i].latex_name) {
91 temp = _(types[i].short_gui_name);
95 temp += getParam("reference");
97 if (!isLatex && !getParam("name").empty()) {
99 temp += getParam("name");
105 int InsetRef::latex(odocstream & os, OutputParams const &) const
107 // We don't want to output p_["name"], since that is only used
108 // in docbook. So we construct new params, without it, and use that.
109 InsetCommandParams p(REF_CODE, getCmdName());
110 p["reference"] = getParam("reference");
111 os << escape(p.getCommand());
116 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
118 docstring const str = getParam("reference");
119 os << '[' << str << ']';
120 return 2 + str.size();
124 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
126 docstring const & name = getParam("name");
128 if (runparams.flavor == OutputParams::XML) {
129 os << "<xref linkend=\""
130 << sgml::cleanID(buffer(), runparams, getParam("reference"))
133 os << "<xref linkend=\""
134 << sgml::cleanID(buffer(), runparams, getParam("reference"))
138 os << "<link linkend=\""
139 << sgml::cleanID(buffer(), runparams, getParam("reference"))
149 void InsetRef::textString(odocstream & os) const
151 plaintext(os, OutputParams(0));
155 void InsetRef::updateLabels(ParIterator const & it)
157 docstring const & label = getParam("reference");
158 buffer().references(label).push_back(make_pair(this, it));
162 void InsetRef::addToToc(ParConstIterator const & cpit) const
164 docstring const & label = getParam("reference");
165 if (buffer().insetLabel(label))
166 // This InsetRef has already been taken care of in InsetLabel::addToToc().
169 Toc & toc = buffer().tocBackend().toc("label");
170 docstring const reflabel = _("BROKEN: ") + screenLabel();
171 toc.push_back(TocItem(cpit, 0, reflabel));
175 void InsetRef::validate(LaTeXFeatures & features) const
177 if (getCmdName() == "vref" || getCmdName() == "vpageref")
178 features.require("varioref");
179 else if (getCmdName() == "prettyref")
180 features.require("prettyref");
181 else if (getCmdName() == "eqref")
182 features.require("amsmath");
186 InsetRef::type_info InsetRef::types[] = {
187 { "ref", N_("Standard"), N_("Ref: ")},
188 { "eqref", N_("Equation"), N_("EqRef: ")},
189 { "pageref", N_("Page Number"), N_("Page: ")},
190 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
191 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
192 { "prettyref", N_("PrettyRef"), N_("FormatRef: ")},
197 int InsetRef::getType(string const & name)
199 for (int i = 0; !types[i].latex_name.empty(); ++i)
200 if (name == types[i].latex_name)
206 string const & InsetRef::getName(int type)
208 return types[type].latex_name;