]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/gtk/radiobut.cpp |
c801d85f KB |
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 | ||
9dc44eff | 17 | #include <gtk/gtk.h> |
9e691f46 | 18 | #include "wx/gtk/private.h" |
9dc44eff | 19 | #include "wx/gtk/private/gtk2-compat.h" |
c801d85f | 20 | |
6de97a3b RR |
21 | //----------------------------------------------------------------------------- |
22 | // data | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
d7fa7eaa | 25 | extern bool g_blockEventsOnDrag; |
6de97a3b RR |
26 | |
27 | //----------------------------------------------------------------------------- | |
bb4549de | 28 | // "clicked" |
6de97a3b RR |
29 | //----------------------------------------------------------------------------- |
30 | ||
865bb325 | 31 | extern "C" { |
b2ff89d6 | 32 | static |
e2762ff0 | 33 | void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb ) |
6de97a3b | 34 | { |
a2053b27 | 35 | if (!rb->m_hasVMT) return; |
b2ff89d6 | 36 | |
f5d29b39 | 37 | if (g_blockEventsOnDrag) return; |
b2ff89d6 | 38 | |
385e8575 | 39 | if (!gtk_toggle_button_get_active(button)) return; |
b2ff89d6 | 40 | |
f5d29b39 RR |
41 | wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId()); |
42 | event.SetInt( rb->GetValue() ); | |
43 | event.SetEventObject( rb ); | |
937013e0 | 44 | rb->HandleWindowEvent( event ); |
6de97a3b | 45 | } |
865bb325 | 46 | } |
6de97a3b | 47 | |
bb4549de RR |
48 | //----------------------------------------------------------------------------- |
49 | // wxRadioButton | |
50 | //----------------------------------------------------------------------------- | |
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 | |
a45c6a89 VZ |
68 | // Check if this radio button should be put into an existing group. This |
69 | // shouldn't be done if it's given a style to explicitly start a new group | |
70 | // or if it's not meant to be a part of a group at all. | |
739b7529 | 71 | GSList* radioButtonGroup = NULL; |
f938aa99 | 72 | if (!HasFlag(wxRB_GROUP) && !HasFlag(wxRB_SINGLE)) |
953704c1 | 73 | { |
9864c56d | 74 | // search backward for last group start |
222ed1d6 | 75 | wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast(); |
d968078a | 76 | for (; node; node = node->GetPrevious()) |
e2762ff0 RR |
77 | { |
78 | wxWindow *child = node->GetData(); | |
a45c6a89 VZ |
79 | |
80 | // We stop at the first previous radio button in any case as it | |
81 | // wouldn't make sense to put this button in a group with another | |
82 | // one if there is a radio button that is not part of the same | |
83 | // group between them. | |
84 | if (wxIsKindOf(child, wxRadioButton)) | |
e2762ff0 | 85 | { |
a45c6a89 VZ |
86 | // Any preceding radio button can be used to get its group, not |
87 | // necessarily one with wxRB_GROUP style, but exclude | |
88 | // wxRB_SINGLE ones as their group should never be shared. | |
89 | if (!child->HasFlag(wxRB_SINGLE)) | |
90 | { | |
91 | radioButtonGroup = gtk_radio_button_get_group( | |
92 | GTK_RADIO_BUTTON(child->m_widget)); | |
93 | } | |
94 | ||
d968078a | 95 | break; |
e2762ff0 | 96 | } |
e2762ff0 | 97 | } |
953704c1 RR |
98 | } |
99 | ||
739b7529 | 100 | m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) ); |
9ff9d30c | 101 | g_object_ref(m_widget); |
b2ff89d6 | 102 | |
f5d29b39 | 103 | SetLabel(label); |
6de97a3b | 104 | |
e9a7fde5 RR |
105 | g_signal_connect_after (m_widget, "clicked", |
106 | G_CALLBACK (gtk_radiobutton_clicked_callback), this); | |
b2ff89d6 | 107 | |
f03fc89f | 108 | m_parent->DoAddChild( this ); |
b2ff89d6 | 109 | |
abdeb9e7 | 110 | PostCreation(size); |
6de97a3b | 111 | |
d968078a | 112 | return true; |
6de97a3b RR |
113 | } |
114 | ||
115 | void wxRadioButton::SetLabel( const wxString& label ) | |
116 | { | |
223d09f6 | 117 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") ); |
b2ff89d6 | 118 | |
6ea2bc50 VZ |
119 | // save the original label |
120 | wxControlBase::SetLabel(label); | |
121 | ||
385e8575 | 122 | GTKSetLabelForLabel(GTK_LABEL(gtk_bin_get_child(GTK_BIN(m_widget))), label); |
6de97a3b RR |
123 | } |
124 | ||
125 | void wxRadioButton::SetValue( bool val ) | |
126 | { | |
223d09f6 | 127 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") ); |
f6bcfd97 | 128 | |
953704c1 | 129 | if (val == GetValue()) |
0659e7ee RR |
130 | return; |
131 | ||
d968078a PC |
132 | g_signal_handlers_block_by_func( |
133 | m_widget, (void*)gtk_radiobutton_clicked_callback, this); | |
953704c1 | 134 | |
f5d29b39 | 135 | if (val) |
953704c1 | 136 | { |
e2762ff0 | 137 | gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE ); |
953704c1 | 138 | } |
f5d29b39 | 139 | else |
953704c1 RR |
140 | { |
141 | // should give an assert | |
f6bcfd97 BP |
142 | // RL - No it shouldn't. A wxGenericValidator might try to set it |
143 | // as FALSE. Failing silently is probably TRTTD here. | |
953704c1 | 144 | } |
f6bcfd97 | 145 | |
d968078a PC |
146 | g_signal_handlers_unblock_by_func( |
147 | m_widget, (void*)gtk_radiobutton_clicked_callback, this); | |
6de97a3b RR |
148 | } |
149 | ||
eb082a08 | 150 | bool wxRadioButton::GetValue() const |
6de97a3b | 151 | { |
d968078a | 152 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobutton") ); |
b2ff89d6 | 153 | |
385e8575 | 154 | return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget)) != 0; |
6de97a3b RR |
155 | } |
156 | ||
f03fc89f | 157 | bool wxRadioButton::Enable( bool enable ) |
d3904ceb | 158 | { |
b545684e | 159 | if (!base_type::Enable(enable)) |
d968078a | 160 | return false; |
b2ff89d6 | 161 | |
385e8575 | 162 | gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget)), enable); |
f03fc89f | 163 | |
b545684e | 164 | if (enable) |
ad60f9e7 | 165 | GTKFixSensitivity(); |
ad60f9e7 | 166 | |
d968078a | 167 | return true; |
d3904ceb RR |
168 | } |
169 | ||
f40fdaa3 | 170 | void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 171 | { |
9dc44eff PC |
172 | GTKApplyStyle(m_widget, style); |
173 | GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style); | |
f96aa4d9 | 174 | } |
dcf924a3 | 175 | |
ef5c70f9 VZ |
176 | GdkWindow * |
177 | wxRadioButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const | |
2f073eb2 | 178 | { |
9dc44eff | 179 | return gtk_button_get_event_window(GTK_BUTTON(m_widget)); |
2f073eb2 RR |
180 | } |
181 | ||
9d522606 RD |
182 | // static |
183 | wxVisualAttributes | |
184 | wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
185 | { | |
186 | wxVisualAttributes attr; | |
bc0eb46c VS |
187 | // NB: we need toplevel window so that GTK+ can find the right style |
188 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
9d522606 | 189 | GtkWidget* widget = gtk_radio_button_new_with_label(NULL, ""); |
bc0eb46c | 190 | gtk_container_add(GTK_CONTAINER(wnd), widget); |
9d522606 | 191 | attr = GetDefaultAttributesFromGTKWidget(widget); |
bc0eb46c | 192 | gtk_widget_destroy(wnd); |
9d522606 RD |
193 | return attr; |
194 | } | |
195 | ||
196 | ||
dcf924a3 | 197 | #endif |