]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
7e0eb6db33f8dc78092998e30294916cd70ab800
[lyx.git] / src / insets / InsetRef.cpp
1 /**
2  * \file InsetRef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include <config.h>
11
12 #include "InsetRef.h"
13
14 #include "Buffer.h"
15 #include "Cursor.h"
16 #include "DispatchResult.h"
17 #include "FuncRequest.h"
18 #include "LaTeXFeatures.h"
19 #include "LyXFunc.h"
20 #include "OutputParams.h"
21 #include "output_xhtml.h"
22 #include "ParIterator.h"
23 #include "sgml.h"
24 #include "TocBackend.h"
25
26 #include "support/docstream.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29
30 using namespace lyx::support;
31 using namespace std;
32
33 namespace lyx {
34
35
36 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
37         : InsetCommand(buf, p, "ref"), isLatex(buf->isLatex())
38 {}
39
40
41 InsetRef::InsetRef(InsetRef const & ir)
42         : InsetCommand(ir), isLatex(ir.isLatex)
43 {}
44
45
46 bool InsetRef::isCompatibleCommand(string const & s) {
47         //FIXME This is likely not the best way to handle this.
48         //But this stuff is hardcoded elsewhere already.
49         return s == "ref" 
50                 || s == "pageref"
51                 || s == "vref" 
52                 || s == "vpageref"
53                 || s == "prettyref"
54                 || s == "eqref";
55 }
56
57
58 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
59 {
60         static ParamInfo param_info_;
61         if (param_info_.empty()) {
62                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
63                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
64         }
65         return param_info_;
66 }
67
68
69 docstring InsetRef::screenLabel() const
70 {
71         return screen_label_;
72 }
73
74
75 int InsetRef::latex(odocstream & os, OutputParams const &) const
76 {
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, getCmdName());
80         p["reference"] = getParam("reference");
81         os << escape(p.getCommand());
82         return 0;
83 }
84
85
86 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
87 {
88         docstring const str = getParam("reference");
89         os << '[' << str << ']';
90         return 2 + str.size();
91 }
92
93
94 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
95 {
96         docstring const & name = getParam("name");
97         if (name.empty()) {
98                 if (runparams.flavor == OutputParams::XML) {
99                         os << "<xref linkend=\""
100                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
101                            << "\" />";
102                 } else {
103                         os << "<xref linkend=\""
104                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
105                            << "\">";
106                 }
107         } else {
108                 os << "<link linkend=\""
109                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
110                    << "\">"
111                    << getParam("name")
112                    << "</link>";
113         }
114
115         return 0;
116 }
117
118
119 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
120 {
121         // FIXME What we'd really like to do is to be able to output some
122         // appropriate sort of text here. But to do that, we need to associate
123         // some sort of counter with the label, and we don't have that yet.
124         docstring const ref = html::cleanAttr(getParam("reference"));
125         string const attr = "href=\"#" + to_utf8(ref) + "\"";
126         xs << StartTag("a", attr);
127         xs << ref;
128         xs << EndTag("a");
129         return docstring();
130 }
131
132
133 void InsetRef::tocString(odocstream & os) const
134 {
135         plaintext(os, OutputParams(0));
136 }
137
138
139 void InsetRef::updateLabels(ParIterator const & it, bool)
140 {
141         docstring const & label = getParam("reference");
142         // register this inset into the buffer reference cache.
143         buffer().references(label).push_back(make_pair(this, it));
144
145         for (int i = 0; !types[i].latex_name.empty(); ++i) {
146                 if (getCmdName() == types[i].latex_name) {
147                         screen_label_ = _(types[i].short_gui_name);
148                         break;
149                 }
150         }
151         screen_label_ += label;
152
153         if (!isLatex && !getParam("name").empty()) {
154                 screen_label_ += "||";
155                 screen_label_ += getParam("name");
156         }
157 }
158
159
160 void InsetRef::addToToc(DocIterator const & cpit)
161 {
162         docstring const & label = getParam("reference");
163         if (buffer().insetLabel(label))
164                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
165                 return;
166
167         // It seems that this reference does not point to any valid label.
168         screen_label_ = _("BROKEN: ") + screen_label_;
169         Toc & toc = buffer().tocBackend().toc("label");
170         toc.push_back(TocItem(cpit, 0, screen_label_));
171 }
172
173
174 void InsetRef::validate(LaTeXFeatures & features) const
175 {
176         if (getCmdName() == "vref" || getCmdName() == "vpageref")
177                 features.require("varioref");
178         else if (getCmdName() == "prettyref")
179                 features.require("prettyref");
180         else if (getCmdName() == "eqref")
181                 features.require("amsmath");
182 }
183
184
185 InsetRef::type_info InsetRef::types[] = {
186         { "ref",       N_("Standard"),              N_("Ref: ")},
187         { "eqref",     N_("Equation"),              N_("EqRef: ")},
188         { "pageref",   N_("Page Number"),           N_("Page: ")},
189         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
190         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
191         { "prettyref", N_("PrettyRef"),             N_("FormatRef: ")},
192         { "", "", "" }
193 };
194
195
196 int InsetRef::getType(string const & name)
197 {
198         for (int i = 0; !types[i].latex_name.empty(); ++i)
199                 if (name == types[i].latex_name)
200                         return i;
201         return 0;
202 }
203
204
205 string const & InsetRef::getName(int type)
206 {
207         return types[type].latex_name;
208 }
209
210
211 } // namespace lyx