]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/checkbox.cpp
A little clarification
[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
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
1169a919
JS
65wxCheckBoxBase::wxCheckBoxBase()
66{
67}
68
c801d85f
KB
69IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
70
2cc0e28f 71wxCheckBox::wxCheckBox()
c801d85f 72{
6de97a3b 73}
c801d85f 74
2cc0e28f
VZ
75bool wxCheckBox::Create(wxWindow *parent,
76 wxWindowID id,
77 const wxString &label,
78 const wxPoint &pos,
79 const wxSize &size,
80 long style,
81 const wxValidator& validator,
82 const wxString &name )
c801d85f 83{
b292e2f5
RR
84 m_needParent = TRUE;
85 m_acceptsFocus = TRUE;
9864c56d 86 m_blockEvent = FALSE;
ff8bfdbb 87
4dcaf11a
RR
88 if (!PreCreation( parent, pos, size ) ||
89 !CreateBase( parent, id, pos, size, style, validator, name ))
90 {
223d09f6 91 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
1db8dc4a 92 return FALSE;
4dcaf11a 93 }
ce4169a4 94
2cc0e28f
VZ
95 if ( style & wxALIGN_RIGHT )
96 {
97 // VZ: as I don't know a way to create a right aligned checkbox with
98 // GTK we will create a checkbox without label and a label at the
99 // left of it
100 m_widgetCheckbox = gtk_check_button_new();
6de97a3b 101
eaafd2f8 102 m_widgetLabel = gtk_label_new("");
2cc0e28f 103 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
ff8bfdbb 104
2cc0e28f
VZ
105 m_widget = gtk_hbox_new(FALSE, 0);
106 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
107 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
108
2cc0e28f
VZ
109 gtk_widget_show( m_widgetLabel );
110 gtk_widget_show( m_widgetCheckbox );
111 }
112 else
113 {
eaafd2f8 114 m_widgetCheckbox = gtk_check_button_new_with_label("");
9e691f46 115 m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox );
2cc0e28f
VZ
116 m_widget = m_widgetCheckbox;
117 }
eaafd2f8 118 SetLabel( label );
2cc0e28f 119
2cc0e28f
VZ
120 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
121 "clicked",
122 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
123 (gpointer *)this );
ff8bfdbb 124
f03fc89f 125 m_parent->DoAddChild( this );
ff8bfdbb 126
b292e2f5 127 PostCreation();
e8e24dfa 128 InheritAttributes();
db434467
RR
129
130 wxSize size_best( DoGetBestSize() );
131 wxSize new_size( size );
132 if (new_size.x == -1)
133 new_size.x = size_best.x;
134 if (new_size.y == -1)
135 new_size.y = size_best.y;
136 if ((new_size.x != size.x) || (new_size.y != size.y))
137 SetSize( new_size.x, new_size.y );
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