replaced T() makro with wxT() due to namespace probs, _T() exists, too
[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
11 #ifdef __GNUG__
12 #pragma implementation "radiobut.h"
13 #endif
14
15 #include "wx/radiobut.h"
16
17 #if wxUSE_RADIOBOX
18
19 #include "gdk/gdk.h"
20 #include "gtk/gtk.h"
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34
35 //-----------------------------------------------------------------------------
36 // "clicked"
37 //-----------------------------------------------------------------------------
38
39 static
40 void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioButton *rb )
41 {
42 if (g_isIdle) wxapp_install_idle_handler();
43
44 if (!rb->m_hasVMT) return;
45
46 if (g_blockEventsOnDrag) return;
47
48 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
49 event.SetInt( rb->GetValue() );
50 event.SetEventObject( rb );
51 rb->GetEventHandler()->ProcessEvent( event );
52 }
53
54 //-----------------------------------------------------------------------------
55 // wxRadioButton
56 //-----------------------------------------------------------------------------
57
58 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
59
60 bool wxRadioButton::Create( wxWindow *parent, wxWindowID id, const wxString& label,
61 const wxPoint& pos, const wxSize& size, long style,
62 const wxValidator& validator, const wxString& name )
63 {
64 m_acceptsFocus = TRUE;
65 m_needParent = TRUE;
66 m_isRadioButton = TRUE;
67
68 if (!PreCreation( parent, pos, size ) ||
69 !CreateBase( parent, id, pos, size, style, validator, name ))
70 {
71 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
72 return FALSE;
73 }
74
75 if (HasFlag(wxRB_GROUP))
76 {
77 /* start a new group */
78 m_radioButtonGroup = (GSList*) NULL;
79 }
80 else
81 {
82 /* search backward for last group start */
83 wxRadioButton *chief = (wxRadioButton*) NULL;
84 wxWindowList::Node *node = parent->GetChildren().GetLast();
85 while (node)
86 {
87 wxWindow *child = node->GetData();
88 if (child->m_isRadioButton)
89 {
90 chief = (wxRadioButton*) child;
91 if (child->HasFlag(wxRB_GROUP)) break;
92 }
93 node = node->GetPrevious();
94 }
95 if (chief)
96 {
97 /* we are part of the group started by chief */
98 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
99 }
100 else
101 {
102 /* start a new group */
103 m_radioButtonGroup = (GSList*) NULL;
104 }
105 }
106
107 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, label.mbc_str() );
108
109 SetLabel(label);
110
111 wxSize newSize = size;
112 if (newSize.x == -1) newSize.x = 22+gdk_string_measure( m_widget->style->font, label.mbc_str() );
113 if (newSize.y == -1) newSize.y = 26;
114 SetSize( newSize.x, newSize.y );
115
116 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
117 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
118
119 m_parent->DoAddChild( this );
120
121 PostCreation();
122
123 SetBackgroundColour( parent->GetBackgroundColour() );
124 SetForegroundColour( parent->GetForegroundColour() );
125 SetFont( parent->GetFont() );
126
127 Show( TRUE );
128
129 return TRUE;
130 }
131
132 void wxRadioButton::SetLabel( const wxString& label )
133 {
134 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
135
136 wxControl::SetLabel( label );
137 GtkButton *bin = GTK_BUTTON( m_widget );
138 GtkLabel *g_label = GTK_LABEL( bin->child );
139 gtk_label_set( g_label, GetLabel().mbc_str() );
140 }
141
142 void wxRadioButton::SetValue( bool val )
143 {
144 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
145
146 if (val == GetValue())
147 return;
148
149 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
150 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
151
152 if (val)
153 {
154 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget), TRUE );
155 }
156 else
157 {
158 // should give an assert
159 }
160
161 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
162 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
163 }
164
165 bool wxRadioButton::GetValue() const
166 {
167 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
168
169 return GTK_TOGGLE_BUTTON(m_widget)->active;
170 }
171
172 bool wxRadioButton::Enable( bool enable )
173 {
174 if ( !wxControl::Enable( enable ) )
175 return FALSE;
176
177 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
178
179 return TRUE;
180 }
181
182 void wxRadioButton::ApplyWidgetStyle()
183 {
184 SetWidgetStyle();
185 gtk_widget_set_style( m_widget, m_widgetStyle );
186 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
187 }
188
189 #endif