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