1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "checkbox.h"
15 #include "wx/checkbox.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
34 extern wxCursor g_globalCursor
;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 static void gtk_checkbox_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxCheckBox
*cb
)
42 if (g_isIdle
) wxapp_install_idle_handler();
44 if (!cb
->m_hasVMT
) return;
46 if (g_blockEventsOnDrag
) return;
48 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
49 event
.SetInt( cb
->GetValue() );
50 event
.SetEventObject(cb
);
51 cb
->GetEventHandler()->ProcessEvent(event
);
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
60 wxCheckBox::wxCheckBox()
64 bool wxCheckBox::Create(wxWindow
*parent
,
66 const wxString
&label
,
70 const wxValidator
& validator
,
71 const wxString
&name
)
74 m_acceptsFocus
= TRUE
;
76 if (!PreCreation( parent
, pos
, size
) ||
77 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
79 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
83 wxControl::SetLabel( label
);
85 if ( style
& wxALIGN_RIGHT
)
87 // VZ: as I don't know a way to create a right aligned checkbox with
88 // GTK we will create a checkbox without label and a label at the
90 m_widgetCheckbox
= gtk_check_button_new();
92 m_widgetLabel
= gtk_label_new(m_label
.mbc_str());
93 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
95 m_widget
= gtk_hbox_new(FALSE
, 0);
96 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
97 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
99 gtk_widget_show( m_widgetLabel
);
100 gtk_widget_show( m_widgetCheckbox
);
104 m_widgetCheckbox
= gtk_check_button_new_with_label( m_label
.mbc_str() );
105 m_widgetLabel
= GTK_BUTTON( m_widgetCheckbox
)->child
;
106 m_widget
= m_widgetCheckbox
;
109 SetSizeOrDefault( size
);
111 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
113 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback
),
116 m_parent
->DoAddChild( this );
120 SetBackgroundColour( parent
->GetBackgroundColour() );
121 SetForegroundColour( parent
->GetForegroundColour() );
122 SetFont( parent
->GetFont() );
129 void wxCheckBox::SetValue( bool state
)
131 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
133 if (state
== GetValue())
136 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widgetCheckbox
),
137 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback
),
140 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
142 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
144 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback
),
148 bool wxCheckBox::GetValue() const
150 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, FALSE
, wxT("invalid checkbox") );
152 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
155 void wxCheckBox::SetLabel( const wxString
& label
)
157 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
159 wxControl::SetLabel( label
);
161 gtk_label_set( GTK_LABEL(m_widgetLabel
), GetLabel().mbc_str() );
164 bool wxCheckBox::Enable( bool enable
)
166 if ( !wxControl::Enable( enable
) )
169 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
174 void wxCheckBox::ApplyWidgetStyle()
177 gtk_widget_set_style( m_widgetCheckbox
, m_widgetStyle
);
178 gtk_widget_set_style( m_widgetLabel
, m_widgetStyle
);
181 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
183 return (window
== GTK_TOGGLE_BUTTON(m_widget
)->event_window
);
186 void wxCheckBox::OnInternalIdle()
188 wxCursor cursor
= m_cursor
;
189 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
191 if (GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->event_window
&& cursor
.Ok())
193 /* I now set the cursor the anew in every OnInternalIdle call
194 as setting the cursor in a parent window also effects the
195 windows above so that checking for the current cursor is
198 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->event_window
, cursor
.GetCursor() );
204 wxSize
wxCheckBox::DoGetBestSize() const
206 return wxSize( 25 + gdk_string_measure( m_widgetCheckbox
->style
->font
,
207 m_label
.mbc_str() ), 26 );