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