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
46 return !gtk_check_version(2, 10, 0);
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
59 static void gtk_hyperlink_clicked_callback( GtkWidget
*WXUNUSED(widget
),
60 wxHyperlinkCtrl
*linkCtrl
)
63 linkCtrl
->SendEvent();
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 bool wxHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
72 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
73 const wxSize
& size
, long style
, const wxString
& name
)
77 // do validation checks:
78 CheckParams(label
, url
, style
);
80 if (!PreCreation( parent
, pos
, size
) ||
81 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
83 wxFAIL_MSG( wxT("wxHyperlinkCtrl creation failed") );
87 m_widget
= gtk_link_button_new("asdfsaf asdfdsaf asdfdsa");
88 g_object_ref(m_widget
);
91 float x_alignment
= 0.5;
92 if (HasFlag(wxHL_ALIGN_LEFT
))
94 else if (HasFlag(wxHL_ALIGN_RIGHT
))
96 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, 0.5);
98 // set to non empty strings both the url and the label
99 SetURL(url
.empty() ? label
: url
);
100 SetLabel(label
.empty() ? url
: label
);
102 // our signal handlers:
103 g_signal_connect_after (m_widget
, "clicked",
104 G_CALLBACK (gtk_hyperlink_clicked_callback
),
107 m_parent
->DoAddChild( this );
111 // wxWindowGTK will connect to the enter_notify and leave_notify GTK+ signals
112 // thus overriding GTK+'s internal signal handlers which set the cursor of
113 // the widget - thus we need to manually set it here:
114 SetCursor(wxCursor(wxCURSOR_HAND
));
117 return wxGenericHyperlinkCtrl::Create(parent
, id
, label
, url
, pos
, size
, style
, name
);
122 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
125 return wxControl::DoGetBestSize();
126 return wxGenericHyperlinkCtrl::DoGetBestSize();
129 wxSize
wxHyperlinkCtrl::DoGetBestClientSize() const
132 return wxControl::DoGetBestClientSize();
133 return wxGenericHyperlinkCtrl::DoGetBestClientSize();
136 void wxHyperlinkCtrl::SetLabel(const wxString
&label
)
140 wxControl::SetLabel(label
);
141 const wxString labelGTK
= GTKConvertMnemonics(label
);
142 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
145 wxGenericHyperlinkCtrl::SetLabel(label
);
148 void wxHyperlinkCtrl::SetURL(const wxString
&uri
)
151 gtk_link_button_set_uri(GTK_LINK_BUTTON(m_widget
), wxGTK_CONV(uri
));
153 wxGenericHyperlinkCtrl::SetURL(uri
);
156 wxString
wxHyperlinkCtrl::GetURL() const
160 const gchar
*str
= gtk_link_button_get_uri(GTK_LINK_BUTTON(m_widget
));
161 return wxString::FromUTF8(str
);
164 return wxGenericHyperlinkCtrl::GetURL();
167 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
171 // simply do nothing: GTK+ does not allow us to change it :(
174 wxGenericHyperlinkCtrl::SetNormalColour(colour
);
177 wxColour
wxHyperlinkCtrl::GetNormalColour() const
182 GdkColor
*link_color
= NULL
;
184 // convert GdkColor in wxColour
185 gtk_widget_style_get(m_widget
, "link-color", &link_color
, NULL
);
187 ret
= wxColour(*link_color
);
188 gdk_color_free (link_color
);
191 ret
= wxGenericHyperlinkCtrl::GetNormalColour();
196 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
200 // simply do nothing: GTK+ does not allow us to change it :(
203 wxGenericHyperlinkCtrl::SetVisitedColour(colour
);
206 wxColour
wxHyperlinkCtrl::GetVisitedColour() const
211 GdkColor
*link_color
= NULL
;
213 // convert GdkColor in wxColour
214 gtk_widget_style_get(m_widget
, "visited-link-color", &link_color
, NULL
);
216 ret
= wxColour(*link_color
);
217 gdk_color_free (link_color
);
220 return wxGenericHyperlinkCtrl::GetVisitedColour();
225 void wxHyperlinkCtrl::SetHoverColour(const wxColour
&colour
)
229 // simply do nothing: GTK+ does not allow us to change it :(
232 wxGenericHyperlinkCtrl::SetHoverColour(colour
);
235 wxColour
wxHyperlinkCtrl::GetHoverColour() const
239 // hover colour == normal colour for native GTK+ widget
240 return GetNormalColour();
243 return wxGenericHyperlinkCtrl::GetHoverColour();
246 GdkWindow
*wxHyperlinkCtrl::GTKGetWindow(wxArrayGdkWindows
& windows
) const
248 return UseNative() ? gtk_button_get_event_window(GTK_BUTTON(m_widget
))
249 : wxGenericHyperlinkCtrl::GTKGetWindow(windows
);
252 #endif // wxUSE_HYPERLINKCTRL && GTK+ 2.10+