]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/radiobut.cpp
support for standard command IDs
[wxWidgets.git] / src / gtk / radiobut.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: radiobut.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
dcf924a3
RR
12
13#if wxUSE_RADIOBOX
14
1e6feb95
VZ
15#include "wx/radiobut.h"
16
9e691f46 17#include "wx/gtk/private.h"
c801d85f 18
acfd422a
RR
19//-----------------------------------------------------------------------------
20// idle system
21//-----------------------------------------------------------------------------
22
23extern void wxapp_install_idle_handler();
24extern bool g_isIdle;
25
6de97a3b
RR
26//-----------------------------------------------------------------------------
27// data
28//-----------------------------------------------------------------------------
29
d7fa7eaa
RR
30extern bool g_blockEventsOnDrag;
31extern wxCursor g_globalCursor;
32extern wxWindowGTK *g_delayedFocus;
6de97a3b
RR
33
34//-----------------------------------------------------------------------------
bb4549de 35// "clicked"
6de97a3b
RR
36//-----------------------------------------------------------------------------
37
865bb325 38extern "C" {
bb4549de 39static
e2762ff0 40void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
6de97a3b 41{
acfd422a
RR
42 if (g_isIdle) wxapp_install_idle_handler();
43
a2053b27 44 if (!rb->m_hasVMT) return;
bb4549de 45
f5d29b39 46 if (g_blockEventsOnDrag) return;
6de97a3b 47
e2762ff0 48 if (!button->active) return;
9864c56d
RR
49
50 if (rb->m_blockEvent) return;
e2762ff0 51
f5d29b39
RR
52 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
53 event.SetInt( rb->GetValue() );
54 event.SetEventObject( rb );
55 rb->GetEventHandler()->ProcessEvent( event );
6de97a3b 56}
865bb325 57}
6de97a3b 58
bb4549de
RR
59//-----------------------------------------------------------------------------
60// wxRadioButton
61//-----------------------------------------------------------------------------
62
63IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
64
2b4f3c9f
VZ
65bool wxRadioButton::Create( wxWindow *parent,
66 wxWindowID id,
67 const wxString& label,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxValidator& validator,
72 const wxString& name )
6de97a3b 73{
b292e2f5 74 m_acceptsFocus = TRUE;
f5d29b39 75 m_needParent = TRUE;
9864c56d
RR
76
77 m_blockEvent = FALSE;
6de97a3b 78
4dcaf11a
RR
79 if (!PreCreation( parent, pos, size ) ||
80 !CreateBase( parent, id, pos, size, style, validator, name ))
81 {
223d09f6 82 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
9864c56d 83 return FALSE;
4dcaf11a 84 }
953704c1 85
739b7529
DS
86 GSList* radioButtonGroup = NULL;
87 if (!HasFlag(wxRB_GROUP))
953704c1 88 {
9864c56d 89 // search backward for last group start
953704c1 90 wxRadioButton *chief = (wxRadioButton*) NULL;
222ed1d6 91 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
953704c1 92 while (node)
e2762ff0
RR
93 {
94 wxWindow *child = node->GetData();
2b4f3c9f 95 if (child->IsRadioButton())
e2762ff0
RR
96 {
97 chief = (wxRadioButton*) child;
2b4f3c9f
VZ
98 if (child->HasFlag(wxRB_GROUP))
99 break;
e2762ff0
RR
100 }
101 node = node->GetPrevious();
953704c1 102 }
e2762ff0
RR
103 if (chief)
104 {
9864c56d 105 // we are part of the group started by chief
739b7529 106 radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
e2762ff0 107 }
953704c1
RR
108 }
109
739b7529 110 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
6de97a3b 111
f5d29b39 112 SetLabel(label);
6de97a3b 113
f5d29b39
RR
114 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
115 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
6de97a3b 116
f03fc89f 117 m_parent->DoAddChild( this );
6ca41e57 118
abdeb9e7 119 PostCreation(size);
6de97a3b 120
f5d29b39 121 return TRUE;
6de97a3b
RR
122}
123
124void wxRadioButton::SetLabel( const wxString& label )
125{
223d09f6 126 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f96aa4d9 127
f5d29b39 128 wxControl::SetLabel( label );
9e691f46 129 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(m_widget) );
eaafd2f8
VS
130#ifdef __WXGTK20__
131 wxString label2 = PrepareLabelMnemonics( label );
132 gtk_label_set_text_with_mnemonic( g_label, wxGTK_CONV( label2 ) );
133#else
fab591c5 134 gtk_label_set( g_label, wxGTK_CONV( GetLabel() ) );
eaafd2f8 135#endif
6de97a3b
RR
136}
137
138void wxRadioButton::SetValue( bool val )
139{
223d09f6 140 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f6bcfd97 141
953704c1 142 if (val == GetValue())
0659e7ee
RR
143 return;
144
9864c56d 145 m_blockEvent = TRUE;
953704c1 146
f5d29b39 147 if (val)
953704c1 148 {
e2762ff0 149 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 150 }
f5d29b39 151 else
953704c1
RR
152 {
153 // should give an assert
f6bcfd97
BP
154 // RL - No it shouldn't. A wxGenericValidator might try to set it
155 // as FALSE. Failing silently is probably TRTTD here.
953704c1 156 }
f6bcfd97 157
9864c56d 158 m_blockEvent = FALSE;
6de97a3b
RR
159}
160
eb082a08 161bool wxRadioButton::GetValue() const
6de97a3b 162{
223d09f6 163 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
f96aa4d9 164
f5d29b39 165 return GTK_TOGGLE_BUTTON(m_widget)->active;
6de97a3b
RR
166}
167
f03fc89f 168bool wxRadioButton::Enable( bool enable )
d3904ceb 169{
f03fc89f
VZ
170 if ( !wxControl::Enable( enable ) )
171 return FALSE;
f96aa4d9 172
9e691f46 173 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
174
175 return TRUE;
d3904ceb
RR
176}
177
f40fdaa3 178void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 179{
f40fdaa3
VS
180 gtk_widget_modify_style(m_widget, style);
181 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
f96aa4d9 182}
dcf924a3 183
2f073eb2
RR
184bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
185{
9e691f46 186 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
187}
188
189void wxRadioButton::OnInternalIdle()
190{
191 wxCursor cursor = m_cursor;
192 if (g_globalCursor.Ok()) cursor = g_globalCursor;
193
9e691f46
VZ
194 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
195 if ( win && cursor.Ok())
2f073eb2
RR
196 {
197 /* I now set the cursor the anew in every OnInternalIdle call
e2762ff0
RR
198 as setting the cursor in a parent window also effects the
199 windows above so that checking for the current cursor is
200 not possible. */
201
9e691f46 202 gdk_window_set_cursor( win, cursor.GetCursor() );
2f073eb2
RR
203 }
204
d7fa7eaa
RR
205 if (g_delayedFocus == this)
206 {
207 if (GTK_WIDGET_REALIZED(m_widget))
208 {
209 gtk_widget_grab_focus( m_widget );
210 g_delayedFocus = NULL;
211 }
212 }
213
e39af974
JS
214 if (wxUpdateUIEvent::CanUpdate(this))
215 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2f073eb2
RR
216}
217
db434467
RR
218wxSize wxRadioButton::DoGetBestSize() const
219{
220 return wxControl::DoGetBestSize();
221}
222
9d522606
RD
223// static
224wxVisualAttributes
225wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
226{
227 wxVisualAttributes attr;
bc0eb46c
VS
228 // NB: we need toplevel window so that GTK+ can find the right style
229 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 230 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
bc0eb46c 231 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 232 attr = GetDefaultAttributesFromGTKWidget(widget);
bc0eb46c 233 gtk_widget_destroy(wnd);
9d522606
RD
234 return attr;
235}
236
237
dcf924a3 238#endif