| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: radiobut.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // For compilers that support precompilation, includes "wx.h". |
| 11 | #include "wx/wxprec.h" |
| 12 | |
| 13 | #if wxUSE_RADIOBTN |
| 14 | |
| 15 | #include "wx/radiobut.h" |
| 16 | |
| 17 | #include "wx/gtk/private.h" |
| 18 | |
| 19 | //----------------------------------------------------------------------------- |
| 20 | // data |
| 21 | //----------------------------------------------------------------------------- |
| 22 | |
| 23 | extern bool g_blockEventsOnDrag; |
| 24 | |
| 25 | //----------------------------------------------------------------------------- |
| 26 | // "clicked" |
| 27 | //----------------------------------------------------------------------------- |
| 28 | |
| 29 | extern "C" { |
| 30 | static |
| 31 | void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb ) |
| 32 | { |
| 33 | if (!rb->m_hasVMT) return; |
| 34 | |
| 35 | if (g_blockEventsOnDrag) return; |
| 36 | |
| 37 | if (!button->active) return; |
| 38 | |
| 39 | wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId()); |
| 40 | event.SetInt( rb->GetValue() ); |
| 41 | event.SetEventObject( rb ); |
| 42 | rb->HandleWindowEvent( event ); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | //----------------------------------------------------------------------------- |
| 47 | // wxRadioButton |
| 48 | //----------------------------------------------------------------------------- |
| 49 | |
| 50 | IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl) |
| 51 | |
| 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 ) |
| 60 | { |
| 61 | if (!PreCreation( parent, pos, size ) || |
| 62 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 63 | { |
| 64 | wxFAIL_MSG( wxT("wxRadioButton creation failed") ); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | GSList* radioButtonGroup = NULL; |
| 69 | if (!HasFlag(wxRB_GROUP)) |
| 70 | { |
| 71 | // search backward for last group start |
| 72 | wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast(); |
| 73 | for (; node; node = node->GetPrevious()) |
| 74 | { |
| 75 | wxWindow *child = node->GetData(); |
| 76 | if (child->HasFlag(wxRB_GROUP) && wxIsKindOf(child, wxRadioButton)) |
| 77 | { |
| 78 | radioButtonGroup = gtk_radio_button_get_group( |
| 79 | GTK_RADIO_BUTTON(child->m_widget)); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) ); |
| 86 | |
| 87 | SetLabel(label); |
| 88 | |
| 89 | g_signal_connect (m_widget, "clicked", |
| 90 | G_CALLBACK (gtk_radiobutton_clicked_callback), this); |
| 91 | |
| 92 | m_parent->DoAddChild( this ); |
| 93 | |
| 94 | PostCreation(size); |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | void wxRadioButton::SetLabel( const wxString& label ) |
| 100 | { |
| 101 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") ); |
| 102 | |
| 103 | GTKSetLabelForLabel(GTK_LABEL(GTK_BIN(m_widget)->child), label); |
| 104 | } |
| 105 | |
| 106 | void wxRadioButton::SetValue( bool val ) |
| 107 | { |
| 108 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") ); |
| 109 | |
| 110 | if (val == GetValue()) |
| 111 | return; |
| 112 | |
| 113 | g_signal_handlers_block_by_func( |
| 114 | m_widget, (void*)gtk_radiobutton_clicked_callback, this); |
| 115 | |
| 116 | if (val) |
| 117 | { |
| 118 | gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE ); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | // should give an assert |
| 123 | // RL - No it shouldn't. A wxGenericValidator might try to set it |
| 124 | // as FALSE. Failing silently is probably TRTTD here. |
| 125 | } |
| 126 | |
| 127 | g_signal_handlers_unblock_by_func( |
| 128 | m_widget, (void*)gtk_radiobutton_clicked_callback, this); |
| 129 | } |
| 130 | |
| 131 | bool wxRadioButton::GetValue() const |
| 132 | { |
| 133 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobutton") ); |
| 134 | |
| 135 | return GTK_TOGGLE_BUTTON(m_widget)->active; |
| 136 | } |
| 137 | |
| 138 | bool wxRadioButton::Enable( bool enable ) |
| 139 | { |
| 140 | if ( !wxControl::Enable( enable ) ) |
| 141 | return false; |
| 142 | |
| 143 | gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style) |
| 149 | { |
| 150 | gtk_widget_modify_style(m_widget, style); |
| 151 | gtk_widget_modify_style(GTK_BIN(m_widget)->child, style); |
| 152 | } |
| 153 | |
| 154 | GdkWindow * |
| 155 | wxRadioButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
| 156 | { |
| 157 | return GTK_BUTTON(m_widget)->event_window; |
| 158 | } |
| 159 | |
| 160 | // static |
| 161 | wxVisualAttributes |
| 162 | wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
| 163 | { |
| 164 | wxVisualAttributes attr; |
| 165 | // NB: we need toplevel window so that GTK+ can find the right style |
| 166 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 167 | GtkWidget* widget = gtk_radio_button_new_with_label(NULL, ""); |
| 168 | gtk_container_add(GTK_CONTAINER(wnd), widget); |
| 169 | attr = GetDefaultAttributesFromGTKWidget(widget); |
| 170 | gtk_widget_destroy(wnd); |
| 171 | return attr; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | #endif |