]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/hyperlink.cpp
VC6 compilation fix (don't reuse the same variable in 2 for loops)
[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__)
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 m_needParent = true;
80 m_acceptsFocus = true;
81
82 if (!PreCreation( parent, pos, size ) ||
83 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
84 {
85 wxFAIL_MSG( wxT("wxHyperlinkCtrl creation failed") );
86 return false;
87 }
88
89 m_widget = gtk_link_button_new("asdfsaf asdfdsaf asdfdsa");
90 gtk_widget_show( GTK_WIDGET(m_widget) );
91
92 // alignment
93 float x_alignment = 0.5;
94 if (HasFlag(wxHL_ALIGN_LEFT))
95 x_alignment = 0.0;
96 else if (HasFlag(wxHL_ALIGN_RIGHT))
97 x_alignment = 1.0;
98 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, 0.5);
99
100 // set to non empty strings both the url and the label
101 SetURL(url.empty() ? label : url);
102 SetLabel(label.empty() ? url : label);
103
104 // our signal handlers:
105 g_signal_connect_after (m_widget, "clicked",
106 G_CALLBACK (gtk_hyperlink_clicked_callback),
107 this);
108
109 m_parent->DoAddChild( this );
110
111 PostCreation(size);
112 SetInitialSize(size);
113
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));
118 }
119 else
120 return wxGenericHyperlinkCtrl::Create(parent, id, label, url, pos, size, style, name);
121
122 return true;
123 }
124
125 wxSize wxHyperlinkCtrl::DoGetBestSize() const
126 {
127 if ( UseNative() )
128 return wxControl::DoGetBestSize();
129 return wxGenericHyperlinkCtrl::DoGetBestSize();
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 wxConvFileName->cMB2WX(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+