1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/hyperlink.cpp
3 // Purpose: Hyperlink control
4 // Author: Francesco Montorsi
6 // Copyright: (c) 2007 Francesco Montorsi
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // --------------------------------------------------------------------------
16 // --------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
25 #if wxUSE_HYPERLINKCTRL && defined(__WXGTK210__) && !defined(__WXUNIVERSAL__)
27 #include "wx/hyperlink.h"
33 #include "wx/gtk/private.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 static inline bool UseNative()
41 // native gtk_link_button widget is only available in GTK+ 2.10 and later
45 return !gtk_check_version(2, 10, 0);
49 // ============================================================================
51 // ============================================================================
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
);
90 float x_alignment
= 0.5;
91 if (HasFlag(wxHL_ALIGN_LEFT
))
93 else if (HasFlag(wxHL_ALIGN_RIGHT
))
95 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, 0.5);
97 // set to non empty strings both the url and the label
98 SetURL(url
.empty() ? label
: url
);
99 SetLabel(label
.empty() ? url
: label
);
101 // our signal handlers:
102 g_signal_connect_after (m_widget
, "clicked",
103 G_CALLBACK (gtk_hyperlink_clicked_callback
),
106 m_parent
->DoAddChild( this );
110 // wxWindowGTK will connect to the enter_notify and leave_notify GTK+ signals
111 // thus overriding GTK+'s internal signal handlers which set the cursor of
112 // the widget - thus we need to manually set it here:
113 SetCursor(wxCursor(wxCURSOR_HAND
));
116 return wxGenericHyperlinkCtrl::Create(parent
, id
, label
, url
, pos
, size
, style
, name
);
121 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
124 return wxControl::DoGetBestSize();
125 return wxGenericHyperlinkCtrl::DoGetBestSize();
128 wxSize
wxHyperlinkCtrl::DoGetBestClientSize() const
131 return wxControl::DoGetBestClientSize();
132 return wxGenericHyperlinkCtrl::DoGetBestClientSize();
135 void wxHyperlinkCtrl::SetLabel(const wxString
&label
)
139 wxControl::SetLabel(label
);
140 const wxString labelGTK
= GTKConvertMnemonics(label
);
141 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
144 wxGenericHyperlinkCtrl::SetLabel(label
);
147 void wxHyperlinkCtrl::SetURL(const wxString
&uri
)
150 gtk_link_button_set_uri(GTK_LINK_BUTTON(m_widget
), wxGTK_CONV(uri
));
152 wxGenericHyperlinkCtrl::SetURL(uri
);
155 wxString
wxHyperlinkCtrl::GetURL() const
159 const gchar
*str
= gtk_link_button_get_uri(GTK_LINK_BUTTON(m_widget
));
160 return wxString::FromUTF8(str
);
163 return wxGenericHyperlinkCtrl::GetURL();
166 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
170 // simply do nothing: GTK+ does not allow us to change it :(
173 wxGenericHyperlinkCtrl::SetNormalColour(colour
);
176 wxColour
wxHyperlinkCtrl::GetNormalColour() const
181 GdkColor
*link_color
= NULL
;
183 // convert GdkColor in wxColour
184 gtk_widget_style_get(m_widget
, "link-color", &link_color
, NULL
);
186 ret
= wxColour(*link_color
);
187 gdk_color_free (link_color
);
190 ret
= wxGenericHyperlinkCtrl::GetNormalColour();
195 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
199 // simply do nothing: GTK+ does not allow us to change it :(
202 wxGenericHyperlinkCtrl::SetVisitedColour(colour
);
205 wxColour
wxHyperlinkCtrl::GetVisitedColour() const
210 GdkColor
*link_color
= NULL
;
212 // convert GdkColor in wxColour
213 gtk_widget_style_get(m_widget
, "visited-link-color", &link_color
, NULL
);
215 ret
= wxColour(*link_color
);
216 gdk_color_free (link_color
);
219 return wxGenericHyperlinkCtrl::GetVisitedColour();
224 void wxHyperlinkCtrl::SetHoverColour(const wxColour
&colour
)
228 // simply do nothing: GTK+ does not allow us to change it :(
231 wxGenericHyperlinkCtrl::SetHoverColour(colour
);
234 wxColour
wxHyperlinkCtrl::GetHoverColour() const
238 // hover colour == normal colour for native GTK+ widget
239 return GetNormalColour();
242 return wxGenericHyperlinkCtrl::GetHoverColour();
245 GdkWindow
*wxHyperlinkCtrl::GTKGetWindow(wxArrayGdkWindows
& windows
) const
247 return UseNative() ? gtk_button_get_event_window(GTK_BUTTON(m_widget
))
248 : wxGenericHyperlinkCtrl::GTKGetWindow(windows
);
251 #endif // wxUSE_HYPERLINKCTRL && GTK+ 2.10+