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