]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/radiobut.cpp
Rearrange code to fix some problems
[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
6de97a3b
RR
19//-----------------------------------------------------------------------------
20// data
21//-----------------------------------------------------------------------------
22
d7fa7eaa 23extern bool g_blockEventsOnDrag;
6de97a3b
RR
24
25//-----------------------------------------------------------------------------
bb4549de 26// "clicked"
6de97a3b
RR
27//-----------------------------------------------------------------------------
28
865bb325 29extern "C" {
b2ff89d6 30static
e2762ff0 31void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
6de97a3b 32{
a2053b27 33 if (!rb->m_hasVMT) return;
b2ff89d6 34
f5d29b39 35 if (g_blockEventsOnDrag) return;
b2ff89d6 36
e2762ff0 37 if (!button->active) return;
b2ff89d6 38
9864c56d 39 if (rb->m_blockEvent) return;
b2ff89d6 40
f5d29b39
RR
41 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
42 event.SetInt( rb->GetValue() );
43 event.SetEventObject( rb );
44 rb->GetEventHandler()->ProcessEvent( event );
6de97a3b 45}
865bb325 46}
6de97a3b 47
bb4549de
RR
48//-----------------------------------------------------------------------------
49// wxRadioButton
50//-----------------------------------------------------------------------------
51
52IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
b2ff89d6 53
2b4f3c9f
VZ
54bool 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 )
6de97a3b 62{
e8375af8 63 m_blockEvent = false;
6de97a3b 64
4dcaf11a
RR
65 if (!PreCreation( parent, pos, size ) ||
66 !CreateBase( parent, id, pos, size, style, validator, name ))
67 {
223d09f6 68 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
e8375af8 69 return false;
4dcaf11a 70 }
953704c1 71
739b7529
DS
72 GSList* radioButtonGroup = NULL;
73 if (!HasFlag(wxRB_GROUP))
953704c1 74 {
9864c56d 75 // search backward for last group start
953704c1 76 wxRadioButton *chief = (wxRadioButton*) NULL;
222ed1d6 77 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
953704c1 78 while (node)
e2762ff0
RR
79 {
80 wxWindow *child = node->GetData();
2b4f3c9f 81 if (child->IsRadioButton())
e2762ff0
RR
82 {
83 chief = (wxRadioButton*) child;
2b4f3c9f
VZ
84 if (child->HasFlag(wxRB_GROUP))
85 break;
e2762ff0
RR
86 }
87 node = node->GetPrevious();
953704c1 88 }
e2762ff0
RR
89 if (chief)
90 {
9864c56d 91 // we are part of the group started by chief
78e017d8 92 radioButtonGroup = gtk_radio_button_get_group( GTK_RADIO_BUTTON(chief->m_widget) );
e2762ff0 93 }
953704c1
RR
94 }
95
739b7529 96 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
b2ff89d6 97
f5d29b39 98 SetLabel(label);
6de97a3b 99
9fa72bd2
MR
100 g_signal_connect (m_widget, "clicked",
101 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
b2ff89d6 102
f03fc89f 103 m_parent->DoAddChild( this );
b2ff89d6 104
abdeb9e7 105 PostCreation(size);
6de97a3b 106
f5d29b39 107 return TRUE;
6de97a3b
RR
108}
109
110void wxRadioButton::SetLabel( const wxString& label )
111{
223d09f6 112 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
b2ff89d6 113
afa7bd1e 114 GTKSetLabelForLabel(GTK_LABEL(GTK_BIN(m_widget)->child), label);
6de97a3b
RR
115}
116
117void wxRadioButton::SetValue( bool val )
118{
223d09f6 119 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f6bcfd97 120
953704c1 121 if (val == GetValue())
0659e7ee
RR
122 return;
123
9864c56d 124 m_blockEvent = TRUE;
953704c1 125
f5d29b39 126 if (val)
953704c1 127 {
e2762ff0 128 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 129 }
f5d29b39 130 else
953704c1
RR
131 {
132 // should give an assert
f6bcfd97
BP
133 // RL - No it shouldn't. A wxGenericValidator might try to set it
134 // as FALSE. Failing silently is probably TRTTD here.
953704c1 135 }
f6bcfd97 136
9864c56d 137 m_blockEvent = FALSE;
6de97a3b
RR
138}
139
eb082a08 140bool wxRadioButton::GetValue() const
6de97a3b 141{
223d09f6 142 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
b2ff89d6 143
f5d29b39 144 return GTK_TOGGLE_BUTTON(m_widget)->active;
6de97a3b
RR
145}
146
f03fc89f 147bool wxRadioButton::Enable( bool enable )
d3904ceb 148{
f03fc89f
VZ
149 if ( !wxControl::Enable( enable ) )
150 return FALSE;
b2ff89d6 151
afa7bd1e 152 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
f03fc89f
VZ
153
154 return TRUE;
d3904ceb
RR
155}
156
f40fdaa3 157void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 158{
f40fdaa3 159 gtk_widget_modify_style(m_widget, style);
afa7bd1e 160 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
f96aa4d9 161}
dcf924a3 162
ef5c70f9
VZ
163GdkWindow *
164wxRadioButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2f073eb2 165{
ef5c70f9 166 return GTK_BUTTON(m_widget)->event_window;
2f073eb2
RR
167}
168
db434467
RR
169wxSize wxRadioButton::DoGetBestSize() const
170{
171 return wxControl::DoGetBestSize();
172}
173
9d522606
RD
174// static
175wxVisualAttributes
176wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
177{
178 wxVisualAttributes attr;
bc0eb46c
VS
179 // NB: we need toplevel window so that GTK+ can find the right style
180 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 181 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
bc0eb46c 182 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 183 attr = GetDefaultAttributesFromGTKWidget(widget);
bc0eb46c 184 gtk_widget_destroy(wnd);
9d522606
RD
185 return attr;
186}
187
188
dcf924a3 189#endif