1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "checkbox.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/checkbox.h"
24 #include "wx/gtk/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
38 extern wxCursor g_globalCursor
;
39 extern wxWindowGTK
*g_delayedFocus
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static void gtk_checkbox_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxCheckBox
*cb
)
47 if (g_isIdle
) wxapp_install_idle_handler();
49 if (!cb
->m_hasVMT
) return;
51 if (g_blockEventsOnDrag
) return;
53 if (cb
->m_blockEvent
) return;
55 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
56 event
.SetInt( cb
->GetValue() );
57 event
.SetEventObject(cb
);
58 cb
->GetEventHandler()->ProcessEvent(event
);
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
67 wxCheckBox::wxCheckBox()
71 bool wxCheckBox::Create(wxWindow
*parent
,
73 const wxString
&label
,
77 const wxValidator
& validator
,
78 const wxString
&name
)
81 m_acceptsFocus
= TRUE
;
84 if (!PreCreation( parent
, pos
, size
) ||
85 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
87 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
91 if ( style
& wxALIGN_RIGHT
)
93 // VZ: as I don't know a way to create a right aligned checkbox with
94 // GTK we will create a checkbox without label and a label at the
96 m_widgetCheckbox
= gtk_check_button_new();
98 m_widgetLabel
= gtk_label_new("");
99 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
101 m_widget
= gtk_hbox_new(FALSE
, 0);
102 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
103 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
105 gtk_widget_show( m_widgetLabel
);
106 gtk_widget_show( m_widgetCheckbox
);
110 m_widgetCheckbox
= gtk_check_button_new_with_label("");
111 m_widgetLabel
= BUTTON_CHILD( m_widgetCheckbox
);
112 m_widget
= m_widgetCheckbox
;
116 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
118 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback
),
121 m_parent
->DoAddChild( this );
126 wxSize
size_best( DoGetBestSize() );
127 wxSize
new_size( size
);
128 if (new_size
.x
== -1)
129 new_size
.x
= size_best
.x
;
130 if (new_size
.y
== -1)
131 new_size
.y
= size_best
.y
;
132 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
133 SetSize( new_size
.x
, new_size
.y
);
140 void wxCheckBox::SetValue( bool state
)
142 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
144 if (state
== GetValue())
149 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
151 m_blockEvent
= FALSE
;
154 bool wxCheckBox::GetValue() const
156 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, FALSE
, wxT("invalid checkbox") );
158 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
161 void wxCheckBox::SetLabel( const wxString
& label
)
163 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
165 wxControl::SetLabel( label
);
168 wxString label2
= PrepareLabelMnemonics( label
);
169 gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel
), wxGTK_CONV( label2
) );
171 gtk_label_set( GTK_LABEL(m_widgetLabel
), wxGTK_CONV( GetLabel() ) );
175 bool wxCheckBox::Enable( bool enable
)
177 if ( !wxControl::Enable( enable
) )
180 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
185 void wxCheckBox::ApplyWidgetStyle()
188 gtk_widget_set_style( m_widgetCheckbox
, m_widgetStyle
);
189 gtk_widget_set_style( m_widgetLabel
, m_widgetStyle
);
192 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
194 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
197 void wxCheckBox::OnInternalIdle()
199 wxCursor cursor
= m_cursor
;
200 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
202 GdkWindow
*event_window
= TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox
);
203 if ( event_window
&& cursor
.Ok() )
205 /* I now set the cursor the anew in every OnInternalIdle call
206 as setting the cursor in a parent window also effects the
207 windows above so that checking for the current cursor is
210 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
213 if (g_delayedFocus
== this)
215 if (GTK_WIDGET_REALIZED(m_widget
))
217 gtk_widget_grab_focus( m_widget
);
218 g_delayedFocus
= NULL
;
222 if (wxUpdateUIEvent::CanUpdate(this))
223 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
226 wxSize
wxCheckBox::DoGetBestSize() const
228 return wxControl::DoGetBestSize();