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