]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
#5502 add binding for full screen toggle on mac
[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 "BufferParams.h"
16 #include "Cursor.h"
17 #include "DispatchResult.h"
18 #include "InsetLabel.h"
19 #include "Language.h"
20 #include "LaTeXFeatures.h"
21 #include "LyX.h"
22 #include "OutputParams.h"
23 #include "output_xhtml.h"
24 #include "ParIterator.h"
25 #include "sgml.h"
26 #include "TocBackend.h"
27
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32 #include "support/textutils.h"
33
34 using namespace lyx::support;
35 using namespace std;
36
37 namespace lyx {
38
39
40 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
41         : InsetCommand(buf, p)
42 {}
43
44
45 InsetRef::InsetRef(InsetRef const & ir)
46         : InsetCommand(ir)
47 {}
48
49
50 bool InsetRef::isCompatibleCommand(string const & s) {
51         //FIXME This is likely not the best way to handle this.
52         //But this stuff is hardcoded elsewhere already.
53         return s == "ref" 
54                 || s == "pageref"
55                 || s == "vref" 
56                 || s == "vpageref"
57                 || s == "formatted"
58                 || s == "eqref"
59                 || s == "nameref";
60 }
61
62
63 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
64 {
65         static ParamInfo param_info_;
66         if (param_info_.empty()) {
67                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
68                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
69                                 ParamInfo::HANDLING_ESCAPE);
70         }
71         return param_info_;
72 }
73
74
75 // the ref argument is the label name we are referencing.
76 // we expect ref to be in the form: pfx:suffix.
77 //
78 // if it isn't, then we can't produce a formatted reference, 
79 // so we return "\ref" and put ref into label.
80 //
81 // for refstyle, we return "\pfxcmd", and put suffix into 
82 // label and pfx into prefix. this is because refstyle expects
83 // the command: \pfxcmd{suffix}.
84 // 
85 // for prettyref, we return "\prettyref" and put ref into label
86 // and pfx into prefix. this is because prettyref uses the whole
87 // label, thus: \prettyref{pfx:suffix}.
88 //
89 docstring InsetRef::getFormattedCmd(docstring const & ref, 
90         docstring & label, docstring & prefix) const
91 {
92         static docstring const defcmd = from_ascii("\\ref");
93         static docstring const prtcmd = from_ascii("\\prettyref");
94         
95         label = split(ref, prefix, ':');
96
97         // we have to have xxx:xxxxx...
98         if (label.empty()) {
99                 LYXERR0("Label `" << ref << "' contains no prefix.");
100                 label = ref;
101                 prefix = from_ascii("");
102                 return defcmd;
103         }
104
105         if (prefix.empty()) {
106                 // we have ":xxxx"
107                 label = ref;
108                 return defcmd;
109         }
110         
111         if (!buffer().params().use_refstyle) {
112                 // \prettyref uses the whole label
113                 label = ref;
114                 return prtcmd;
115         }
116
117         // make sure the prefix is legal for a latex command
118         int const len = prefix.size();
119         for (int i = 0; i < len; i++) {
120                 char_type const c = prefix[i];
121                 if (!isAlphaASCII(c)) {
122                         LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
123                         // restore the label
124                         label = ref;
125                         return defcmd;
126                 }
127         }
128         return from_ascii("\\") + prefix + from_ascii("ref");
129 }
130
131
132 docstring InsetRef::getEscapedLabel(OutputParams const & rp) const
133 {
134         InsetCommandParams const & p = params();
135         ParamInfo const & pi = p.info();
136         ParamInfo::ParamData const & pd = pi["reference"];
137         return p.prepareCommand(rp, getParam("reference"), pd.handling());              
138 }
139
140
141 void InsetRef::latex(otexstream & os, OutputParams const & rp) const
142 {
143         string const & cmd = getCmdName();
144         docstring const & data = getEscapedLabel(rp);
145
146         if (rp.inulemcmd > 0)
147                 os << "\\mbox{";
148
149         if (cmd == "eqref" && buffer().params().use_refstyle) {
150                 // we advertise this as printing "(n)", so we'll do that, at least
151                 // for refstyle, since refstlye's own \eqref prints, by default,
152                 // "equation n". if one wants \eqref, one can get it by using a
153                 // formatted label in this case.
154                 os << '(' << from_ascii("\\ref{") << data << from_ascii("})");
155         } 
156         else if (cmd == "formatted") {
157                 docstring label;
158                 docstring prefix;
159                 docstring const fcmd = getFormattedCmd(data, label, prefix);
160                 os << fcmd << '{' << label << '}';
161         }
162         else {
163                 // We don't want to output p_["name"], since that is only used 
164                 // in docbook. So we construct new params, without it, and use that.
165                 InsetCommandParams p(REF_CODE, cmd);
166                 docstring const ref = getParam("reference");
167                 p["reference"] = ref;
168                 os << p.getCommand(rp);
169         }
170
171         if (rp.inulemcmd > 0)
172                 os << "}";
173 }
174
175
176 int InsetRef::plaintext(odocstringstream & os,
177         OutputParams const &, size_t) const
178 {
179         docstring const str = getParam("reference");
180         os << '[' << str << ']';
181         return 2 + str.size();
182 }
183
184
185 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
186 {
187         docstring const & name = getParam("name");
188         if (name.empty()) {
189                 if (runparams.flavor == OutputParams::XML) {
190                         os << "<xref linkend=\""
191                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
192                            << "\" />";
193                 } else {
194                         os << "<xref linkend=\""
195                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
196                            << "\">";
197                 }
198         } else {
199                 os << "<link linkend=\""
200                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
201                    << "\">"
202                    << getParam("name")
203                    << "</link>";
204         }
205
206         return 0;
207 }
208
209
210 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
211 {
212         docstring const & ref = getParam("reference");
213         InsetLabel const * il = buffer().insetLabel(ref);
214         string const & cmd = params().getCmdName();
215         docstring display_string;
216
217         if (il && !il->counterValue().empty()) {
218                 // Try to construct a label from the InsetLabel we reference.
219                 docstring const & value = il->counterValue();
220                 if (cmd == "ref")
221                         display_string = value;
222                 else if (cmd == "vref")
223                         // normally, would be "ref on page #", but we have no pages
224                         display_string = value;
225                 else if (cmd == "pageref" || cmd == "vpageref")
226                         // normally would be "on page #", but we have no pages.
227                         display_string = translateIfPossible(from_ascii("elsewhere"),
228                                 op.local_font->language()->lang());
229                 else if (cmd == "eqref")
230                         display_string = '(' + value + ')';
231                 else if (cmd == "formatted")
232                         display_string = il->prettyCounter();
233                 else if (cmd == "nameref")
234                         // FIXME We don't really have the ability to handle these
235                         // properly in XHTML output yet (bug #8599).
236                         // It might not be that hard to do. We have the InsetLabel,
237                         // and we can presumably find its paragraph using the TOC.
238                         // But the label might be referencing a section, yet not be
239                         // in that section. So this is not trivial.
240                         display_string = il->prettyCounter();
241         } else 
242                         display_string = ref;
243
244         // FIXME What we'd really like to do is to be able to output some
245         // appropriate sort of text here. But to do that, we need to associate
246         // some sort of counter with the label, and we don't have that yet.
247         string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
248         xs << html::StartTag("a", attr);
249         xs << display_string;
250         xs << html::EndTag("a");
251         return docstring();
252 }
253
254
255 void InsetRef::toString(odocstream & os) const
256 {
257         odocstringstream ods;
258         plaintext(ods, OutputParams(0));
259         os << ods.str();
260 }
261
262
263 void InsetRef::forOutliner(docstring & os, size_t) const
264 {
265         // There's no need for details in the TOC, and a long label
266         // will just get in the way.
267         os += '#';
268 }
269
270
271 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
272 {
273         docstring const & ref = getParam("reference");
274         // register this inset into the buffer reference cache.
275         buffer().addReference(ref, this, it);
276
277         docstring label;
278         for (int i = 0; !types[i].latex_name.empty(); ++i) {
279                 if (getCmdName() == types[i].latex_name) {
280                         label = _(types[i].short_gui_name);
281                         break;
282                 }
283         }
284         label += ref;
285         
286         if (!buffer().params().isLatex() && !getParam("name").empty()) {
287                 label += "||";
288                 label += getParam("name");
289         }
290         
291         screen_label_ = label;
292         bool shortened = false;
293         unsigned int const maxLabelChars = 24;
294         if (screen_label_.size() > maxLabelChars) {
295                 screen_label_.erase(maxLabelChars - 3);
296                 screen_label_ += "...";
297                 shortened = true;
298         }
299         if (shortened)
300                 tooltip_ = label;
301         else 
302                 tooltip_ = from_ascii("");
303 }
304
305
306 void InsetRef::addToToc(DocIterator const & cpit, bool output_active) const
307 {
308         docstring const & label = getParam("reference");
309         if (buffer().insetLabel(label))
310                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
311                 return;
312
313         // It seems that this reference does not point to any valid label.
314         screen_label_ = _("BROKEN: ") + screen_label_;
315         Toc & toc = buffer().tocBackend().toc("label");
316         toc.push_back(TocItem(cpit, 0, screen_label_, output_active));
317 }
318
319
320 void InsetRef::validate(LaTeXFeatures & features) const
321 {
322         string const cmd = getCmdName();
323         if (cmd == "vref" || cmd == "vpageref")
324                 features.require("varioref");
325         else if (cmd == "formatted") {
326                 docstring const data = getEscapedLabel(features.runparams());
327                 docstring label;
328                 docstring prefix;
329                 string const fcmd = to_utf8(getFormattedCmd(data, label, prefix));
330                 if (buffer().params().use_refstyle) {
331                         features.require("refstyle");
332                         if (prefix == "cha")
333                                 features.addPreambleSnippet("\\let\\charef=\\chapref");
334                         else if (!prefix.empty()) {
335                                 string lcmd = "\\AtBeginDocument{\\providecommand" + 
336                                                 fcmd + "[1]{\\ref{" + to_utf8(prefix) + ":#1}}}";
337                                 features.addPreambleSnippet(lcmd);
338                         }
339                 } else {
340                         features.require("prettyref");
341                         // prettyref uses "cha" for chapters, so we provide a kind of
342                         // translation.
343                         if (prefix == "chap")
344                                 features.addPreambleSnippet("\\let\\pr@chap=\\pr@cha");
345                 }
346         } else if (cmd == "eqref" && !buffer().params().use_refstyle)
347                 // with refstyle, we simply output "(\ref{label})"
348                 features.require("amsmath");
349         else if (cmd == "nameref")
350                 features.require("nameref");
351 }
352
353
354 InsetRef::type_info const InsetRef::types[] = {
355         { "ref",       N_("Standard"),              N_("Ref: ")},
356         { "eqref",     N_("Equation"),              N_("EqRef: ")},
357         { "pageref",   N_("Page Number"),           N_("Page: ")},
358         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
359         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
360         { "formatted", N_("Formatted"),             N_("Format: ")},
361         { "nameref",   N_("Reference to Name"),     N_("NameRef:")},
362         { "", "", "" }
363 };
364
365
366 int InsetRef::getType(string const & name)
367 {
368         for (int i = 0; !types[i].latex_name.empty(); ++i)
369                 if (name == types[i].latex_name)
370                         return i;
371         return 0;
372 }
373
374
375 string const & InsetRef::getName(int type)
376 {
377         return types[type].latex_name;
378 }
379
380
381 docstring InsetRef::getTOCString() const 
382 {
383         return tooltip_.empty() ? screen_label_ : tooltip_;
384 }
385
386 } // namespace lyx