]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/checkbox.cpp
Send all menu item actions to a dedicated target. This is to ensure
[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
ff8bfdbb 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
14f355c2 11#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
12#pragma implementation "checkbox.h"
13#endif
14
14f355c2
VS
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
1e6feb95 18#include "wx/defs.h"
c801d85f 19
dcf924a3
RR
20#if wxUSE_CHECKBOX
21
1e6feb95
VZ
22#include "wx/checkbox.h"
23
9e691f46 24#include "wx/gtk/private.h"
83624f79 25
acfd422a
RR
26//-----------------------------------------------------------------------------
27// idle system
28//-----------------------------------------------------------------------------
29
30extern void wxapp_install_idle_handler();
31extern bool g_isIdle;
32
66bd6b93
RR
33//-----------------------------------------------------------------------------
34// data
35//-----------------------------------------------------------------------------
36
d7fa7eaa
RR
37extern bool g_blockEventsOnDrag;
38extern wxCursor g_globalCursor;
39extern wxWindowGTK *g_delayedFocus;
66bd6b93 40
c801d85f 41//-----------------------------------------------------------------------------
e1e955e1 42// "clicked"
c801d85f
KB
43//-----------------------------------------------------------------------------
44
66bd6b93 45static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb )
c801d85f 46{
acfd422a
RR
47 if (g_isIdle) wxapp_install_idle_handler();
48
a2053b27 49 if (!cb->m_hasVMT) return;
ff8bfdbb 50
b292e2f5 51 if (g_blockEventsOnDrag) return;
9864c56d
RR
52
53 if (cb->m_blockEvent) return;
ff8bfdbb 54
b292e2f5
RR
55 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
56 event.SetInt( cb->GetValue() );
57 event.SetEventObject(cb);
58 cb->GetEventHandler()->ProcessEvent(event);
6de97a3b 59}
c801d85f 60
e1e955e1
RR
61//-----------------------------------------------------------------------------
62// wxCheckBox
c801d85f
KB
63//-----------------------------------------------------------------------------
64
65IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
66
2cc0e28f 67wxCheckBox::wxCheckBox()
c801d85f 68{
6de97a3b 69}
c801d85f 70
2cc0e28f
VZ
71bool wxCheckBox::Create(wxWindow *parent,
72 wxWindowID id,
73 const wxString &label,
74 const wxPoint &pos,
75 const wxSize &size,
76 long style,
77 const wxValidator& validator,
78 const wxString &name )
c801d85f 79{
b292e2f5
RR
80 m_needParent = TRUE;
81 m_acceptsFocus = TRUE;
9864c56d 82 m_blockEvent = FALSE;
ff8bfdbb 83
4dcaf11a
RR
84 if (!PreCreation( parent, pos, size ) ||
85 !CreateBase( parent, id, pos, size, style, validator, name ))
86 {
223d09f6 87 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
1db8dc4a 88 return FALSE;
4dcaf11a 89 }
ce4169a4 90
2cc0e28f
VZ
91 if ( style & wxALIGN_RIGHT )
92 {
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
95 // left of it
96 m_widgetCheckbox = gtk_check_button_new();
6de97a3b 97
eaafd2f8 98 m_widgetLabel = gtk_label_new("");
2cc0e28f 99 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
ff8bfdbb 100
2cc0e28f
VZ
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);
104
2cc0e28f
VZ
105 gtk_widget_show( m_widgetLabel );
106 gtk_widget_show( m_widgetCheckbox );
107 }
108 else
109 {
eaafd2f8 110 m_widgetCheckbox = gtk_check_button_new_with_label("");
9e691f46 111 m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox );
2cc0e28f
VZ
112 m_widget = m_widgetCheckbox;
113 }
eaafd2f8 114 SetLabel( label );
2cc0e28f 115
2cc0e28f
VZ
116 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
117 "clicked",
118 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
119 (gpointer *)this );
ff8bfdbb 120
f03fc89f 121 m_parent->DoAddChild( this );
ff8bfdbb 122
b292e2f5 123 PostCreation();
ff8bfdbb 124
db434467
RR
125 SetFont( parent->GetFont() );
126
127 wxSize size_best( DoGetBestSize() );
128 wxSize new_size( size );
129 if (new_size.x == -1)
130 new_size.x = size_best.x;
131 if (new_size.y == -1)
132 new_size.y = size_best.y;
133 if ((new_size.x != size.x) || (new_size.y != size.y))
134 SetSize( new_size.x, new_size.y );
135
b292e2f5
RR
136 SetBackgroundColour( parent->GetBackgroundColour() );
137 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 138
b292e2f5 139 Show( TRUE );
ff8bfdbb 140
b292e2f5 141 return TRUE;
6de97a3b 142}
c801d85f 143
debe6624 144void wxCheckBox::SetValue( bool state )
c801d85f 145{
223d09f6 146 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
ff8bfdbb 147
953704c1 148 if (state == GetValue())
8bf45ed1
VZ
149 return;
150
9864c56d 151 m_blockEvent = TRUE;
ae0bdb01 152
2cc0e28f 153 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
1db8dc4a 154
9864c56d 155 m_blockEvent = FALSE;
6de97a3b 156}
c801d85f 157
83058c58 158bool wxCheckBox::GetValue() const
c801d85f 159{
223d09f6 160 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") );
f96aa4d9 161
2cc0e28f 162 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
6de97a3b 163}
c801d85f 164
83058c58
RR
165void wxCheckBox::SetLabel( const wxString& label )
166{
223d09f6 167 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
f96aa4d9 168
b292e2f5 169 wxControl::SetLabel( label );
ff8bfdbb 170
eaafd2f8
VS
171#ifdef __WXGTK20__
172 wxString label2 = PrepareLabelMnemonics( label );
173 gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel), wxGTK_CONV( label2 ) );
174#else
fab591c5 175 gtk_label_set( GTK_LABEL(m_widgetLabel), wxGTK_CONV( GetLabel() ) );
eaafd2f8 176#endif
83058c58
RR
177}
178
f03fc89f 179bool wxCheckBox::Enable( bool enable )
d3904ceb 180{
f03fc89f
VZ
181 if ( !wxControl::Enable( enable ) )
182 return FALSE;
ff8bfdbb 183
2cc0e28f 184 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f
VZ
185
186 return TRUE;
d3904ceb
RR
187}
188
58614078 189void wxCheckBox::ApplyWidgetStyle()
868a2826 190{
b292e2f5 191 SetWidgetStyle();
2cc0e28f
VZ
192 gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle );
193 gtk_widget_set_style( m_widgetLabel, m_widgetStyle );
f96aa4d9
RR
194}
195
2f073eb2
RR
196bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
197{
9e691f46 198 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
199}
200
201void wxCheckBox::OnInternalIdle()
202{
203 wxCursor cursor = m_cursor;
204 if (g_globalCursor.Ok()) cursor = g_globalCursor;
205
9e691f46
VZ
206 GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
207 if ( event_window && cursor.Ok() )
2f073eb2
RR
208 {
209 /* I now set the cursor the anew in every OnInternalIdle call
1db8dc4a
VZ
210 as setting the cursor in a parent window also effects the
211 windows above so that checking for the current cursor is
212 not possible. */
213
9e691f46 214 gdk_window_set_cursor( event_window, cursor.GetCursor() );
2f073eb2
RR
215 }
216
d7fa7eaa
RR
217 if (g_delayedFocus == this)
218 {
219 if (GTK_WIDGET_REALIZED(m_widget))
220 {
221 gtk_widget_grab_focus( m_widget );
222 g_delayedFocus = NULL;
223 }
224 }
225
e39af974
JS
226 if (wxUpdateUIEvent::CanUpdate(this))
227 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2f073eb2
RR
228}
229
f68586e5
VZ
230wxSize wxCheckBox::DoGetBestSize() const
231{
db434467 232 return wxControl::DoGetBestSize();
f68586e5
VZ
233}
234
dcf924a3 235#endif