]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/checkbox.cpp
removed some stray MSW code
[wxWidgets.git] / src / gtk / 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
ff8bfdbb 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "checkbox.h"
13#endif
14
1e6feb95 15#include "wx/defs.h"
c801d85f 16
dcf924a3
RR
17#if wxUSE_CHECKBOX
18
1e6feb95
VZ
19#include "wx/checkbox.h"
20
9e691f46 21#include "wx/gtk/private.h"
83624f79 22
acfd422a
RR
23//-----------------------------------------------------------------------------
24// idle system
25//-----------------------------------------------------------------------------
26
27extern void wxapp_install_idle_handler();
28extern bool g_isIdle;
29
66bd6b93
RR
30//-----------------------------------------------------------------------------
31// data
32//-----------------------------------------------------------------------------
33
d7fa7eaa
RR
34extern bool g_blockEventsOnDrag;
35extern wxCursor g_globalCursor;
36extern wxWindowGTK *g_delayedFocus;
66bd6b93 37
c801d85f 38//-----------------------------------------------------------------------------
e1e955e1 39// "clicked"
c801d85f
KB
40//-----------------------------------------------------------------------------
41
66bd6b93 42static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb )
c801d85f 43{
acfd422a
RR
44 if (g_isIdle) wxapp_install_idle_handler();
45
a2053b27 46 if (!cb->m_hasVMT) return;
ff8bfdbb 47
b292e2f5 48 if (g_blockEventsOnDrag) return;
9864c56d
RR
49
50 if (cb->m_blockEvent) return;
ff8bfdbb 51
b292e2f5
RR
52 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
53 event.SetInt( cb->GetValue() );
54 event.SetEventObject(cb);
55 cb->GetEventHandler()->ProcessEvent(event);
6de97a3b 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
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
VZ
113 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
114 "clicked",
115 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
116 (gpointer *)this );
ff8bfdbb 117
f03fc89f 118 m_parent->DoAddChild( this );
ff8bfdbb 119
b292e2f5 120 PostCreation();
ff8bfdbb 121
db434467
RR
122 SetFont( parent->GetFont() );
123
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 );
132
b292e2f5
RR
133 SetBackgroundColour( parent->GetBackgroundColour() );
134 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 135
b292e2f5 136 Show( TRUE );
ff8bfdbb 137
b292e2f5 138 return TRUE;
6de97a3b 139}
c801d85f 140
debe6624 141void wxCheckBox::SetValue( bool state )
c801d85f 142{
223d09f6 143 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
ff8bfdbb 144
953704c1 145 if (state == GetValue())
8bf45ed1
VZ
146 return;
147
9864c56d 148 m_blockEvent = TRUE;
ae0bdb01 149
2cc0e28f 150 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
1db8dc4a 151
9864c56d 152 m_blockEvent = FALSE;
6de97a3b 153}
c801d85f 154
83058c58 155bool wxCheckBox::GetValue() const
c801d85f 156{
223d09f6 157 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") );
f96aa4d9 158
2cc0e28f 159 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
6de97a3b 160}
c801d85f 161
83058c58
RR
162void wxCheckBox::SetLabel( const wxString& label )
163{
223d09f6 164 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
f96aa4d9 165
b292e2f5 166 wxControl::SetLabel( label );
ff8bfdbb 167
eaafd2f8
VS
168#ifdef __WXGTK20__
169 wxString label2 = PrepareLabelMnemonics( label );
170 gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel), wxGTK_CONV( label2 ) );
171#else
fab591c5 172 gtk_label_set( GTK_LABEL(m_widgetLabel), wxGTK_CONV( GetLabel() ) );
eaafd2f8 173#endif
83058c58
RR
174}
175
f03fc89f 176bool wxCheckBox::Enable( bool enable )
d3904ceb 177{
f03fc89f
VZ
178 if ( !wxControl::Enable( enable ) )
179 return FALSE;
ff8bfdbb 180
2cc0e28f 181 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f
VZ
182
183 return TRUE;
d3904ceb
RR
184}
185
58614078 186void wxCheckBox::ApplyWidgetStyle()
868a2826 187{
b292e2f5 188 SetWidgetStyle();
2cc0e28f
VZ
189 gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle );
190 gtk_widget_set_style( m_widgetLabel, m_widgetStyle );
f96aa4d9
RR
191}
192
2f073eb2
RR
193bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
194{
9e691f46 195 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
196}
197
198void wxCheckBox::OnInternalIdle()
199{
200 wxCursor cursor = m_cursor;
201 if (g_globalCursor.Ok()) cursor = g_globalCursor;
202
9e691f46
VZ
203 GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
204 if ( event_window && cursor.Ok() )
2f073eb2
RR
205 {
206 /* I now set the cursor the anew in every OnInternalIdle call
1db8dc4a
VZ
207 as setting the cursor in a parent window also effects the
208 windows above so that checking for the current cursor is
209 not possible. */
210
9e691f46 211 gdk_window_set_cursor( event_window, cursor.GetCursor() );
2f073eb2
RR
212 }
213
d7fa7eaa
RR
214 if (g_delayedFocus == this)
215 {
216 if (GTK_WIDGET_REALIZED(m_widget))
217 {
218 gtk_widget_grab_focus( m_widget );
219 g_delayedFocus = NULL;
220 }
221 }
222
e39af974
JS
223 if (wxUpdateUIEvent::CanUpdate(this))
224 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2f073eb2
RR
225}
226
f68586e5
VZ
227wxSize wxCheckBox::DoGetBestSize() const
228{
db434467 229 return wxControl::DoGetBestSize();
f68586e5
VZ
230}
231
dcf924a3 232#endif