]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/radiobut.cpp
wxWindowDC now uses its window's font
[wxWidgets.git] / src / gtk1 / radiobut.cpp
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
11 #ifdef __GNUG__
12 #pragma implementation "radiobut.h"
13 #endif
14
15 #include "wx/radiobut.h"
16
17 #if wxUSE_RADIOBOX
18
19 #include "gdk/gdk.h"
20 #include "gtk/gtk.h"
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34
35 //-----------------------------------------------------------------------------
36 // "clicked"
37 //-----------------------------------------------------------------------------
38
39 static
40 void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioButton *rb )
41 {
42 if (g_isIdle) wxapp_install_idle_handler();
43
44 if (!rb->m_hasVMT) return;
45
46 if (rb->m_blockFirstEvent)
47 {
48 rb->m_blockFirstEvent = FALSE;
49 return;
50 }
51
52 if (g_blockEventsOnDrag) return;
53
54 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
55 event.SetInt( rb->GetValue() );
56 event.SetEventObject( rb );
57 rb->GetEventHandler()->ProcessEvent( event );
58 }
59
60 //-----------------------------------------------------------------------------
61 // wxRadioButton
62 //-----------------------------------------------------------------------------
63
64 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
65
66 bool wxRadioButton::Create( wxWindow *parent, wxWindowID id, const wxString& label,
67 const wxPoint& pos, const wxSize& size, long style,
68 const wxValidator& validator, const wxString& name )
69 {
70 m_acceptsFocus = TRUE;
71 m_needParent = TRUE;
72
73 wxSize newSize = size;
74
75 PreCreation( parent, id, pos, newSize, style, name );
76
77 #if wxUSE_VALIDATORS
78 SetValidator( validator );
79 #endif
80
81 m_widget = gtk_radio_button_new_with_label( (GSList *) NULL, label.mbc_str() );
82
83 m_theOtherRadioButtton =
84 gtk_radio_button_new_with_label(
85 gtk_radio_button_group( GTK_RADIO_BUTTON(m_widget) ),
86 "button2" );
87
88 SetLabel(label);
89
90 m_blockFirstEvent = FALSE;
91
92 if (newSize.x == -1) newSize.x = 22+gdk_string_measure( m_widget->style->font, label.mbc_str() );
93 if (newSize.y == -1) newSize.y = 26;
94 SetSize( newSize.x, newSize.y );
95
96 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
97 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
98
99 m_parent->DoAddChild( this );
100
101 PostCreation();
102
103 SetBackgroundColour( parent->GetBackgroundColour() );
104 SetForegroundColour( parent->GetForegroundColour() );
105 SetFont( parent->GetFont() );
106
107 Show( TRUE );
108
109 return TRUE;
110 }
111
112 void wxRadioButton::SetLabel( const wxString& label )
113 {
114 wxCHECK_RET( m_widget != NULL, _T("invalid radiobutton") );
115
116 wxControl::SetLabel( label );
117 GtkButton *bin = GTK_BUTTON( m_widget );
118 GtkLabel *g_label = GTK_LABEL( bin->child );
119 gtk_label_set( g_label, GetLabel().mbc_str() );
120 }
121
122 void wxRadioButton::SetValue( bool val )
123 {
124 wxCHECK_RET( m_widget != NULL, _T("invalid radiobutton") );
125
126 if ( val == GetValue() )
127 return;
128
129 m_blockFirstEvent = TRUE;
130
131 if (val)
132 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget), TRUE );
133 else
134 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_theOtherRadioButtton), TRUE );
135 }
136
137 bool wxRadioButton::GetValue() const
138 {
139 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobutton") );
140
141 return GTK_TOGGLE_BUTTON(m_widget)->active;
142 }
143
144 bool wxRadioButton::Enable( bool enable )
145 {
146 if ( !wxControl::Enable( enable ) )
147 return FALSE;
148
149 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
150
151 return TRUE;
152 }
153
154 void wxRadioButton::ApplyWidgetStyle()
155 {
156 SetWidgetStyle();
157 gtk_widget_set_style( m_widget, m_widgetStyle );
158 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
159 }
160
161 #endif