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__)
28 #include "wx/hyperlink.h"
34 #include "wx/gtk/private.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 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
);
80 m_acceptsFocus
= true;
82 if (!PreCreation( parent
, pos
, size
) ||
83 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
85 wxFAIL_MSG( wxT("wxHyperlinkCtrl creation failed") );
89 m_widget
= gtk_link_button_new("asdfsaf asdfdsaf asdfdsa");
90 gtk_widget_show( GTK_WIDGET(m_widget
) );
93 float x_alignment
= 0.5;
94 if (HasFlag(wxHL_ALIGN_LEFT
))
96 else if (HasFlag(wxHL_ALIGN_RIGHT
))
98 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, 0.5);
100 // set to non empty strings both the url and the label
101 SetURL(url
.empty() ? label
: url
);
102 SetLabel(label
.empty() ? url
: label
);
104 // our signal handlers:
105 g_signal_connect_after (m_widget
, "clicked",
106 G_CALLBACK (gtk_hyperlink_clicked_callback
),
109 m_parent
->DoAddChild( this );
112 SetInitialSize(size
);
114 // wxWindowGTK will connect to the enter_notify and leave_notify GTK+ signals
115 // thus overriding GTK+'s internal signal handlers which set the cursor of
116 // the widget - thus we need to manually set it here:
117 SetCursor(wxCursor(wxCURSOR_HAND
));
120 return wxGenericHyperlinkCtrl::Create(parent
, id
, label
, url
, pos
, size
, style
, name
);
125 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
128 return wxControl::DoGetBestSize();
129 return wxGenericHyperlinkCtrl::DoGetBestSize();
132 void wxHyperlinkCtrl::SetLabel(const wxString
&label
)
136 wxControl::SetLabel(label
);
137 const wxString labelGTK
= GTKConvertMnemonics(label
);
138 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
141 wxGenericHyperlinkCtrl::SetLabel(label
);
144 void wxHyperlinkCtrl::SetURL(const wxString
&uri
)
147 gtk_link_button_set_uri(GTK_LINK_BUTTON(m_widget
), uri
.c_str());
149 wxGenericHyperlinkCtrl::SetURL(uri
);
152 wxString
wxHyperlinkCtrl::GetURL() const
156 const gchar
*str
= gtk_link_button_get_uri(GTK_LINK_BUTTON(m_widget
));
157 return wxConvFileName
->cMB2WX(str
);
160 return wxGenericHyperlinkCtrl::GetURL();
163 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
167 // simply do nothing: GTK+ does not allow us to change it :(
170 wxGenericHyperlinkCtrl::SetNormalColour(colour
);
173 wxColour
wxHyperlinkCtrl::GetNormalColour() const
177 GdkColor
*link_color
= NULL
;
178 wxColour ret
= wxNullColour
;
180 // convert GdkColor in wxColour
181 gtk_widget_style_get(m_widget
, "link-color", &link_color
, NULL
);
183 ret
.Set(link_color
->red
, link_color
->green
, link_color
->blue
);
184 gdk_color_free (link_color
);
189 return wxGenericHyperlinkCtrl::GetNormalColour();
192 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
196 // simply do nothing: GTK+ does not allow us to change it :(
199 wxGenericHyperlinkCtrl::SetVisitedColour(colour
);
202 wxColour
wxHyperlinkCtrl::GetVisitedColour() const
206 GdkColor
*link_color
= NULL
;
207 wxColour ret
= wxNullColour
;
209 // convert GdkColor in wxColour
210 gtk_widget_style_get(m_widget
, "visited-link-color", &link_color
, NULL
);
212 ret
.Set(link_color
->red
, link_color
->green
, link_color
->blue
);
213 gdk_color_free (link_color
);
218 return wxGenericHyperlinkCtrl::GetVisitedColour();
221 void wxHyperlinkCtrl::SetHoverColour(const wxColour
&colour
)
225 // simply do nothing: GTK+ does not allow us to change it :(
228 wxGenericHyperlinkCtrl::SetHoverColour(colour
);
231 wxColour
wxHyperlinkCtrl::GetHoverColour() const
235 // hover colour == normal colour for native GTK+ widget
236 return GetNormalColour();
239 return wxGenericHyperlinkCtrl::GetHoverColour();
242 GdkWindow
*wxHyperlinkCtrl::GTKGetWindow(wxArrayGdkWindows
& windows
) const
244 return UseNative() ? GTK_BUTTON(m_widget
)->event_window
245 : wxGenericHyperlinkCtrl::GTKGetWindow(windows
);
248 #endif // wxUSE_HYPERLINKCTRL && GTK+ 2.10+