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 "buffer_funcs.h"
17 #include "DispatchResult.h"
18 #include "FuncRequest.h"
19 #include "LaTeXFeatures.h"
21 #include "OutputParams.h"
22 #include "ParIterator.h"
24 #include "TocBackend.h"
26 #include "support/docstream.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
30 using namespace lyx::support;
36 InsetRef::InsetRef(Buffer const & buf, InsetCommandParams const & p)
37 : InsetCommand(p, "ref"), isLatex(buf.isLatex())
41 InsetRef::InsetRef(InsetRef const & ir)
42 : InsetCommand(ir), isLatex(ir.isLatex)
46 void InsetRef::initView()
48 // We need an update of the Buffer reference cache. This is achieved by
50 lyx::updateLabels(buffer());
54 bool InsetRef::isCompatibleCommand(string const & s) {
55 //FIXME This is likely not the best way to handle this.
56 //But this stuff is hardcoded elsewhere already.
66 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
68 static ParamInfo param_info_;
69 if (param_info_.empty()) {
70 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
71 param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
77 void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
80 case LFUN_MOUSE_RELEASE:
81 // Eventually trigger dialog with button 3 not 1
82 if (cmd.button() == mouse_button::button3)
83 lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO,
84 getParam("reference")));
86 InsetCommand::doDispatch(cur, cmd);
90 InsetCommand::doDispatch(cur, cmd);
95 docstring InsetRef::screenLabel() const
101 int InsetRef::latex(odocstream & os, OutputParams const &) const
103 // We don't want to output p_["name"], since that is only used
104 // in docbook. So we construct new params, without it, and use that.
105 InsetCommandParams p(REF_CODE, getCmdName());
106 p["reference"] = getParam("reference");
107 os << escape(p.getCommand());
112 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
114 docstring const str = getParam("reference");
115 os << '[' << str << ']';
116 return 2 + str.size();
120 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
122 docstring const & name = getParam("name");
124 if (runparams.flavor == OutputParams::XML) {
125 os << "<xref linkend=\""
126 << sgml::cleanID(buffer(), runparams, getParam("reference"))
129 os << "<xref linkend=\""
130 << sgml::cleanID(buffer(), runparams, getParam("reference"))
134 os << "<link linkend=\""
135 << sgml::cleanID(buffer(), runparams, getParam("reference"))
145 void InsetRef::textString(odocstream & os) const
147 plaintext(os, OutputParams(0));
151 void InsetRef::updateLabels(ParIterator const & it)
153 docstring const & label = getParam("reference");
154 // register this inset into the buffer reference cache.
155 buffer().references(label).push_back(make_pair(this, it));
157 for (int i = 0; !types[i].latex_name.empty(); ++i) {
158 if (getCmdName() == types[i].latex_name) {
159 screen_label_ = _(types[i].short_gui_name);
163 screen_label_ += getParam("reference");
165 if (!isLatex && !getParam("name").empty()) {
166 screen_label_ += "||";
167 screen_label_ += getParam("name");
172 void InsetRef::addToToc(ParConstIterator const & cpit) const
174 docstring const & label = getParam("reference");
175 if (buffer().insetLabel(label))
176 // This InsetRef has already been taken care of in InsetLabel::addToToc().
179 // It seems that this reference does not point to any valid label.
180 screen_label_ = _("BROKEN: ") + screen_label_;
181 Toc & toc = buffer().tocBackend().toc("label");
182 toc.push_back(TocItem(cpit, 0, screen_label_));
186 void InsetRef::validate(LaTeXFeatures & features) const
188 if (getCmdName() == "vref" || getCmdName() == "vpageref")
189 features.require("varioref");
190 else if (getCmdName() == "prettyref")
191 features.require("prettyref");
192 else if (getCmdName() == "eqref")
193 features.require("amsmath");
197 InsetRef::type_info InsetRef::types[] = {
198 { "ref", N_("Standard"), N_("Ref: ")},
199 { "eqref", N_("Equation"), N_("EqRef: ")},
200 { "pageref", N_("Page Number"), N_("Page: ")},
201 { "vpageref", N_("Textual Page Number"), N_("TextPage: ")},
202 { "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
203 { "prettyref", N_("PrettyRef"), N_("FormatRef: ")},
208 int InsetRef::getType(string const & name)
210 for (int i = 0; !types[i].latex_name.empty(); ++i)
211 if (name == types[i].latex_name)
217 string const & InsetRef::getName(int type)
219 return types[type].latex_name;