]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/radiobut.cpp
removed 2nd frame from html test sample; test sample now shows DL,DT,DD support
[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
3f26799e
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
2f073eb2
RR
33extern bool g_blockEventsOnDrag;
34extern wxCursor g_globalCursor;
6de97a3b
RR
35
36//-----------------------------------------------------------------------------
bb4549de 37// "clicked"
6de97a3b
RR
38//-----------------------------------------------------------------------------
39
bb4549de
RR
40static
41void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioButton *rb )
6de97a3b 42{
acfd422a
RR
43 if (g_isIdle) wxapp_install_idle_handler();
44
a2053b27 45 if (!rb->m_hasVMT) return;
bb4549de 46
f5d29b39 47 if (g_blockEventsOnDrag) return;
6de97a3b 48
f5d29b39
RR
49 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
50 event.SetInt( rb->GetValue() );
51 event.SetEventObject( rb );
52 rb->GetEventHandler()->ProcessEvent( event );
6de97a3b
RR
53}
54
bb4549de
RR
55//-----------------------------------------------------------------------------
56// wxRadioButton
57//-----------------------------------------------------------------------------
58
59IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
60
6de97a3b
RR
61bool wxRadioButton::Create( wxWindow *parent, wxWindowID id, const wxString& label,
62 const wxPoint& pos, const wxSize& size, long style,
63 const wxValidator& validator, const wxString& name )
64{
b292e2f5 65 m_acceptsFocus = TRUE;
f5d29b39 66 m_needParent = TRUE;
953704c1 67 m_isRadioButton = TRUE;
6de97a3b 68
4dcaf11a
RR
69 if (!PreCreation( parent, pos, size ) ||
70 !CreateBase( parent, id, pos, size, style, validator, name ))
71 {
223d09f6 72 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
4dcaf11a
RR
73 return FALSE;
74 }
953704c1
RR
75
76 if (HasFlag(wxRB_GROUP))
77 {
78 /* start a new group */
79 m_radioButtonGroup = (GSList*) NULL;
80 }
81 else
82 {
83 /* search backward for last group start */
84 wxRadioButton *chief = (wxRadioButton*) NULL;
85 wxWindowList::Node *node = parent->GetChildren().GetLast();
86 while (node)
87 {
88 wxWindow *child = node->GetData();
89 if (child->m_isRadioButton)
90 {
91 chief = (wxRadioButton*) child;
92 if (child->HasFlag(wxRB_GROUP)) break;
93 }
953704c1
RR
94 node = node->GetPrevious();
95 }
0544bc0a
RR
96 if (chief)
97 {
98 /* we are part of the group started by chief */
99 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
100 }
101 else
102 {
103 /* start a new group */
104 m_radioButtonGroup = (GSList*) NULL;
105 }
953704c1
RR
106 }
107
108 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, label.mbc_str() );
6de97a3b 109
f5d29b39 110 SetLabel(label);
6de97a3b 111
4dcaf11a 112 wxSize newSize = size;
b019151f 113 if (newSize.x == -1) newSize.x = 22+gdk_string_measure( m_widget->style->font, label.mbc_str() );
f5d29b39
RR
114 if (newSize.y == -1) newSize.y = 26;
115 SetSize( newSize.x, newSize.y );
6de97a3b 116
f5d29b39
RR
117 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
118 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
6de97a3b 119
f03fc89f 120 m_parent->DoAddChild( this );
6ca41e57 121
f5d29b39 122 PostCreation();
6de97a3b 123
f5d29b39
RR
124 SetBackgroundColour( parent->GetBackgroundColour() );
125 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 126 SetFont( parent->GetFont() );
58614078 127
f5d29b39 128 Show( TRUE );
6de97a3b 129
f5d29b39 130 return TRUE;
6de97a3b
RR
131}
132
133void wxRadioButton::SetLabel( const wxString& label )
134{
223d09f6 135 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f96aa4d9 136
f5d29b39
RR
137 wxControl::SetLabel( label );
138 GtkButton *bin = GTK_BUTTON( m_widget );
139 GtkLabel *g_label = GTK_LABEL( bin->child );
b019151f 140 gtk_label_set( g_label, GetLabel().mbc_str() );
6de97a3b
RR
141}
142
143void wxRadioButton::SetValue( bool val )
144{
223d09f6 145 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f96aa4d9 146
953704c1 147 if (val == GetValue())
0659e7ee
RR
148 return;
149
953704c1
RR
150 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
151 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
152
f5d29b39 153 if (val)
953704c1 154 {
f5d29b39 155 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 156 }
f5d29b39 157 else
953704c1
RR
158 {
159 // should give an assert
160 }
161
162 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
163 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
6de97a3b
RR
164}
165
eb082a08 166bool wxRadioButton::GetValue() const
6de97a3b 167{
223d09f6 168 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
f96aa4d9 169
f5d29b39 170 return GTK_TOGGLE_BUTTON(m_widget)->active;
6de97a3b
RR
171}
172
f03fc89f 173bool wxRadioButton::Enable( bool enable )
d3904ceb 174{
f03fc89f
VZ
175 if ( !wxControl::Enable( enable ) )
176 return FALSE;
f96aa4d9 177
f5d29b39 178 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
f03fc89f
VZ
179
180 return TRUE;
d3904ceb
RR
181}
182
58614078 183void wxRadioButton::ApplyWidgetStyle()
868a2826 184{
f5d29b39
RR
185 SetWidgetStyle();
186 gtk_widget_set_style( m_widget, m_widgetStyle );
187 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
f96aa4d9 188}
dcf924a3 189
2f073eb2
RR
190bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
191{
192 return (window == GTK_TOGGLE_BUTTON(m_widget)->event_window);
193}
194
195void wxRadioButton::OnInternalIdle()
196{
197 wxCursor cursor = m_cursor;
198 if (g_globalCursor.Ok()) cursor = g_globalCursor;
199
200 if (GTK_TOGGLE_BUTTON(m_widget)->event_window && cursor.Ok())
201 {
202 /* I now set the cursor the anew in every OnInternalIdle call
203 as setting the cursor in a parent window also effects the
204 windows above so that checking for the current cursor is
205 not possible. */
206
207 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widget)->event_window, cursor.GetCursor() );
208 }
209
210 UpdateWindowUI();
211}
212
dcf924a3 213#endif