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 IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkCtrl
, wxGenericHyperlinkCtrl
)
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
58 static void gtk_hyperlink_clicked_callback( GtkWidget
*WXUNUSED(widget
),
59 wxHyperlinkCtrl
*linkCtrl
)
62 linkCtrl
->SendEvent();
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 bool wxHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
71 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
72 const wxSize
& size
, long style
, const wxString
& name
)
76 // do validation checks:
77 CheckParams(label
, url
, style
);
79 if (!PreCreation( parent
, pos
, size
) ||
80 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
82 wxFAIL_MSG( wxT("wxHyperlinkCtrl creation failed") );
86 m_widget
= gtk_link_button_new("asdfsaf asdfdsaf asdfdsa");
87 g_object_ref(m_widget
);
88 gtk_widget_show(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 );
110 SetInitialSize(size
);
112 // wxWindowGTK will connect to the enter_notify and leave_notify GTK+ signals
113 // thus overriding GTK+'s internal signal handlers which set the cursor of
114 // the widget - thus we need to manually set it here:
115 SetCursor(wxCursor(wxCURSOR_HAND
));
118 return wxGenericHyperlinkCtrl::Create(parent
, id
, label
, url
, pos
, size
, style
, name
);
123 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
126 return wxControl::DoGetBestSize();
127 return wxGenericHyperlinkCtrl::DoGetBestSize();
130 void wxHyperlinkCtrl::SetLabel(const wxString
&label
)
134 wxControl::SetLabel(label
);
135 const wxString labelGTK
= GTKConvertMnemonics(label
);
136 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
139 wxGenericHyperlinkCtrl::SetLabel(label
);
142 void wxHyperlinkCtrl::SetURL(const wxString
&uri
)
145 gtk_link_button_set_uri(GTK_LINK_BUTTON(m_widget
), wxGTK_CONV(uri
));
147 wxGenericHyperlinkCtrl::SetURL(uri
);
150 wxString
wxHyperlinkCtrl::GetURL() const
154 const gchar
*str
= gtk_link_button_get_uri(GTK_LINK_BUTTON(m_widget
));
155 return wxString(str
, *wxConvFileName
);
158 return wxGenericHyperlinkCtrl::GetURL();
161 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
165 // simply do nothing: GTK+ does not allow us to change it :(
168 wxGenericHyperlinkCtrl::SetNormalColour(colour
);
171 wxColour
wxHyperlinkCtrl::GetNormalColour() const
176 GdkColor
*link_color
= NULL
;
178 // convert GdkColor in wxColour
179 gtk_widget_style_get(m_widget
, "link-color", &link_color
, NULL
);
181 ret
= wxColour(*link_color
);
182 gdk_color_free (link_color
);
185 ret
= wxGenericHyperlinkCtrl::GetNormalColour();
190 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
194 // simply do nothing: GTK+ does not allow us to change it :(
197 wxGenericHyperlinkCtrl::SetVisitedColour(colour
);
200 wxColour
wxHyperlinkCtrl::GetVisitedColour() const
205 GdkColor
*link_color
= NULL
;
207 // convert GdkColor in wxColour
208 gtk_widget_style_get(m_widget
, "visited-link-color", &link_color
, NULL
);
210 ret
= wxColour(*link_color
);
211 gdk_color_free (link_color
);
214 return wxGenericHyperlinkCtrl::GetVisitedColour();
219 void wxHyperlinkCtrl::SetHoverColour(const wxColour
&colour
)
223 // simply do nothing: GTK+ does not allow us to change it :(
226 wxGenericHyperlinkCtrl::SetHoverColour(colour
);
229 wxColour
wxHyperlinkCtrl::GetHoverColour() const
233 // hover colour == normal colour for native GTK+ widget
234 return GetNormalColour();
237 return wxGenericHyperlinkCtrl::GetHoverColour();
240 GdkWindow
*wxHyperlinkCtrl::GTKGetWindow(wxArrayGdkWindows
& windows
) const
242 return UseNative() ? GTK_BUTTON(m_widget
)->event_window
243 : wxGenericHyperlinkCtrl::GTKGetWindow(windows
);
246 #endif // wxUSE_HYPERLINKCTRL && GTK+ 2.10+