| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/gtk/hyperlink.cpp |
| 3 | // Purpose: Hyperlink control |
| 4 | // Author: Francesco Montorsi |
| 5 | // Created: 14/2/2007 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2007 Francesco Montorsi |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | // ============================================================================ |
| 12 | // declarations |
| 13 | // ============================================================================ |
| 14 | |
| 15 | // -------------------------------------------------------------------------- |
| 16 | // headers |
| 17 | // -------------------------------------------------------------------------- |
| 18 | |
| 19 | // For compilers that support precompilation, includes "wx.h". |
| 20 | #include "wx/wxprec.h" |
| 21 | |
| 22 | #ifdef __BORLANDC__ |
| 23 | #pragma hdrstop |
| 24 | #endif |
| 25 | |
| 26 | #if wxUSE_HYPERLINKCTRL && defined(__WXGTK210__) |
| 27 | |
| 28 | #include "wx/hyperlink.h" |
| 29 | |
| 30 | #ifndef WX_PRECOMP |
| 31 | #endif |
| 32 | |
| 33 | #include <gtk/gtk.h> |
| 34 | #include "wx/gtk/private.h" |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | // local functions |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
| 40 | static inline bool UseNative() |
| 41 | { |
| 42 | // native gtk_link_button widget is only available in GTK+ 2.10 and later |
| 43 | return !gtk_check_version(2, 10, 0); |
| 44 | } |
| 45 | |
| 46 | // ============================================================================ |
| 47 | // implementation |
| 48 | // ============================================================================ |
| 49 | |
| 50 | IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkCtrl, wxGenericHyperlinkCtrl) |
| 51 | |
| 52 | |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | // "clicked" |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | |
| 57 | extern "C" { |
| 58 | static void gtk_hyperlink_clicked_callback( GtkWidget *WXUNUSED(widget), |
| 59 | wxHyperlinkCtrl *linkCtrl ) |
| 60 | { |
| 61 | // send the event |
| 62 | linkCtrl->SendEvent(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // ---------------------------------------------------------------------------- |
| 67 | // wxHyperlinkCtrl |
| 68 | // ---------------------------------------------------------------------------- |
| 69 | |
| 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) |
| 73 | { |
| 74 | if ( UseNative() ) |
| 75 | { |
| 76 | // do validation checks: |
| 77 | CheckParams(label, url, style); |
| 78 | |
| 79 | if (!PreCreation( parent, pos, size ) || |
| 80 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) |
| 81 | { |
| 82 | wxFAIL_MSG( wxT("wxHyperlinkCtrl creation failed") ); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | m_widget = gtk_link_button_new("asdfsaf asdfdsaf asdfdsa"); |
| 87 | gtk_widget_show(m_widget); |
| 88 | |
| 89 | // alignment |
| 90 | float x_alignment = 0.5; |
| 91 | if (HasFlag(wxHL_ALIGN_LEFT)) |
| 92 | x_alignment = 0.0; |
| 93 | else if (HasFlag(wxHL_ALIGN_RIGHT)) |
| 94 | x_alignment = 1.0; |
| 95 | gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, 0.5); |
| 96 | |
| 97 | // set to non empty strings both the url and the label |
| 98 | SetURL(url.empty() ? label : url); |
| 99 | SetLabel(label.empty() ? url : label); |
| 100 | |
| 101 | // our signal handlers: |
| 102 | g_signal_connect_after (m_widget, "clicked", |
| 103 | G_CALLBACK (gtk_hyperlink_clicked_callback), |
| 104 | this); |
| 105 | |
| 106 | m_parent->DoAddChild( this ); |
| 107 | |
| 108 | PostCreation(size); |
| 109 | SetInitialSize(size); |
| 110 | |
| 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)); |
| 115 | } |
| 116 | else |
| 117 | return wxGenericHyperlinkCtrl::Create(parent, id, label, url, pos, size, style, name); |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | wxSize wxHyperlinkCtrl::DoGetBestSize() const |
| 123 | { |
| 124 | if ( UseNative() ) |
| 125 | return wxControl::DoGetBestSize(); |
| 126 | return wxGenericHyperlinkCtrl::DoGetBestSize(); |
| 127 | } |
| 128 | |
| 129 | void wxHyperlinkCtrl::SetLabel(const wxString &label) |
| 130 | { |
| 131 | if ( UseNative() ) |
| 132 | { |
| 133 | wxControl::SetLabel(label); |
| 134 | const wxString labelGTK = GTKConvertMnemonics(label); |
| 135 | gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK)); |
| 136 | } |
| 137 | else |
| 138 | wxGenericHyperlinkCtrl::SetLabel(label); |
| 139 | } |
| 140 | |
| 141 | void wxHyperlinkCtrl::SetURL(const wxString &uri) |
| 142 | { |
| 143 | if ( UseNative() ) |
| 144 | gtk_link_button_set_uri(GTK_LINK_BUTTON(m_widget), wxGTK_CONV(uri)); |
| 145 | else |
| 146 | wxGenericHyperlinkCtrl::SetURL(uri); |
| 147 | } |
| 148 | |
| 149 | wxString wxHyperlinkCtrl::GetURL() const |
| 150 | { |
| 151 | if ( UseNative() ) |
| 152 | { |
| 153 | const gchar *str = gtk_link_button_get_uri(GTK_LINK_BUTTON(m_widget)); |
| 154 | return wxString(str, *wxConvFileName); |
| 155 | } |
| 156 | |
| 157 | return wxGenericHyperlinkCtrl::GetURL(); |
| 158 | } |
| 159 | |
| 160 | void wxHyperlinkCtrl::SetNormalColour(const wxColour &colour) |
| 161 | { |
| 162 | if ( UseNative() ) |
| 163 | { |
| 164 | // simply do nothing: GTK+ does not allow us to change it :( |
| 165 | } |
| 166 | else |
| 167 | wxGenericHyperlinkCtrl::SetNormalColour(colour); |
| 168 | } |
| 169 | |
| 170 | wxColour wxHyperlinkCtrl::GetNormalColour() const |
| 171 | { |
| 172 | wxColour ret; |
| 173 | if ( UseNative() ) |
| 174 | { |
| 175 | GdkColor *link_color = NULL; |
| 176 | |
| 177 | // convert GdkColor in wxColour |
| 178 | gtk_widget_style_get(m_widget, "link-color", &link_color, NULL); |
| 179 | if (link_color) |
| 180 | ret = wxColour(*link_color); |
| 181 | gdk_color_free (link_color); |
| 182 | } |
| 183 | else |
| 184 | ret = wxGenericHyperlinkCtrl::GetNormalColour(); |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | void wxHyperlinkCtrl::SetVisitedColour(const wxColour &colour) |
| 190 | { |
| 191 | if ( UseNative() ) |
| 192 | { |
| 193 | // simply do nothing: GTK+ does not allow us to change it :( |
| 194 | } |
| 195 | else |
| 196 | wxGenericHyperlinkCtrl::SetVisitedColour(colour); |
| 197 | } |
| 198 | |
| 199 | wxColour wxHyperlinkCtrl::GetVisitedColour() const |
| 200 | { |
| 201 | wxColour ret; |
| 202 | if ( UseNative() ) |
| 203 | { |
| 204 | GdkColor *link_color = NULL; |
| 205 | |
| 206 | // convert GdkColor in wxColour |
| 207 | gtk_widget_style_get(m_widget, "visited-link-color", &link_color, NULL); |
| 208 | if (link_color) |
| 209 | ret = wxColour(*link_color); |
| 210 | gdk_color_free (link_color); |
| 211 | } |
| 212 | else |
| 213 | return wxGenericHyperlinkCtrl::GetVisitedColour(); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | void wxHyperlinkCtrl::SetHoverColour(const wxColour &colour) |
| 219 | { |
| 220 | if ( UseNative() ) |
| 221 | { |
| 222 | // simply do nothing: GTK+ does not allow us to change it :( |
| 223 | } |
| 224 | else |
| 225 | wxGenericHyperlinkCtrl::SetHoverColour(colour); |
| 226 | } |
| 227 | |
| 228 | wxColour wxHyperlinkCtrl::GetHoverColour() const |
| 229 | { |
| 230 | if ( UseNative() ) |
| 231 | { |
| 232 | // hover colour == normal colour for native GTK+ widget |
| 233 | return GetNormalColour(); |
| 234 | } |
| 235 | |
| 236 | return wxGenericHyperlinkCtrl::GetHoverColour(); |
| 237 | } |
| 238 | |
| 239 | GdkWindow *wxHyperlinkCtrl::GTKGetWindow(wxArrayGdkWindows& windows) const |
| 240 | { |
| 241 | return UseNative() ? GTK_BUTTON(m_widget)->event_window |
| 242 | : wxGenericHyperlinkCtrl::GTKGetWindow(windows); |
| 243 | } |
| 244 | |
| 245 | #endif // wxUSE_HYPERLINKCTRL && GTK+ 2.10+ |