]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/radiobut.cpp
wxGTK compilation fixes
[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
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "radiobut.h"
13#endif
14
15#include "wx/radiobut.h"
dcf924a3
RR
16
17#if wxUSE_RADIOBOX
18
83624f79
RR
19#include "gdk/gdk.h"
20#include "gtk/gtk.h"
c801d85f 21
acfd422a
RR
22//-----------------------------------------------------------------------------
23// idle system
24//-----------------------------------------------------------------------------
25
26extern void wxapp_install_idle_handler();
27extern bool g_isIdle;
28
6de97a3b
RR
29//-----------------------------------------------------------------------------
30// data
31//-----------------------------------------------------------------------------
32
33extern bool g_blockEventsOnDrag;
34
35//-----------------------------------------------------------------------------
bb4549de 36// "clicked"
6de97a3b
RR
37//-----------------------------------------------------------------------------
38
bb4549de
RR
39static
40void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioButton *rb )
6de97a3b 41{
acfd422a
RR
42 if (g_isIdle) wxapp_install_idle_handler();
43
a2053b27 44 if (!rb->m_hasVMT) return;
bb4549de 45
f5d29b39 46 if (g_blockEventsOnDrag) return;
6de97a3b 47
f5d29b39
RR
48 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
49 event.SetInt( rb->GetValue() );
50 event.SetEventObject( rb );
51 rb->GetEventHandler()->ProcessEvent( event );
6de97a3b
RR
52}
53
bb4549de
RR
54//-----------------------------------------------------------------------------
55// wxRadioButton
56//-----------------------------------------------------------------------------
57
58IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
59
6de97a3b
RR
60bool wxRadioButton::Create( wxWindow *parent, wxWindowID id, const wxString& label,
61 const wxPoint& pos, const wxSize& size, long style,
62 const wxValidator& validator, const wxString& name )
63{
b292e2f5 64 m_acceptsFocus = TRUE;
f5d29b39 65 m_needParent = TRUE;
6de97a3b 66
f5d29b39 67 wxSize newSize = size;
6de97a3b 68
f5d29b39 69 PreCreation( parent, id, pos, newSize, style, name );
953704c1
RR
70
71 m_isRadioButton = TRUE;
6de97a3b 72
ce4169a4 73#if wxUSE_VALIDATORS
f5d29b39 74 SetValidator( validator );
ce4169a4 75#endif
6de97a3b 76
953704c1
RR
77
78 if (HasFlag(wxRB_GROUP))
79 {
80 /* start a new group */
81 m_radioButtonGroup = (GSList*) NULL;
82 }
83 else
84 {
85 /* search backward for last group start */
86 wxRadioButton *chief = (wxRadioButton*) NULL;
87 wxWindowList::Node *node = parent->GetChildren().GetLast();
88 while (node)
89 {
90 wxWindow *child = node->GetData();
91 if (child->m_isRadioButton)
92 {
93 chief = (wxRadioButton*) child;
94 if (child->HasFlag(wxRB_GROUP)) break;
95 }
953704c1
RR
96 node = node->GetPrevious();
97 }
0544bc0a
RR
98 if (chief)
99 {
100 /* we are part of the group started by chief */
101 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
102 }
103 else
104 {
105 /* start a new group */
106 m_radioButtonGroup = (GSList*) NULL;
107 }
953704c1
RR
108 }
109
110 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, label.mbc_str() );
6de97a3b 111
f5d29b39 112 SetLabel(label);
6de97a3b 113
b019151f 114 if (newSize.x == -1) newSize.x = 22+gdk_string_measure( m_widget->style->font, label.mbc_str() );
f5d29b39
RR
115 if (newSize.y == -1) newSize.y = 26;
116 SetSize( newSize.x, newSize.y );
6de97a3b 117
f5d29b39
RR
118 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
119 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
6de97a3b 120
f03fc89f 121 m_parent->DoAddChild( this );
6ca41e57 122
f5d29b39 123 PostCreation();
6de97a3b 124
f5d29b39
RR
125 SetBackgroundColour( parent->GetBackgroundColour() );
126 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 127 SetFont( parent->GetFont() );
58614078 128
f5d29b39 129 Show( TRUE );
6de97a3b 130
f5d29b39 131 return TRUE;
6de97a3b
RR
132}
133
134void wxRadioButton::SetLabel( const wxString& label )
135{
b019151f 136 wxCHECK_RET( m_widget != NULL, _T("invalid radiobutton") );
f96aa4d9 137
f5d29b39
RR
138 wxControl::SetLabel( label );
139 GtkButton *bin = GTK_BUTTON( m_widget );
140 GtkLabel *g_label = GTK_LABEL( bin->child );
b019151f 141 gtk_label_set( g_label, GetLabel().mbc_str() );
6de97a3b
RR
142}
143
144void wxRadioButton::SetValue( bool val )
145{
b019151f 146 wxCHECK_RET( m_widget != NULL, _T("invalid radiobutton") );
f96aa4d9 147
953704c1 148 if (val == GetValue())
0659e7ee
RR
149 return;
150
953704c1
RR
151 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
152 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
153
f5d29b39 154 if (val)
953704c1 155 {
f5d29b39 156 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 157 }
f5d29b39 158 else
953704c1
RR
159 {
160 // should give an assert
161 }
162
163 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
164 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
6de97a3b
RR
165}
166
eb082a08 167bool wxRadioButton::GetValue() const
6de97a3b 168{
b019151f 169 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobutton") );
f96aa4d9 170
f5d29b39 171 return GTK_TOGGLE_BUTTON(m_widget)->active;
6de97a3b
RR
172}
173
f03fc89f 174bool wxRadioButton::Enable( bool enable )
d3904ceb 175{
f03fc89f
VZ
176 if ( !wxControl::Enable( enable ) )
177 return FALSE;
f96aa4d9 178
f5d29b39 179 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
f03fc89f
VZ
180
181 return TRUE;
d3904ceb
RR
182}
183
58614078 184void wxRadioButton::ApplyWidgetStyle()
868a2826 185{
f5d29b39
RR
186 SetWidgetStyle();
187 gtk_widget_set_style( m_widget, m_widgetStyle );
188 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
f96aa4d9 189}
dcf924a3
RR
190
191#endif