]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/checkbox.cpp
Optimized sizers to not call CalcMin more often than neccessary
[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
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
abdeb9e7 123 PostCreation(size);
ff8bfdbb 124
b292e2f5 125 return TRUE;
6de97a3b 126}
c801d85f 127
debe6624 128void wxCheckBox::SetValue( bool state )
c801d85f 129{
223d09f6 130 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
ff8bfdbb 131
953704c1 132 if (state == GetValue())
8bf45ed1
VZ
133 return;
134
9864c56d 135 m_blockEvent = TRUE;
ae0bdb01 136
2cc0e28f 137 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
1db8dc4a 138
9864c56d 139 m_blockEvent = FALSE;
6de97a3b 140}
c801d85f 141
83058c58 142bool wxCheckBox::GetValue() const
c801d85f 143{
223d09f6 144 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") );
f96aa4d9 145
2cc0e28f 146 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
6de97a3b 147}
c801d85f 148
83058c58
RR
149void wxCheckBox::SetLabel( const wxString& label )
150{
223d09f6 151 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
f96aa4d9 152
b292e2f5 153 wxControl::SetLabel( label );
ff8bfdbb 154
eaafd2f8
VS
155#ifdef __WXGTK20__
156 wxString label2 = PrepareLabelMnemonics( label );
157 gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel), wxGTK_CONV( label2 ) );
158#else
fab591c5 159 gtk_label_set( GTK_LABEL(m_widgetLabel), wxGTK_CONV( GetLabel() ) );
eaafd2f8 160#endif
83058c58
RR
161}
162
f03fc89f 163bool wxCheckBox::Enable( bool enable )
d3904ceb 164{
f03fc89f
VZ
165 if ( !wxControl::Enable( enable ) )
166 return FALSE;
ff8bfdbb 167
2cc0e28f 168 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f
VZ
169
170 return TRUE;
d3904ceb
RR
171}
172
f40fdaa3 173void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 174{
f40fdaa3
VS
175 gtk_widget_modify_style(m_widgetCheckbox, style);
176 gtk_widget_modify_style(m_widgetLabel, style);
f96aa4d9
RR
177}
178
2f073eb2
RR
179bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
180{
9e691f46 181 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
182}
183
184void wxCheckBox::OnInternalIdle()
185{
186 wxCursor cursor = m_cursor;
187 if (g_globalCursor.Ok()) cursor = g_globalCursor;
188
9e691f46
VZ
189 GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
190 if ( event_window && cursor.Ok() )
2f073eb2
RR
191 {
192 /* I now set the cursor the anew in every OnInternalIdle call
1db8dc4a
VZ
193 as setting the cursor in a parent window also effects the
194 windows above so that checking for the current cursor is
195 not possible. */
196
9e691f46 197 gdk_window_set_cursor( event_window, cursor.GetCursor() );
2f073eb2
RR
198 }
199
d7fa7eaa
RR
200 if (g_delayedFocus == this)
201 {
202 if (GTK_WIDGET_REALIZED(m_widget))
203 {
204 gtk_widget_grab_focus( m_widget );
205 g_delayedFocus = NULL;
206 }
207 }
208
e39af974
JS
209 if (wxUpdateUIEvent::CanUpdate(this))
210 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2f073eb2
RR
211}
212
f68586e5
VZ
213wxSize wxCheckBox::DoGetBestSize() const
214{
db434467 215 return wxControl::DoGetBestSize();
f68586e5
VZ
216}
217
9d522606
RD
218// static
219wxVisualAttributes
220wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
221{
222 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
223}
224
dcf924a3 225#endif