]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/radiobut.cpp
common rtti in fontcmn.cpp
[wxWidgets.git] / src / gtk1 / radiobut.cpp
CommitLineData
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
3cbab641 17#include "wx/gtk1/private.h"
c801d85f 18
acfd422a
RR
19//-----------------------------------------------------------------------------
20// idle system
21//-----------------------------------------------------------------------------
22
23extern void wxapp_install_idle_handler();
24extern bool g_isIdle;
25
6de97a3b
RR
26//-----------------------------------------------------------------------------
27// data
28//-----------------------------------------------------------------------------
29
d7fa7eaa
RR
30extern bool g_blockEventsOnDrag;
31extern wxCursor g_globalCursor;
32extern wxWindowGTK *g_delayedFocus;
6de97a3b
RR
33
34//-----------------------------------------------------------------------------
bb4549de 35// "clicked"
6de97a3b
RR
36//-----------------------------------------------------------------------------
37
865bb325 38extern "C" {
b2ff89d6 39static
e2762ff0 40void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
6de97a3b 41{
acfd422a
RR
42 if (g_isIdle) wxapp_install_idle_handler();
43
a2053b27 44 if (!rb->m_hasVMT) return;
b2ff89d6 45
f5d29b39 46 if (g_blockEventsOnDrag) return;
b2ff89d6 47
e2762ff0 48 if (!button->active) return;
b2ff89d6 49
9864c56d 50 if (rb->m_blockEvent) return;
b2ff89d6 51
f5d29b39
RR
52 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
53 event.SetInt( rb->GetValue() );
54 event.SetEventObject( rb );
937013e0 55 rb->HandleWindowEvent( event );
6de97a3b 56}
865bb325 57}
6de97a3b 58
bb4549de
RR
59//-----------------------------------------------------------------------------
60// wxRadioButton
61//-----------------------------------------------------------------------------
62
2b4f3c9f
VZ
63bool wxRadioButton::Create( wxWindow *parent,
64 wxWindowID id,
65 const wxString& label,
66 const wxPoint& pos,
67 const wxSize& size,
68 long style,
69 const wxValidator& validator,
70 const wxString& name )
6de97a3b 71{
b292e2f5 72 m_acceptsFocus = TRUE;
f5d29b39 73 m_needParent = TRUE;
b2ff89d6 74
9864c56d 75 m_blockEvent = FALSE;
6de97a3b 76
4dcaf11a
RR
77 if (!PreCreation( parent, pos, size ) ||
78 !CreateBase( parent, id, pos, size, style, validator, name ))
79 {
223d09f6 80 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
9864c56d 81 return FALSE;
4dcaf11a 82 }
953704c1 83
739b7529
DS
84 GSList* radioButtonGroup = NULL;
85 if (!HasFlag(wxRB_GROUP))
953704c1 86 {
9864c56d 87 // search backward for last group start
d3b9f782 88 wxRadioButton *chief = NULL;
222ed1d6 89 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
953704c1 90 while (node)
e2762ff0
RR
91 {
92 wxWindow *child = node->GetData();
2b4f3c9f 93 if (child->IsRadioButton())
e2762ff0
RR
94 {
95 chief = (wxRadioButton*) child;
2b4f3c9f
VZ
96 if (child->HasFlag(wxRB_GROUP))
97 break;
e2762ff0
RR
98 }
99 node = node->GetPrevious();
953704c1 100 }
e2762ff0
RR
101 if (chief)
102 {
9864c56d 103 // we are part of the group started by chief
739b7529 104 radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
e2762ff0 105 }
953704c1
RR
106 }
107
739b7529 108 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
b2ff89d6 109
f5d29b39 110 SetLabel(label);
6de97a3b 111
b2ff89d6 112 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
f5d29b39 113 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
b2ff89d6 114
f03fc89f 115 m_parent->DoAddChild( this );
b2ff89d6 116
abdeb9e7 117 PostCreation(size);
6de97a3b 118
f5d29b39 119 return TRUE;
6de97a3b
RR
120}
121
122void wxRadioButton::SetLabel( const wxString& label )
123{
223d09f6 124 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
b2ff89d6
VZ
125
126 GTKSetLabelForLabel(GTK_LABEL(BUTTON_CHILD(m_widget)), label);
6de97a3b
RR
127}
128
129void wxRadioButton::SetValue( bool val )
130{
223d09f6 131 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f6bcfd97 132
953704c1 133 if (val == GetValue())
0659e7ee
RR
134 return;
135
9864c56d 136 m_blockEvent = TRUE;
953704c1 137
f5d29b39 138 if (val)
953704c1 139 {
e2762ff0 140 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 141 }
f5d29b39 142 else
953704c1
RR
143 {
144 // should give an assert
f6bcfd97
BP
145 // RL - No it shouldn't. A wxGenericValidator might try to set it
146 // as FALSE. Failing silently is probably TRTTD here.
953704c1 147 }
f6bcfd97 148
9864c56d 149 m_blockEvent = FALSE;
6de97a3b
RR
150}
151
eb082a08 152bool wxRadioButton::GetValue() const
6de97a3b 153{
223d09f6 154 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
b2ff89d6 155
f5d29b39 156 return GTK_TOGGLE_BUTTON(m_widget)->active;
6de97a3b
RR
157}
158
f03fc89f 159bool wxRadioButton::Enable( bool enable )
d3904ceb 160{
f03fc89f
VZ
161 if ( !wxControl::Enable( enable ) )
162 return FALSE;
b2ff89d6 163
9e691f46 164 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
165
166 return TRUE;
d3904ceb
RR
167}
168
f40fdaa3 169void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 170{
f40fdaa3
VS
171 gtk_widget_modify_style(m_widget, style);
172 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
f96aa4d9 173}
dcf924a3 174
2f073eb2
RR
175bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
176{
9e691f46 177 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
178}
179
180void wxRadioButton::OnInternalIdle()
181{
182 wxCursor cursor = m_cursor;
183 if (g_globalCursor.Ok()) cursor = g_globalCursor;
184
9e691f46
VZ
185 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
186 if ( win && cursor.Ok())
2f073eb2
RR
187 {
188 /* I now set the cursor the anew in every OnInternalIdle call
e2762ff0
RR
189 as setting the cursor in a parent window also effects the
190 windows above so that checking for the current cursor is
191 not possible. */
b2ff89d6 192
9e691f46 193 gdk_window_set_cursor( win, cursor.GetCursor() );
2f073eb2
RR
194 }
195
d7fa7eaa
RR
196 if (g_delayedFocus == this)
197 {
198 if (GTK_WIDGET_REALIZED(m_widget))
199 {
200 gtk_widget_grab_focus( m_widget );
201 g_delayedFocus = NULL;
202 }
203 }
204
e39af974
JS
205 if (wxUpdateUIEvent::CanUpdate(this))
206 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2f073eb2
RR
207}
208
db434467
RR
209wxSize wxRadioButton::DoGetBestSize() const
210{
211 return wxControl::DoGetBestSize();
212}
213
9d522606
RD
214// static
215wxVisualAttributes
216wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
217{
218 wxVisualAttributes attr;
bc0eb46c
VS
219 // NB: we need toplevel window so that GTK+ can find the right style
220 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 221 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
bc0eb46c 222 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 223 attr = GetDefaultAttributesFromGTKWidget(widget);
bc0eb46c 224 gtk_widget_destroy(wnd);
9d522606
RD
225 return attr;
226}
227
228
dcf924a3 229#endif