1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/hyperlink.cpp
3 // Purpose: Hyperlink control
4 // Author: Francesco Montorsi
7 // Copyright: (c) 2007 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // --------------------------------------------------------------------------
17 // --------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_HYPERLINKCTRL && defined(__WXGTK210__) && !defined(__WXUNIVERSAL__)
28 #include "wx/hyperlink.h"
34 #include "wx/gtk/private.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 static inline bool UseNative()
42 // native gtk_link_button widget is only available in GTK+ 2.10 and later
43 return !gtk_check_version(2, 10, 0);
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
55 static void gtk_hyperlink_clicked_callback( GtkWidget
*WXUNUSED(widget
),
56 wxHyperlinkCtrl
*linkCtrl
)
59 linkCtrl
->SendEvent();
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
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
)
73 // do validation checks:
74 CheckParams(label
, url
, style
);
76 if (!PreCreation( parent
, pos
, size
) ||
77 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
79 wxFAIL_MSG( wxT("wxHyperlinkCtrl creation failed") );
83 m_widget
= gtk_link_button_new("asdfsaf asdfdsaf asdfdsa");
84 g_object_ref(m_widget
);
85 gtk_widget_show(m_widget
);
88 float x_alignment
= 0.5;
89 if (HasFlag(wxHL_ALIGN_LEFT
))
91 else if (HasFlag(wxHL_ALIGN_RIGHT
))
93 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, 0.5);
95 // set to non empty strings both the url and the label
96 SetURL(url
.empty() ? label
: url
);
97 SetLabel(label
.empty() ? url
: label
);
99 // our signal handlers:
100 g_signal_connect_after (m_widget
, "clicked",
101 G_CALLBACK (gtk_hyperlink_clicked_callback
),
104 m_parent
->DoAddChild( this );
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
));
114 return wxGenericHyperlinkCtrl::Create(parent
, id
, label
, url
, pos
, size
, style
, name
);
119 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
122 return wxControl::DoGetBestSize();
123 return wxGenericHyperlinkCtrl::DoGetBestSize();
126 wxSize
wxHyperlinkCtrl::DoGetBestClientSize() const
129 return wxControl::DoGetBestClientSize();
130 return wxGenericHyperlinkCtrl::DoGetBestClientSize();
133 void wxHyperlinkCtrl::SetLabel(const wxString
&label
)
137 wxControl::SetLabel(label
);
138 const wxString labelGTK
= GTKConvertMnemonics(label
);
139 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
142 wxGenericHyperlinkCtrl::SetLabel(label
);
145 void wxHyperlinkCtrl::SetURL(const wxString
&uri
)
148 gtk_link_button_set_uri(GTK_LINK_BUTTON(m_widget
), wxGTK_CONV(uri
));
150 wxGenericHyperlinkCtrl::SetURL(uri
);
153 wxString
wxHyperlinkCtrl::GetURL() const
157 const gchar
*str
= gtk_link_button_get_uri(GTK_LINK_BUTTON(m_widget
));
158 return wxString::FromUTF8(str
);
161 return wxGenericHyperlinkCtrl::GetURL();
164 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
168 // simply do nothing: GTK+ does not allow us to change it :(
171 wxGenericHyperlinkCtrl::SetNormalColour(colour
);
174 wxColour
wxHyperlinkCtrl::GetNormalColour() const
179 GdkColor
*link_color
= NULL
;
181 // convert GdkColor in wxColour
182 gtk_widget_style_get(m_widget
, "link-color", &link_color
, NULL
);
184 ret
= wxColour(*link_color
);
185 gdk_color_free (link_color
);
188 ret
= wxGenericHyperlinkCtrl::GetNormalColour();
193 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
197 // simply do nothing: GTK+ does not allow us to change it :(
200 wxGenericHyperlinkCtrl::SetVisitedColour(colour
);
203 wxColour
wxHyperlinkCtrl::GetVisitedColour() const
208 GdkColor
*link_color
= NULL
;
210 // convert GdkColor in wxColour
211 gtk_widget_style_get(m_widget
, "visited-link-color", &link_color
, NULL
);
213 ret
= wxColour(*link_color
);
214 gdk_color_free (link_color
);
217 return wxGenericHyperlinkCtrl::GetVisitedColour();
222 void wxHyperlinkCtrl::SetHoverColour(const wxColour
&colour
)
226 // simply do nothing: GTK+ does not allow us to change it :(
229 wxGenericHyperlinkCtrl::SetHoverColour(colour
);
232 wxColour
wxHyperlinkCtrl::GetHoverColour() const
236 // hover colour == normal colour for native GTK+ widget
237 return GetNormalColour();
240 return wxGenericHyperlinkCtrl::GetHoverColour();
243 GdkWindow
*wxHyperlinkCtrl::GTKGetWindow(wxArrayGdkWindows
& windows
) const
245 return UseNative() ? GTK_BUTTON(m_widget
)->event_window
246 : wxGenericHyperlinkCtrl::GTKGetWindow(windows
);
249 #endif // wxUSE_HYPERLINKCTRL && GTK+ 2.10+