Reorganize idle system code.
[wxWidgets.git] / src / gtk / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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_RADIOBOX
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 (!button->active) return;
38
39 if (rb->m_blockEvent) return;
40
41 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
42 event.SetInt( rb->GetValue() );
43 event.SetEventObject( rb );
44 rb->GetEventHandler()->ProcessEvent( event );
45 }
46 }
47
48 //-----------------------------------------------------------------------------
49 // wxRadioButton
50 //-----------------------------------------------------------------------------
51
52 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
53
54 bool wxRadioButton::Create( wxWindow *parent,
55 wxWindowID id,
56 const wxString& label,
57 const wxPoint& pos,
58 const wxSize& size,
59 long style,
60 const wxValidator& validator,
61 const wxString& name )
62 {
63 m_needParent = TRUE;
64
65 m_blockEvent = FALSE;
66
67 if (!PreCreation( parent, pos, size ) ||
68 !CreateBase( parent, id, pos, size, style, validator, name ))
69 {
70 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
71 return FALSE;
72 }
73
74 GSList* radioButtonGroup = NULL;
75 if (!HasFlag(wxRB_GROUP))
76 {
77 // search backward for last group start
78 wxRadioButton *chief = (wxRadioButton*) NULL;
79 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
80 while (node)
81 {
82 wxWindow *child = node->GetData();
83 if (child->IsRadioButton())
84 {
85 chief = (wxRadioButton*) child;
86 if (child->HasFlag(wxRB_GROUP))
87 break;
88 }
89 node = node->GetPrevious();
90 }
91 if (chief)
92 {
93 // we are part of the group started by chief
94 radioButtonGroup = gtk_radio_button_get_group( GTK_RADIO_BUTTON(chief->m_widget) );
95 }
96 }
97
98 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
99
100 SetLabel(label);
101
102 g_signal_connect (m_widget, "clicked",
103 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
104
105 m_parent->DoAddChild( this );
106
107 PostCreation(size);
108
109 return TRUE;
110 }
111
112 void wxRadioButton::SetLabel( const wxString& label )
113 {
114 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
115
116 GTKSetLabelForLabel(GTK_LABEL(GTK_BIN(m_widget)->child), label);
117 }
118
119 void wxRadioButton::SetValue( bool val )
120 {
121 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
122
123 if (val == GetValue())
124 return;
125
126 m_blockEvent = TRUE;
127
128 if (val)
129 {
130 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
131 }
132 else
133 {
134 // should give an assert
135 // RL - No it shouldn't. A wxGenericValidator might try to set it
136 // as FALSE. Failing silently is probably TRTTD here.
137 }
138
139 m_blockEvent = FALSE;
140 }
141
142 bool wxRadioButton::GetValue() const
143 {
144 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
145
146 return GTK_TOGGLE_BUTTON(m_widget)->active;
147 }
148
149 bool wxRadioButton::Enable( bool enable )
150 {
151 if ( !wxControl::Enable( enable ) )
152 return FALSE;
153
154 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
155
156 return TRUE;
157 }
158
159 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
160 {
161 gtk_widget_modify_style(m_widget, style);
162 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
163 }
164
165 GdkWindow *
166 wxRadioButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
167 {
168 return GTK_BUTTON(m_widget)->event_window;
169 }
170
171 wxSize wxRadioButton::DoGetBestSize() const
172 {
173 return wxControl::DoGetBestSize();
174 }
175
176 // static
177 wxVisualAttributes
178 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
179 {
180 wxVisualAttributes attr;
181 // NB: we need toplevel window so that GTK+ can find the right style
182 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
183 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
184 gtk_container_add(GTK_CONTAINER(wnd), widget);
185 attr = GetDefaultAttributesFromGTKWidget(widget);
186 gtk_widget_destroy(wnd);
187 return attr;
188 }
189
190
191 #endif