]>
Commit | Line | Data |
---|---|---|
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 | 12 | |
d968078a | 13 | #if wxUSE_RADIOBTN |
dcf924a3 | 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 | 23 | extern bool g_blockEventsOnDrag; |
6de97a3b RR |
24 | |
25 | //----------------------------------------------------------------------------- | |
bb4549de | 26 | // "clicked" |
6de97a3b RR |
27 | //----------------------------------------------------------------------------- |
28 | ||
865bb325 | 29 | extern "C" { |
b2ff89d6 | 30 | static |
e2762ff0 | 31 | void 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 | |
f5d29b39 RR |
39 | wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId()); |
40 | event.SetInt( rb->GetValue() ); | |
41 | event.SetEventObject( rb ); | |
937013e0 | 42 | rb->HandleWindowEvent( event ); |
6de97a3b | 43 | } |
865bb325 | 44 | } |
6de97a3b | 45 | |
bb4549de RR |
46 | //----------------------------------------------------------------------------- |
47 | // wxRadioButton | |
48 | //----------------------------------------------------------------------------- | |
49 | ||
50 | IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl) | |
b2ff89d6 | 51 | |
2b4f3c9f VZ |
52 | bool wxRadioButton::Create( wxWindow *parent, |
53 | wxWindowID id, | |
54 | const wxString& label, | |
55 | const wxPoint& pos, | |
56 | const wxSize& size, | |
57 | long style, | |
58 | const wxValidator& validator, | |
59 | const wxString& name ) | |
6de97a3b | 60 | { |
4dcaf11a RR |
61 | if (!PreCreation( parent, pos, size ) || |
62 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
63 | { | |
223d09f6 | 64 | wxFAIL_MSG( wxT("wxRadioButton creation failed") ); |
e8375af8 | 65 | return false; |
4dcaf11a | 66 | } |
953704c1 | 67 | |
739b7529 | 68 | GSList* radioButtonGroup = NULL; |
f938aa99 | 69 | if (!HasFlag(wxRB_GROUP) && !HasFlag(wxRB_SINGLE)) |
953704c1 | 70 | { |
9864c56d | 71 | // search backward for last group start |
222ed1d6 | 72 | wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast(); |
d968078a | 73 | for (; node; node = node->GetPrevious()) |
e2762ff0 RR |
74 | { |
75 | wxWindow *child = node->GetData(); | |
d968078a | 76 | if (child->HasFlag(wxRB_GROUP) && wxIsKindOf(child, wxRadioButton)) |
e2762ff0 | 77 | { |
d968078a PC |
78 | radioButtonGroup = gtk_radio_button_get_group( |
79 | GTK_RADIO_BUTTON(child->m_widget)); | |
80 | break; | |
e2762ff0 | 81 | } |
e2762ff0 | 82 | } |
953704c1 RR |
83 | } |
84 | ||
739b7529 | 85 | m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) ); |
9ff9d30c | 86 | g_object_ref(m_widget); |
b2ff89d6 | 87 | |
f5d29b39 | 88 | SetLabel(label); |
6de97a3b | 89 | |
e9a7fde5 RR |
90 | g_signal_connect_after (m_widget, "clicked", |
91 | G_CALLBACK (gtk_radiobutton_clicked_callback), this); | |
b2ff89d6 | 92 | |
f03fc89f | 93 | m_parent->DoAddChild( this ); |
b2ff89d6 | 94 | |
abdeb9e7 | 95 | PostCreation(size); |
6de97a3b | 96 | |
d968078a | 97 | return true; |
6de97a3b RR |
98 | } |
99 | ||
100 | void wxRadioButton::SetLabel( const wxString& label ) | |
101 | { | |
223d09f6 | 102 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") ); |
b2ff89d6 | 103 | |
afa7bd1e | 104 | GTKSetLabelForLabel(GTK_LABEL(GTK_BIN(m_widget)->child), label); |
6de97a3b RR |
105 | } |
106 | ||
107 | void wxRadioButton::SetValue( bool val ) | |
108 | { | |
223d09f6 | 109 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") ); |
f6bcfd97 | 110 | |
953704c1 | 111 | if (val == GetValue()) |
0659e7ee RR |
112 | return; |
113 | ||
d968078a PC |
114 | g_signal_handlers_block_by_func( |
115 | m_widget, (void*)gtk_radiobutton_clicked_callback, this); | |
953704c1 | 116 | |
f5d29b39 | 117 | if (val) |
953704c1 | 118 | { |
e2762ff0 | 119 | gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE ); |
953704c1 | 120 | } |
f5d29b39 | 121 | else |
953704c1 RR |
122 | { |
123 | // should give an assert | |
f6bcfd97 BP |
124 | // RL - No it shouldn't. A wxGenericValidator might try to set it |
125 | // as FALSE. Failing silently is probably TRTTD here. | |
953704c1 | 126 | } |
f6bcfd97 | 127 | |
d968078a PC |
128 | g_signal_handlers_unblock_by_func( |
129 | m_widget, (void*)gtk_radiobutton_clicked_callback, this); | |
6de97a3b RR |
130 | } |
131 | ||
eb082a08 | 132 | bool wxRadioButton::GetValue() const |
6de97a3b | 133 | { |
d968078a | 134 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobutton") ); |
b2ff89d6 | 135 | |
f5d29b39 | 136 | return GTK_TOGGLE_BUTTON(m_widget)->active; |
6de97a3b RR |
137 | } |
138 | ||
f03fc89f | 139 | bool wxRadioButton::Enable( bool enable ) |
d3904ceb | 140 | { |
f03fc89f | 141 | if ( !wxControl::Enable( enable ) ) |
d968078a | 142 | return false; |
b2ff89d6 | 143 | |
afa7bd1e | 144 | gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); |
f03fc89f | 145 | |
d968078a | 146 | return true; |
d3904ceb RR |
147 | } |
148 | ||
f40fdaa3 | 149 | void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 150 | { |
f40fdaa3 | 151 | gtk_widget_modify_style(m_widget, style); |
afa7bd1e | 152 | gtk_widget_modify_style(GTK_BIN(m_widget)->child, style); |
f96aa4d9 | 153 | } |
dcf924a3 | 154 | |
ef5c70f9 VZ |
155 | GdkWindow * |
156 | wxRadioButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const | |
2f073eb2 | 157 | { |
ef5c70f9 | 158 | return GTK_BUTTON(m_widget)->event_window; |
2f073eb2 RR |
159 | } |
160 | ||
9d522606 RD |
161 | // static |
162 | wxVisualAttributes | |
163 | wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
164 | { | |
165 | wxVisualAttributes attr; | |
bc0eb46c VS |
166 | // NB: we need toplevel window so that GTK+ can find the right style |
167 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
9d522606 | 168 | GtkWidget* widget = gtk_radio_button_new_with_label(NULL, ""); |
bc0eb46c | 169 | gtk_container_add(GTK_CONTAINER(wnd), widget); |
9d522606 | 170 | attr = GetDefaultAttributesFromGTKWidget(widget); |
bc0eb46c | 171 | gtk_widget_destroy(wnd); |
9d522606 RD |
172 | return attr; |
173 | } | |
174 | ||
175 | ||
dcf924a3 | 176 | #endif |