]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/hyperlink.cpp
Update the sample to show how to block navigation and new window events
[wxWidgets.git] / src / gtk / hyperlink.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/hyperlink.cpp
3 // Purpose: Hyperlink control
4 // Author: Francesco Montorsi
5 // Created: 14/2/2007
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // --------------------------------------------------------------------------
16 // headers
17 // --------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_HYPERLINKCTRL && defined(__WXGTK210__) && !defined(__WXUNIVERSAL__)
27
28 #include "wx/hyperlink.h"
29
30 #ifndef WX_PRECOMP
31 #endif
32
33 #include <gtk/gtk.h>
34 #include "wx/gtk/private.h"
35
36 // ----------------------------------------------------------------------------
37 // local functions
38 // ----------------------------------------------------------------------------
39
40 static inline bool UseNative()
41 {
42 // native gtk_link_button widget is only available in GTK+ 2.10 and later
43 return !gtk_check_version(2, 10, 0);
44 }
45
46 // ============================================================================
47 // implementation
48 // ============================================================================
49
50 // ----------------------------------------------------------------------------
51 // "clicked"
52 // ----------------------------------------------------------------------------
53
54 extern "C" {
55 static void gtk_hyperlink_clicked_callback( GtkWidget *WXUNUSED(widget),
56 wxHyperlinkCtrl *linkCtrl )
57 {
58 // send the event
59 linkCtrl->SendEvent();
60 }
61 }
62
63 // ----------------------------------------------------------------------------
64 // wxHyperlinkCtrl
65 // ----------------------------------------------------------------------------
66
67 bool wxHyperlinkCtrl::Create(wxWindow *parent, wxWindowID id,
68 const wxString& label, const wxString& url, const wxPoint& pos,
69 const wxSize& size, long style, const wxString& name)
70 {
71 if ( UseNative() )
72 {
73 // do validation checks:
74 CheckParams(label, url, style);
75
76 if (!PreCreation( parent, pos, size ) ||
77 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
78 {
79 wxFAIL_MSG( wxT("wxHyperlinkCtrl creation failed") );
80 return false;
81 }
82
83 m_widget = gtk_link_button_new("asdfsaf asdfdsaf asdfdsa");
84 g_object_ref(m_widget);
85 gtk_widget_show(m_widget);
86
87 // alignment
88 float x_alignment = 0.5;
89 if (HasFlag(wxHL_ALIGN_LEFT))
90 x_alignment = 0.0;
91 else if (HasFlag(wxHL_ALIGN_RIGHT))
92 x_alignment = 1.0;
93 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, 0.5);
94
95 // set to non empty strings both the url and the label
96 SetURL(url.empty() ? label : url);
97 SetLabel(label.empty() ? url : label);
98
99 // our signal handlers:
100 g_signal_connect_after (m_widget, "clicked",
101 G_CALLBACK (gtk_hyperlink_clicked_callback),
102 this);
103
104 m_parent->DoAddChild( this );
105
106 PostCreation(size);
107
108 // wxWindowGTK will connect to the enter_notify and leave_notify GTK+ signals
109 // thus overriding GTK+'s internal signal handlers which set the cursor of
110 // the widget - thus we need to manually set it here:
111 SetCursor(wxCursor(wxCURSOR_HAND));
112 }
113 else
114 return wxGenericHyperlinkCtrl::Create(parent, id, label, url, pos, size, style, name);
115
116 return true;
117 }
118
119 wxSize wxHyperlinkCtrl::DoGetBestSize() const
120 {
121 if ( UseNative() )
122 return wxControl::DoGetBestSize();
123 return wxGenericHyperlinkCtrl::DoGetBestSize();
124 }
125
126 wxSize wxHyperlinkCtrl::DoGetBestClientSize() const
127 {
128 if ( UseNative() )
129 return wxControl::DoGetBestClientSize();
130 return wxGenericHyperlinkCtrl::DoGetBestClientSize();
131 }
132
133 void wxHyperlinkCtrl::SetLabel(const wxString &label)
134 {
135 if ( UseNative() )
136 {
137 wxControl::SetLabel(label);
138 const wxString labelGTK = GTKConvertMnemonics(label);
139 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
140 }
141 else
142 wxGenericHyperlinkCtrl::SetLabel(label);
143 }
144
145 void wxHyperlinkCtrl::SetURL(const wxString &uri)
146 {
147 if ( UseNative() )
148 gtk_link_button_set_uri(GTK_LINK_BUTTON(m_widget), wxGTK_CONV(uri));
149 else
150 wxGenericHyperlinkCtrl::SetURL(uri);
151 }
152
153 wxString wxHyperlinkCtrl::GetURL() const
154 {
155 if ( UseNative() )
156 {
157 const gchar *str = gtk_link_button_get_uri(GTK_LINK_BUTTON(m_widget));
158 return wxString::FromUTF8(str);
159 }
160
161 return wxGenericHyperlinkCtrl::GetURL();
162 }
163
164 void wxHyperlinkCtrl::SetNormalColour(const wxColour &colour)
165 {
166 if ( UseNative() )
167 {
168 // simply do nothing: GTK+ does not allow us to change it :(
169 }
170 else
171 wxGenericHyperlinkCtrl::SetNormalColour(colour);
172 }
173
174 wxColour wxHyperlinkCtrl::GetNormalColour() const
175 {
176 wxColour ret;
177 if ( UseNative() )
178 {
179 GdkColor *link_color = NULL;
180
181 // convert GdkColor in wxColour
182 gtk_widget_style_get(m_widget, "link-color", &link_color, NULL);
183 if (link_color)
184 ret = wxColour(*link_color);
185 gdk_color_free (link_color);
186 }
187 else
188 ret = wxGenericHyperlinkCtrl::GetNormalColour();
189
190 return ret;
191 }
192
193 void wxHyperlinkCtrl::SetVisitedColour(const wxColour &colour)
194 {
195 if ( UseNative() )
196 {
197 // simply do nothing: GTK+ does not allow us to change it :(
198 }
199 else
200 wxGenericHyperlinkCtrl::SetVisitedColour(colour);
201 }
202
203 wxColour wxHyperlinkCtrl::GetVisitedColour() const
204 {
205 wxColour ret;
206 if ( UseNative() )
207 {
208 GdkColor *link_color = NULL;
209
210 // convert GdkColor in wxColour
211 gtk_widget_style_get(m_widget, "visited-link-color", &link_color, NULL);
212 if (link_color)
213 ret = wxColour(*link_color);
214 gdk_color_free (link_color);
215 }
216 else
217 return wxGenericHyperlinkCtrl::GetVisitedColour();
218
219 return ret;
220 }
221
222 void wxHyperlinkCtrl::SetHoverColour(const wxColour &colour)
223 {
224 if ( UseNative() )
225 {
226 // simply do nothing: GTK+ does not allow us to change it :(
227 }
228 else
229 wxGenericHyperlinkCtrl::SetHoverColour(colour);
230 }
231
232 wxColour wxHyperlinkCtrl::GetHoverColour() const
233 {
234 if ( UseNative() )
235 {
236 // hover colour == normal colour for native GTK+ widget
237 return GetNormalColour();
238 }
239
240 return wxGenericHyperlinkCtrl::GetHoverColour();
241 }
242
243 GdkWindow *wxHyperlinkCtrl::GTKGetWindow(wxArrayGdkWindows& windows) const
244 {
245 return UseNative() ? GTK_BUTTON(m_widget)->event_window
246 : wxGenericHyperlinkCtrl::GTKGetWindow(windows);
247 }
248
249 #endif // wxUSE_HYPERLINKCTRL && GTK+ 2.10+