]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
Cocoa based Qt-4.6 needs to paint every character separately to match metrics computa...
[lyx.git] / src / insets / InsetHyperlink.cpp
1 /**
2  * \file InsetHyperlink.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  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetHyperlink.h"
15
16 #include "Buffer.h"
17 #include "DispatchResult.h"
18 #include "Format.h"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "LaTeXFeatures.h"
22 #include "OutputParams.h"
23 #include "output_xhtml.h"
24
25 #include "support/docstream.h"
26 #include "support/FileName.h"
27 #include "support/filetools.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 using namespace std;
32 using namespace lyx::support;
33
34 namespace lyx {
35
36
37 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
38         : InsetCommand(buf, p)
39 {}
40
41
42 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
43 {
44         static ParamInfo param_info_;
45         if (param_info_.empty()) {
46                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
47                 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
48                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
49         }
50         return param_info_;
51 }
52
53
54 docstring InsetHyperlink::screenLabel() const
55 {
56         docstring const temp = from_ascii("Hyperlink: ");
57
58         docstring url;
59
60         url += getParam("name");
61         if (url.empty())
62                 url += getParam("target");
63
64         // elide if long
65         if (url.length() > 30) {
66                 url = url.substr(0, 10) + "..."
67                         + url.substr(url.length() - 17, url.length());
68         }
69         return temp + url;
70 }
71
72 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
73 {
74         switch (cmd.action()) {
75
76         case LFUN_INSET_EDIT:
77                 viewTarget();
78                 break;
79
80         default:
81                 InsetCommand::doDispatch(cur, cmd);
82                 break;
83         }
84 }
85
86
87 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
88                 FuncStatus & flag) const
89 {
90         switch (cmd.action()) {
91         case LFUN_INSET_EDIT:
92                 flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
93                 return true;
94
95         default:
96                 return InsetCommand::getStatus(cur, cmd, flag);
97         }
98 }
99
100
101 void InsetHyperlink::viewTarget() const
102 {
103         if (getParam("type") == "file:") {
104                 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
105                 string const format = formats.getFormatFromFile(url);
106                 formats.view(buffer(), url, format);
107         }
108 }
109
110
111 void InsetHyperlink::latex(otexstream & os,
112                            OutputParams const & runparams) const
113 {
114         docstring url = getParam("target");
115         docstring name = getParam("name");
116         static docstring const backslash = from_ascii("\\");
117         static docstring const braces = from_ascii("{}");
118         static char_type const chars_url[2] = {'%', '#'};
119         static char_type const chars_name[6] = {
120                 '&', '_', '$', '%', '#', '^'};
121
122         // For the case there is no name given, the target is set as name.
123         // Do this before !url.empty() and !name.empty() to handle characters
124         // like the "%" correctly.
125         if (name.empty())
126                 name = url;
127
128         if (!url.empty()) {
129                 // Replace the "\" character by its ASCII code according to the
130                 // URL specifications because "\" is not allowed in URLs and by
131                 // \href. Only do this when the following character is not also
132                 // a "\", because "\\" is valid code
133                 for (size_t i = 0, pos;
134                         (pos = url.find('\\', i)) != string::npos;
135                         i = pos + 2) {
136                         if (url[pos + 1] != '\\')
137                                 url.replace(pos, 1, from_ascii("%5C"));
138                 }
139
140                 // The characters in chars_url[] need to be escaped in the url
141                 // field because otherwise LaTeX will fail when the hyperlink is
142                 // within an argument of another command, e.g. in a \footnote. It
143                 // is important that they are escaped as "\#" and not as "\#{}".
144                 for (int k = 0; k < 2; k++)
145                         for (size_t i = 0, pos;
146                                 (pos = url.find(chars_url[k], i)) != string::npos;
147                                 i = pos + 2)
148                                 url.replace(pos, 1, backslash + chars_url[k]);
149                 
150                 // add "http://" when the type is web (type = empty)
151                 // and no "://" or "run:" is given
152                 docstring type = getParam("type");
153                 if (url.find(from_ascii("://")) == string::npos
154                         && url.find(from_ascii("run:")) == string::npos
155                         && type.empty())
156                         url = from_ascii("http://") + url;
157
158         } // end if (!url.empty())
159
160         // The characters in chars_name[] need to be changed to a command when
161         // they are in the name field.
162         if (!name.empty()) {
163                 // handle the "\" character, but only when the following character
164                 // is not also a "\", because "\\" is valid code
165                 docstring const textbackslash = from_ascii("\\textbackslash{}");
166                 for (size_t i = 0, pos;
167                         (pos = name.find('\\', i)) != string::npos;
168                         i = pos + 2) {
169                         if (name[pos + 1] != '\\')
170                                 name.replace(pos, 1, textbackslash);
171                 }
172                 // The characters in chars_name[] need to be changed to a command
173                 // when they are in the name field.
174                 // Therefore the treatment of "\" must be the first thing
175                 for (int k = 0; k < 6; k++)
176                         for (size_t i = 0, pos;
177                                 (pos = name.find(chars_name[k], i)) != string::npos;
178                                 i = pos + 2)
179                                 name.replace(pos, 1, backslash + chars_name[k] + braces);
180
181                 // replace the tilde by the \sim character as suggested in the
182                 // LaTeX FAQ for URLs
183                 docstring const sim = from_ascii("$\\sim$");
184                 for (size_t i = 0, pos;
185                         (pos = name.find('~', i)) != string::npos;
186                         i = pos + 1)
187                         name.replace(pos, 1, sim);
188
189         }  // end if (!name.empty())
190         
191         if (runparams.moving_arg)
192                 os << "\\protect";
193
194         // output the ready \href command
195         os << "\\href{" << getParam("type") << url << "}{" << name << '}';
196 }
197
198
199 int InsetHyperlink::plaintext(odocstream & os, OutputParams const &) const
200 {
201         odocstringstream oss;
202
203         oss << '[' << getParam("target");
204         if (getParam("name").empty())
205                 oss << ']';
206         else
207                 oss << "||" << getParam("name") << ']';
208
209         docstring const str = oss.str();
210         os << str;
211         return str.size();
212 }
213
214
215 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
216 {
217         os << "<ulink url=\""
218            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
219            << "\">"
220            << getParam("name")
221            << "</ulink>";
222         return 0;
223 }
224
225
226 docstring InsetHyperlink::xhtml(XHTMLStream & xs, OutputParams const &) const
227 {
228         docstring const & target = 
229                 html::htmlize(getParam("target"), XHTMLStream::ESCAPE_AND);
230         docstring const & name   = getParam("name");
231         xs << html::StartTag("a", to_utf8("href=\"" + target + "\""));
232         xs << (name.empty() ? target : name);
233         xs << html::EndTag("a");
234         return docstring();
235 }
236
237
238 void InsetHyperlink::toString(odocstream & os) const
239 {
240         plaintext(os, OutputParams(0));
241 }
242
243
244 void InsetHyperlink::forToc(docstring & os, size_t) const
245 {
246         docstring const & n = getParam("name");
247         if (!n.empty()) {
248                 os += n;
249                 return;
250         }
251         os += getParam("target");
252 }
253
254
255 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
256 {
257         docstring url = getParam("target");
258         docstring type = getParam("type");
259         docstring guitype = _("www");
260         if (type == "mailto:")
261                 guitype = _("email");
262         else if (type == "file:")
263                 guitype = _("file");
264         return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
265 }
266
267
268 void InsetHyperlink::validate(LaTeXFeatures & features) const
269 {
270         features.require("hyperref");
271 }
272
273
274 docstring InsetHyperlink::contextMenuName() const
275 {
276         return from_ascii("context-hyperlink");
277 }
278
279
280 } // namespace lyx