]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobut.cpp
Fix crash in wxDC::GetMultiLineTextExtent() after last commit.
[wxWidgets.git] / src / gtk / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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 <gtk/gtk.h>
18 #include "wx/gtk/private.h"
19 #include "wx/gtk/private/gtk2-compat.h"
20
21 //-----------------------------------------------------------------------------
22 // data
23 //-----------------------------------------------------------------------------
24
25 extern bool g_blockEventsOnDrag;
26
27 //-----------------------------------------------------------------------------
28 // "clicked"
29 //-----------------------------------------------------------------------------
30
31 extern "C" {
32 static
33 void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
34 {
35 if (g_blockEventsOnDrag) return;
36
37 if (!gtk_toggle_button_get_active(button)) 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 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 )
58 {
59 if (!PreCreation( parent, pos, size ) ||
60 !CreateBase( parent, id, pos, size, style, validator, name ))
61 {
62 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
63 return false;
64 }
65
66 // Check if this radio button should be put into an existing group. This
67 // shouldn't be done if it's given a style to explicitly start a new group
68 // or if it's not meant to be a part of a group at all.
69 GSList* radioButtonGroup = NULL;
70 if (!HasFlag(wxRB_GROUP) && !HasFlag(wxRB_SINGLE))
71 {
72 // search backward for last group start
73 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
74 for (; node; node = node->GetPrevious())
75 {
76 wxWindow *child = node->GetData();
77
78 // We stop at the first previous radio button in any case as it
79 // wouldn't make sense to put this button in a group with another
80 // one if there is a radio button that is not part of the same
81 // group between them.
82 if (wxIsKindOf(child, wxRadioButton))
83 {
84 // Any preceding radio button can be used to get its group, not
85 // necessarily one with wxRB_GROUP style, but exclude
86 // wxRB_SINGLE ones as their group should never be shared.
87 if (!child->HasFlag(wxRB_SINGLE))
88 {
89 radioButtonGroup = gtk_radio_button_get_group(
90 GTK_RADIO_BUTTON(child->m_widget));
91 }
92
93 break;
94 }
95 }
96 }
97
98 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
99 g_object_ref(m_widget);
100
101 SetLabel(label);
102
103 g_signal_connect_after (m_widget, "clicked",
104 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
105
106 m_parent->DoAddChild( this );
107
108 PostCreation(size);
109
110 return true;
111 }
112
113 void wxRadioButton::SetLabel( const wxString& label )
114 {
115 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
116
117 // save the original label
118 wxControlBase::SetLabel(label);
119
120 GTKSetLabelForLabel(GTK_LABEL(gtk_bin_get_child(GTK_BIN(m_widget))), label);
121 }
122
123 void wxRadioButton::SetValue( bool val )
124 {
125 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
126
127 if (val == GetValue())
128 return;
129
130 g_signal_handlers_block_by_func(
131 m_widget, (void*)gtk_radiobutton_clicked_callback, this);
132
133 if (val)
134 {
135 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
136 }
137 else
138 {
139 // should give an assert
140 // RL - No it shouldn't. A wxGenericValidator might try to set it
141 // as FALSE. Failing silently is probably TRTTD here.
142 }
143
144 g_signal_handlers_unblock_by_func(
145 m_widget, (void*)gtk_radiobutton_clicked_callback, this);
146 }
147
148 bool wxRadioButton::GetValue() const
149 {
150 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobutton") );
151
152 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget)) != 0;
153 }
154
155 bool wxRadioButton::Enable( bool enable )
156 {
157 if (!base_type::Enable(enable))
158 return false;
159
160 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget)), enable);
161
162 if (enable)
163 GTKFixSensitivity();
164
165 return true;
166 }
167
168 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
169 {
170 GTKApplyStyle(m_widget, style);
171 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style);
172 }
173
174 GdkWindow *
175 wxRadioButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
176 {
177 return gtk_button_get_event_window(GTK_BUTTON(m_widget));
178 }
179
180 // static
181 wxVisualAttributes
182 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
183 {
184 wxVisualAttributes attr;
185 // NB: we need toplevel window so that GTK+ can find the right style
186 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
187 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
188 gtk_container_add(GTK_CONTAINER(wnd), widget);
189 attr = GetDefaultAttributesFromGTKWidget(widget);
190 gtk_widget_destroy(wnd);
191 return attr;
192 }
193
194
195 #endif