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