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