1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "checkbox.h"
19 #include "wx/checkbox.h"
21 #include "wx/gtk/private.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 extern void wxapp_install_idle_handler();
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern bool g_blockEventsOnDrag
;
35 extern wxCursor g_globalCursor
;
36 extern wxWindowGTK
*g_delayedFocus
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 static void gtk_checkbox_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxCheckBox
*cb
)
44 if (g_isIdle
) wxapp_install_idle_handler();
46 if (!cb
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 if (cb
->m_blockEvent
) return;
52 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
53 event
.SetInt( cb
->GetValue() );
54 event
.SetEventObject(cb
);
55 cb
->GetEventHandler()->ProcessEvent(event
);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
64 wxCheckBox::wxCheckBox()
68 bool wxCheckBox::Create(wxWindow
*parent
,
70 const wxString
&label
,
74 const wxValidator
& validator
,
75 const wxString
&name
)
78 m_acceptsFocus
= TRUE
;
81 if (!PreCreation( parent
, pos
, size
) ||
82 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
84 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
88 if ( style
& wxALIGN_RIGHT
)
90 // VZ: as I don't know a way to create a right aligned checkbox with
91 // GTK we will create a checkbox without label and a label at the
93 m_widgetCheckbox
= gtk_check_button_new();
95 m_widgetLabel
= gtk_label_new("");
96 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
98 m_widget
= gtk_hbox_new(FALSE
, 0);
99 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
100 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
102 gtk_widget_show( m_widgetLabel
);
103 gtk_widget_show( m_widgetCheckbox
);
107 m_widgetCheckbox
= gtk_check_button_new_with_label("");
108 m_widgetLabel
= BUTTON_CHILD( m_widgetCheckbox
);
109 m_widget
= m_widgetCheckbox
;
113 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
115 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback
),
118 m_parent
->DoAddChild( this );
122 SetFont( parent
->GetFont() );
124 wxSize
size_best( DoGetBestSize() );
125 wxSize
new_size( size
);
126 if (new_size
.x
== -1)
127 new_size
.x
= size_best
.x
;
128 if (new_size
.y
== -1)
129 new_size
.y
= size_best
.y
;
130 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
131 SetSize( new_size
.x
, new_size
.y
);
133 SetBackgroundColour( parent
->GetBackgroundColour() );
134 SetForegroundColour( parent
->GetForegroundColour() );
141 void wxCheckBox::SetValue( bool state
)
143 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
145 if (state
== GetValue())
150 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
152 m_blockEvent
= FALSE
;
155 bool wxCheckBox::GetValue() const
157 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, FALSE
, wxT("invalid checkbox") );
159 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
162 void wxCheckBox::SetLabel( const wxString
& label
)
164 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
166 wxControl::SetLabel( label
);
169 wxString label2
= PrepareLabelMnemonics( label
);
170 gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel
), wxGTK_CONV( label2
) );
172 gtk_label_set( GTK_LABEL(m_widgetLabel
), wxGTK_CONV( GetLabel() ) );
176 bool wxCheckBox::Enable( bool enable
)
178 if ( !wxControl::Enable( enable
) )
181 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
186 void wxCheckBox::ApplyWidgetStyle()
189 gtk_widget_set_style( m_widgetCheckbox
, m_widgetStyle
);
190 gtk_widget_set_style( m_widgetLabel
, m_widgetStyle
);
193 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
195 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
198 void wxCheckBox::OnInternalIdle()
200 wxCursor cursor
= m_cursor
;
201 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
203 GdkWindow
*event_window
= TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox
);
204 if ( event_window
&& cursor
.Ok() )
206 /* I now set the cursor the anew in every OnInternalIdle call
207 as setting the cursor in a parent window also effects the
208 windows above so that checking for the current cursor is
211 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
214 if (g_delayedFocus
== this)
216 if (GTK_WIDGET_REALIZED(m_widget
))
218 gtk_widget_grab_focus( m_widget
);
219 g_delayedFocus
= NULL
;
223 if (wxUpdateUIEvent::CanUpdate(this))
224 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
227 wxSize
wxCheckBox::DoGetBestSize() const
229 return wxControl::DoGetBestSize();