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