]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
85100e809918be6e8ebfa41229d2356de81dd2f1
[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 "InsetLabel.h"
19 #include "LaTeXFeatures.h"
20 #include "LyX.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23 #include "ParIterator.h"
24 #include "sgml.h"
25 #include "TocBackend.h"
26
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 using namespace lyx::support;
32 using namespace std;
33
34 namespace lyx {
35
36
37 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
38         : InsetCommand(buf, p, "ref"), isLatex(buf->isLatex())
39 {}
40
41
42 InsetRef::InsetRef(InsetRef const & ir)
43         : InsetCommand(ir), isLatex(ir.isLatex)
44 {}
45
46
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.
50         return s == "ref" 
51                 || s == "pageref"
52                 || s == "vref" 
53                 || s == "vpageref"
54                 || s == "prettyref"
55                 || s == "eqref"
56                 || s == "nameref"
57                 || s == "Nameref";
58 }
59
60
61 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
62 {
63         static ParamInfo param_info_;
64         if (param_info_.empty()) {
65                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
66                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
67                                 ParamInfo::HANDLING_ESCAPE);
68         }
69         return param_info_;
70 }
71
72
73 int InsetRef::latex(odocstream & os, OutputParams const & runparams) const
74 {
75         // We don't want to output p_["name"], since that is only used 
76         // in docbook. So we construct new params, without it, and use that.
77         InsetCommandParams p(REF_CODE, getCmdName());
78         p["reference"] = getParam("reference");
79         os << p.getCommand(runparams);
80         return 0;
81 }
82
83
84 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
85 {
86         docstring const str = getParam("reference");
87         os << '[' << str << ']';
88         return 2 + str.size();
89 }
90
91
92 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
93 {
94         docstring const & name = getParam("name");
95         if (name.empty()) {
96                 if (runparams.flavor == OutputParams::XML) {
97                         os << "<xref linkend=\""
98                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
99                            << "\" />";
100                 } else {
101                         os << "<xref linkend=\""
102                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
103                            << "\">";
104                 }
105         } else {
106                 os << "<link linkend=\""
107                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
108                    << "\">"
109                    << getParam("name")
110                    << "</link>";
111         }
112
113         return 0;
114 }
115
116
117 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
118 {
119         docstring const & ref = getParam("reference");
120         InsetLabel const * il = buffer().insetLabel(ref);
121         string const & cmd = params().getCmdName();
122         docstring display_string;
123
124         if (il && !il->counterValue().empty()) {
125                 // Try to construct a label from the InsetLabel we reference.
126                 docstring const & value = il->counterValue();
127                 if (cmd == "ref")
128                         display_string = value;
129                 else if (cmd == "vref")
130                         // normally, would be "ref on page #", but we have no pages
131                         display_string = value;
132                 else if (cmd == "pageref" || cmd == "vpageref")
133                         // normally would be "on page #", but we have no pages
134                         display_string = _("elsewhere");
135                 else if (cmd == "eqref")
136                         display_string = bformat(from_ascii("equation (%1$s)"), value);
137                 else if (cmd == "prettyref" 
138                          // we don't really have the ability to handle these 
139                          // properly in XHTML output
140                          || cmd == "nameref" || cmd == "Nameref")
141                         display_string = il->prettyCounter();
142         } else 
143                         display_string = ref;
144
145         // FIXME What we'd really like to do is to be able to output some
146         // appropriate sort of text here. But to do that, we need to associate
147         // some sort of counter with the label, and we don't have that yet.
148         string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
149         xs << html::StartTag("a", attr);
150         xs << display_string;
151         xs << html::EndTag("a");
152         return docstring();
153 }
154
155
156 void InsetRef::tocString(odocstream & os) const
157 {
158         plaintext(os, OutputParams(0));
159 }
160
161
162 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
163 {
164         docstring const & ref = getParam("reference");
165         // register this inset into the buffer reference cache.
166         buffer().references(ref).push_back(make_pair(this, it));
167
168         docstring label;
169         for (int i = 0; !types[i].latex_name.empty(); ++i) {
170                 if (getCmdName() == types[i].latex_name) {
171                         label = _(types[i].short_gui_name);
172                         break;
173                 }
174         }
175         label += ref;
176
177         if (!isLatex && !getParam("name").empty()) {
178                 label += "||";
179                 label += getParam("name");
180         }
181         
182         screen_label_ = label;
183         bool shortened = false;
184         unsigned int const maxLabelChars = 24;
185         if (screen_label_.size() > maxLabelChars) {
186                 screen_label_.erase(maxLabelChars - 3);
187                 screen_label_ += "...";
188                 shortened = true;
189         }
190         if (shortened)
191                 tooltip_ = label;
192         else 
193                 tooltip_ = from_ascii("");
194 }
195
196
197 void InsetRef::addToToc(DocIterator const & cpit)
198 {
199         docstring const & label = getParam("reference");
200         if (buffer().insetLabel(label))
201                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
202                 return;
203
204         // It seems that this reference does not point to any valid label.
205         screen_label_ = _("BROKEN: ") + screen_label_;
206         Toc & toc = buffer().tocBackend().toc("label");
207         toc.push_back(TocItem(cpit, 0, screen_label_));
208 }
209
210
211 void InsetRef::validate(LaTeXFeatures & features) const
212 {
213         string const cmd = getCmdName();
214         if (cmd == "vref" || cmd == "vpageref")
215                 features.require("varioref");
216         else if (cmd == "prettyref")
217                 features.require("prettyref");
218         else if (cmd == "eqref")
219                 features.require("amsmath");
220         else if (cmd == "nameref" || cmd == "Nameref")
221                 features.require("nameref");
222 }
223
224
225 InsetRef::type_info InsetRef::types[] = {
226         { "ref",       N_("Standard"),              N_("Ref: ")},
227         { "eqref",     N_("Equation"),              N_("EqRef: ")},
228         { "pageref",   N_("Page Number"),           N_("Page: ")},
229         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
230         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
231         { "prettyref", N_("PrettyRef"),             N_("FrmtRef: ")},
232         { "nameref",   N_("Reference to Name"),     N_("NameRef:")},
233         { "Nameref",   N_("Name+Textual Page"),     N_("NamePgRef:")},
234         { "", "", "" }
235 };
236
237
238 int InsetRef::getType(string const & name)
239 {
240         for (int i = 0; !types[i].latex_name.empty(); ++i)
241                 if (name == types[i].latex_name)
242                         return i;
243         return 0;
244 }
245
246
247 string const & InsetRef::getName(int type)
248 {
249         return types[type].latex_name;
250 }
251
252
253 } // namespace lyx