]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobut.cpp
Initial implementation of wxWebProtocolHandler and wxWebFileProtocolHandler for the...
[wxWidgets.git] / src / gtk / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/radiobut.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_RADIOBTN
14
15 #include "wx/radiobut.h"
16
17 #include "wx/gtk/private.h"
18
19 //-----------------------------------------------------------------------------
20 // data
21 //-----------------------------------------------------------------------------
22
23 extern bool g_blockEventsOnDrag;
24
25 //-----------------------------------------------------------------------------
26 // "clicked"
27 //-----------------------------------------------------------------------------
28
29 extern "C" {
30 static
31 void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
32 {
33 if (!rb->m_hasVMT) return;
34
35 if (g_blockEventsOnDrag) return;
36
37 if (!gtk_toggle_button_get_active(button)) return;
38
39 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
40 event.SetInt( rb->GetValue() );
41 event.SetEventObject( rb );
42 rb->HandleWindowEvent( event );
43 }
44 }
45
46 //-----------------------------------------------------------------------------
47 // wxRadioButton
48 //-----------------------------------------------------------------------------
49
50 bool wxRadioButton::Create( wxWindow *parent,
51 wxWindowID id,
52 const wxString& label,
53 const wxPoint& pos,
54 const wxSize& size,
55 long style,
56 const wxValidator& validator,
57 const wxString& name )
58 {
59 if (!PreCreation( parent, pos, size ) ||
60 !CreateBase( parent, id, pos, size, style, validator, name ))
61 {
62 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
63 return false;
64 }
65
66 GSList* radioButtonGroup = NULL;
67 if (!HasFlag(wxRB_GROUP) && !HasFlag(wxRB_SINGLE))
68 {
69 // search backward for last group start
70 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
71 for (; node; node = node->GetPrevious())
72 {
73 wxWindow *child = node->GetData();
74 if (child->HasFlag(wxRB_GROUP) && wxIsKindOf(child, wxRadioButton))
75 {
76 radioButtonGroup = gtk_radio_button_get_group(
77 GTK_RADIO_BUTTON(child->m_widget));
78 break;
79 }
80 }
81 }
82
83 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
84 g_object_ref(m_widget);
85
86 SetLabel(label);
87
88 g_signal_connect_after (m_widget, "clicked",
89 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
90
91 m_parent->DoAddChild( this );
92
93 PostCreation(size);
94
95 return true;
96 }
97
98 void wxRadioButton::SetLabel( const wxString& label )
99 {
100 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
101
102 // save the original label
103 wxControlBase::SetLabel(label);
104
105 GTKSetLabelForLabel(GTK_LABEL(gtk_bin_get_child(GTK_BIN(m_widget))), label);
106 }
107
108 void wxRadioButton::SetValue( bool val )
109 {
110 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
111
112 if (val == GetValue())
113 return;
114
115 g_signal_handlers_block_by_func(
116 m_widget, (void*)gtk_radiobutton_clicked_callback, this);
117
118 if (val)
119 {
120 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
121 }
122 else
123 {
124 // should give an assert
125 // RL - No it shouldn't. A wxGenericValidator might try to set it
126 // as FALSE. Failing silently is probably TRTTD here.
127 }
128
129 g_signal_handlers_unblock_by_func(
130 m_widget, (void*)gtk_radiobutton_clicked_callback, this);
131 }
132
133 bool wxRadioButton::GetValue() const
134 {
135 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobutton") );
136
137 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget)) != 0;
138 }
139
140 bool wxRadioButton::Enable( bool enable )
141 {
142 if (!base_type::Enable(enable))
143 return false;
144
145 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget)), enable);
146
147 if (enable)
148 GTKFixSensitivity();
149
150 return true;
151 }
152
153 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
154 {
155 gtk_widget_modify_style(m_widget, style);
156 gtk_widget_modify_style(gtk_bin_get_child(GTK_BIN(m_widget)), style);
157 }
158
159 GdkWindow *
160 wxRadioButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
161 {
162 return GTK_BUTTON(m_widget)->event_window;
163 }
164
165 // static
166 wxVisualAttributes
167 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
168 {
169 wxVisualAttributes attr;
170 // NB: we need toplevel window so that GTK+ can find the right style
171 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
172 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
173 gtk_container_add(GTK_CONTAINER(wnd), widget);
174 attr = GetDefaultAttributesFromGTKWidget(widget);
175 gtk_widget_destroy(wnd);
176 return attr;
177 }
178
179
180 #endif