remove unnecessary gtk_widget_show(m_widget) calls, PostCreation() takes care of...
[wxWidgets.git] / src / gtk / hyperlink.cpp
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__) && !defined(__WXUNIVERSAL__)
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 // ----------------------------------------------------------------------------
51 // "clicked"
52 // ----------------------------------------------------------------------------
53
54 extern "C" {
55 static void gtk_hyperlink_clicked_callback( GtkWidget *WXUNUSED(widget),
56 wxHyperlinkCtrl *linkCtrl )
57 {
58 // send the event
59 linkCtrl->SendEvent();
60 }
61 }
62
63 // ----------------------------------------------------------------------------
64 // wxHyperlinkCtrl
65 // ----------------------------------------------------------------------------
66
67 bool wxHyperlinkCtrl::Create(wxWindow *parent, wxWindowID id,
68 const wxString& label, const wxString& url, const wxPoint& pos,
69 const wxSize& size, long style, const wxString& name)
70 {
71 if ( UseNative() )
72 {
73 // do validation checks:
74 CheckParams(label, url, style);
75
76 if (!PreCreation( parent, pos, size ) ||
77 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
78 {
79 wxFAIL_MSG( wxT("wxHyperlinkCtrl creation failed") );
80 return false;
81 }
82
83 m_widget = gtk_link_button_new("asdfsaf asdfdsaf asdfdsa");
84 g_object_ref(m_widget);
85
86 // alignment
87 float x_alignment = 0.5;
88 if (HasFlag(wxHL_ALIGN_LEFT))
89 x_alignment = 0.0;
90 else if (HasFlag(wxHL_ALIGN_RIGHT))
91 x_alignment = 1.0;
92 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, 0.5);
93
94 // set to non empty strings both the url and the label
95 SetURL(url.empty() ? label : url);
96 SetLabel(label.empty() ? url : label);
97
98 // our signal handlers:
99 g_signal_connect_after (m_widget, "clicked",
100 G_CALLBACK (gtk_hyperlink_clicked_callback),
101 this);
102
103 m_parent->DoAddChild( this );
104
105 PostCreation(size);
106
107 // wxWindowGTK will connect to the enter_notify and leave_notify GTK+ signals
108 // thus overriding GTK+'s internal signal handlers which set the cursor of
109 // the widget - thus we need to manually set it here:
110 SetCursor(wxCursor(wxCURSOR_HAND));
111 }
112 else
113 return wxGenericHyperlinkCtrl::Create(parent, id, label, url, pos, size, style, name);
114
115 return true;
116 }
117
118 wxSize wxHyperlinkCtrl::DoGetBestSize() const
119 {
120 if ( UseNative() )
121 return wxControl::DoGetBestSize();
122 return wxGenericHyperlinkCtrl::DoGetBestSize();
123 }
124
125 wxSize wxHyperlinkCtrl::DoGetBestClientSize() const
126 {
127 if ( UseNative() )
128 return wxControl::DoGetBestClientSize();
129 return wxGenericHyperlinkCtrl::DoGetBestClientSize();
130 }
131
132 void wxHyperlinkCtrl::SetLabel(const wxString &label)
133 {
134 if ( UseNative() )
135 {
136 wxControl::SetLabel(label);
137 const wxString labelGTK = GTKConvertMnemonics(label);
138 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
139 }
140 else
141 wxGenericHyperlinkCtrl::SetLabel(label);
142 }
143
144 void wxHyperlinkCtrl::SetURL(const wxString &uri)
145 {
146 if ( UseNative() )
147 gtk_link_button_set_uri(GTK_LINK_BUTTON(m_widget), wxGTK_CONV(uri));
148 else
149 wxGenericHyperlinkCtrl::SetURL(uri);
150 }
151
152 wxString wxHyperlinkCtrl::GetURL() const
153 {
154 if ( UseNative() )
155 {
156 const gchar *str = gtk_link_button_get_uri(GTK_LINK_BUTTON(m_widget));
157 return wxString::FromUTF8(str);
158 }
159
160 return wxGenericHyperlinkCtrl::GetURL();
161 }
162
163 void wxHyperlinkCtrl::SetNormalColour(const wxColour &colour)
164 {
165 if ( UseNative() )
166 {
167 // simply do nothing: GTK+ does not allow us to change it :(
168 }
169 else
170 wxGenericHyperlinkCtrl::SetNormalColour(colour);
171 }
172
173 wxColour wxHyperlinkCtrl::GetNormalColour() const
174 {
175 wxColour ret;
176 if ( UseNative() )
177 {
178 GdkColor *link_color = NULL;
179
180 // convert GdkColor in wxColour
181 gtk_widget_style_get(m_widget, "link-color", &link_color, NULL);
182 if (link_color)
183 ret = wxColour(*link_color);
184 gdk_color_free (link_color);
185 }
186 else
187 ret = wxGenericHyperlinkCtrl::GetNormalColour();
188
189 return ret;
190 }
191
192 void wxHyperlinkCtrl::SetVisitedColour(const wxColour &colour)
193 {
194 if ( UseNative() )
195 {
196 // simply do nothing: GTK+ does not allow us to change it :(
197 }
198 else
199 wxGenericHyperlinkCtrl::SetVisitedColour(colour);
200 }
201
202 wxColour wxHyperlinkCtrl::GetVisitedColour() const
203 {
204 wxColour ret;
205 if ( UseNative() )
206 {
207 GdkColor *link_color = NULL;
208
209 // convert GdkColor in wxColour
210 gtk_widget_style_get(m_widget, "visited-link-color", &link_color, NULL);
211 if (link_color)
212 ret = wxColour(*link_color);
213 gdk_color_free (link_color);
214 }
215 else
216 return wxGenericHyperlinkCtrl::GetVisitedColour();
217
218 return ret;
219 }
220
221 void wxHyperlinkCtrl::SetHoverColour(const wxColour &colour)
222 {
223 if ( UseNative() )
224 {
225 // simply do nothing: GTK+ does not allow us to change it :(
226 }
227 else
228 wxGenericHyperlinkCtrl::SetHoverColour(colour);
229 }
230
231 wxColour wxHyperlinkCtrl::GetHoverColour() const
232 {
233 if ( UseNative() )
234 {
235 // hover colour == normal colour for native GTK+ widget
236 return GetNormalColour();
237 }
238
239 return wxGenericHyperlinkCtrl::GetHoverColour();
240 }
241
242 GdkWindow *wxHyperlinkCtrl::GTKGetWindow(wxArrayGdkWindows& windows) const
243 {
244 return UseNative() ? GTK_BUTTON(m_widget)->event_window
245 : wxGenericHyperlinkCtrl::GTKGetWindow(windows);
246 }
247
248 #endif // wxUSE_HYPERLINKCTRL && GTK+ 2.10+