]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/checkbox.cpp
Refactor: use wxBookCtrlBase::m_selection in all derived classes.
[wxWidgets.git] / src / gtk1 / checkbox.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk1/checkbox.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
dcf924a3
RR
13#if wxUSE_CHECKBOX
14
1e6feb95
VZ
15#include "wx/checkbox.h"
16
3cbab641 17#include "wx/gtk1/private.h"
83624f79 18
acfd422a
RR
19//-----------------------------------------------------------------------------
20// idle system
21//-----------------------------------------------------------------------------
22
23extern void wxapp_install_idle_handler();
24extern bool g_isIdle;
25
66bd6b93
RR
26//-----------------------------------------------------------------------------
27// data
28//-----------------------------------------------------------------------------
29
d7fa7eaa
RR
30extern bool g_blockEventsOnDrag;
31extern wxCursor g_globalCursor;
32extern wxWindowGTK *g_delayedFocus;
66bd6b93 33
c801d85f 34//-----------------------------------------------------------------------------
e1e955e1 35// "clicked"
c801d85f
KB
36//-----------------------------------------------------------------------------
37
865bb325 38extern "C" {
89954433
VZ
39static void gtk_checkbox_toggled_callback(GtkWidget *WXUNUSED(widget),
40 wxCheckBox *cb)
c801d85f 41{
acfd422a
RR
42 if (g_isIdle) wxapp_install_idle_handler();
43
a2053b27 44 if (!cb->m_hasVMT) return;
ff8bfdbb 45
b292e2f5 46 if (g_blockEventsOnDrag) return;
3d257b8d 47
9864c56d 48 if (cb->m_blockEvent) return;
ff8bfdbb 49
b292e2f5 50 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
4dcccda6 51 event.SetInt(cb->GetValue());
b292e2f5 52 event.SetEventObject(cb);
937013e0 53 cb->HandleWindowEvent(event);
6de97a3b 54}
865bb325 55}
c801d85f 56
e1e955e1
RR
57//-----------------------------------------------------------------------------
58// wxCheckBox
c801d85f
KB
59//-----------------------------------------------------------------------------
60
61IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
62
2cc0e28f 63wxCheckBox::wxCheckBox()
c801d85f 64{
6de97a3b 65}
c801d85f 66
2cc0e28f
VZ
67bool wxCheckBox::Create(wxWindow *parent,
68 wxWindowID id,
69 const wxString &label,
70 const wxPoint &pos,
71 const wxSize &size,
72 long style,
73 const wxValidator& validator,
74 const wxString &name )
c801d85f 75{
93763ad5
WS
76 m_needParent = true;
77 m_acceptsFocus = true;
78 m_blockEvent = false;
ff8bfdbb 79
f254e242 80 WXValidateStyle(&style);
4dcaf11a
RR
81 if (!PreCreation( parent, pos, size ) ||
82 !CreateBase( parent, id, pos, size, style, validator, name ))
83 {
223d09f6 84 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
93763ad5 85 return false;
4dcaf11a 86 }
ce4169a4 87
2cc0e28f
VZ
88 if ( style & wxALIGN_RIGHT )
89 {
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
92 // left of it
93 m_widgetCheckbox = gtk_check_button_new();
6de97a3b 94
eaafd2f8 95 m_widgetLabel = gtk_label_new("");
2cc0e28f 96 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
ff8bfdbb 97
2cc0e28f
VZ
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);
101
2cc0e28f
VZ
102 gtk_widget_show( m_widgetLabel );
103 gtk_widget_show( m_widgetCheckbox );
104 }
105 else
106 {
eaafd2f8 107 m_widgetCheckbox = gtk_check_button_new_with_label("");
9e691f46 108 m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox );
2cc0e28f
VZ
109 m_widget = m_widgetCheckbox;
110 }
eaafd2f8 111 SetLabel( label );
2cc0e28f 112
2cc0e28f 113 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
4dcccda6
VS
114 "toggled",
115 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback),
2cc0e28f 116 (gpointer *)this );
ff8bfdbb 117
f03fc89f 118 m_parent->DoAddChild( this );
ff8bfdbb 119
abdeb9e7 120 PostCreation(size);
ff8bfdbb 121
93763ad5 122 return true;
6de97a3b 123}
c801d85f 124
debe6624 125void wxCheckBox::SetValue( bool state )
c801d85f 126{
223d09f6 127 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
ff8bfdbb 128
953704c1 129 if (state == GetValue())
8bf45ed1
VZ
130 return;
131
93763ad5 132 m_blockEvent = true;
ae0bdb01 133
2cc0e28f 134 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
1db8dc4a 135
93763ad5 136 m_blockEvent = false;
6de97a3b 137}
c801d85f 138
83058c58 139bool wxCheckBox::GetValue() const
c801d85f 140{
93763ad5 141 wxCHECK_MSG( m_widgetCheckbox != NULL, false, wxT("invalid checkbox") );
f96aa4d9 142
2cc0e28f 143 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
4dcccda6 144}
4dcccda6 145
83058c58
RR
146void wxCheckBox::SetLabel( const wxString& label )
147{
223d09f6 148 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
f96aa4d9 149
b2ff89d6 150 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label);
83058c58
RR
151}
152
f03fc89f 153bool wxCheckBox::Enable( bool enable )
d3904ceb 154{
f03fc89f 155 if ( !wxControl::Enable( enable ) )
93763ad5 156 return false;
ff8bfdbb 157
2cc0e28f 158 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f 159
93763ad5 160 return true;
d3904ceb
RR
161}
162
f40fdaa3 163void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 164{
f40fdaa3
VS
165 gtk_widget_modify_style(m_widgetCheckbox, style);
166 gtk_widget_modify_style(m_widgetLabel, style);
f96aa4d9
RR
167}
168
2f073eb2
RR
169bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
170{
9e691f46 171 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
172}
173
174void wxCheckBox::OnInternalIdle()
175{
176 wxCursor cursor = m_cursor;
177 if (g_globalCursor.Ok()) cursor = g_globalCursor;
178
9e691f46
VZ
179 GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
180 if ( event_window && cursor.Ok() )
2f073eb2
RR
181 {
182 /* I now set the cursor the anew in every OnInternalIdle call
1db8dc4a
VZ
183 as setting the cursor in a parent window also effects the
184 windows above so that checking for the current cursor is
185 not possible. */
186
9e691f46 187 gdk_window_set_cursor( event_window, cursor.GetCursor() );
2f073eb2
RR
188 }
189
d7fa7eaa
RR
190 if (g_delayedFocus == this)
191 {
192 if (GTK_WIDGET_REALIZED(m_widget))
193 {
194 gtk_widget_grab_focus( m_widget );
195 g_delayedFocus = NULL;
196 }
197 }
3d257b8d 198
e39af974
JS
199 if (wxUpdateUIEvent::CanUpdate(this))
200 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2f073eb2
RR
201}
202
f68586e5
VZ
203wxSize wxCheckBox::DoGetBestSize() const
204{
db434467 205 return wxControl::DoGetBestSize();
f68586e5
VZ
206}
207
9d522606
RD
208// static
209wxVisualAttributes
210wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
211{
212 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
213}
214
dcf924a3 215#endif