]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/checkbox.cpp
fixed typos in wxBase template and project file
[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
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
20e05ffb
RR
21#include <gdk/gdk.h>
22#include <gtk/gtk.h>
83624f79 23
acfd422a
RR
24//-----------------------------------------------------------------------------
25// idle system
26//-----------------------------------------------------------------------------
27
28extern void wxapp_install_idle_handler();
29extern bool g_isIdle;
30
66bd6b93
RR
31//-----------------------------------------------------------------------------
32// data
33//-----------------------------------------------------------------------------
34
2f073eb2
RR
35extern bool g_blockEventsOnDrag;
36extern wxCursor g_globalCursor;
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;
ff8bfdbb 49
b292e2f5
RR
50 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
51 event.SetInt( cb->GetValue() );
52 event.SetEventObject(cb);
53 cb->GetEventHandler()->ProcessEvent(event);
6de97a3b 54}
c801d85f 55
e1e955e1
RR
56//-----------------------------------------------------------------------------
57// wxCheckBox
c801d85f
KB
58//-----------------------------------------------------------------------------
59
60IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
61
2cc0e28f 62wxCheckBox::wxCheckBox()
c801d85f 63{
6de97a3b 64}
c801d85f 65
2cc0e28f
VZ
66bool wxCheckBox::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxString &label,
69 const wxPoint &pos,
70 const wxSize &size,
71 long style,
72 const wxValidator& validator,
73 const wxString &name )
c801d85f 74{
b292e2f5
RR
75 m_needParent = TRUE;
76 m_acceptsFocus = TRUE;
ff8bfdbb 77
4dcaf11a
RR
78 if (!PreCreation( parent, pos, size ) ||
79 !CreateBase( parent, id, pos, size, style, validator, name ))
80 {
223d09f6 81 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
1db8dc4a 82 return FALSE;
4dcaf11a 83 }
ce4169a4 84
2cc0e28f
VZ
85 wxControl::SetLabel( label );
86
87 if ( style & wxALIGN_RIGHT )
88 {
89 // VZ: as I don't know a way to create a right aligned checkbox with
90 // GTK we will create a checkbox without label and a label at the
91 // left of it
92 m_widgetCheckbox = gtk_check_button_new();
6de97a3b 93
3bce7509 94 m_widgetLabel = gtk_label_new(m_label.mbc_str());
2cc0e28f 95 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
ff8bfdbb 96
2cc0e28f
VZ
97 m_widget = gtk_hbox_new(FALSE, 0);
98 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
99 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
100
2cc0e28f
VZ
101 gtk_widget_show( m_widgetLabel );
102 gtk_widget_show( m_widgetCheckbox );
103 }
104 else
105 {
3bce7509 106 m_widgetCheckbox = gtk_check_button_new_with_label( m_label.mbc_str() );
2cc0e28f
VZ
107 m_widgetLabel = GTK_BUTTON( m_widgetCheckbox )->child;
108 m_widget = m_widgetCheckbox;
109 }
110
2cc0e28f
VZ
111 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
112 "clicked",
113 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
114 (gpointer *)this );
ff8bfdbb 115
f03fc89f 116 m_parent->DoAddChild( this );
ff8bfdbb 117
b292e2f5 118 PostCreation();
ff8bfdbb 119
db434467
RR
120 SetFont( parent->GetFont() );
121
122 wxSize size_best( DoGetBestSize() );
123 wxSize new_size( size );
124 if (new_size.x == -1)
125 new_size.x = size_best.x;
126 if (new_size.y == -1)
127 new_size.y = size_best.y;
128 if ((new_size.x != size.x) || (new_size.y != size.y))
129 SetSize( new_size.x, new_size.y );
130
b292e2f5
RR
131 SetBackgroundColour( parent->GetBackgroundColour() );
132 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 133
b292e2f5 134 Show( TRUE );
ff8bfdbb 135
b292e2f5 136 return TRUE;
6de97a3b 137}
c801d85f 138
debe6624 139void wxCheckBox::SetValue( bool state )
c801d85f 140{
223d09f6 141 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
ff8bfdbb 142
953704c1 143 if (state == GetValue())
8bf45ed1
VZ
144 return;
145
953704c1
RR
146 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widgetCheckbox),
147 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
148 (gpointer *)this );
ae0bdb01 149
2cc0e28f 150 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
1db8dc4a 151
953704c1
RR
152 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
153 "clicked",
154 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
155 (gpointer *)this );
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
3bce7509 171 gtk_label_set( GTK_LABEL(m_widgetLabel), GetLabel().mbc_str() );
83058c58
RR
172}
173
f03fc89f 174bool wxCheckBox::Enable( bool enable )
d3904ceb 175{
f03fc89f
VZ
176 if ( !wxControl::Enable( enable ) )
177 return FALSE;
ff8bfdbb 178
2cc0e28f 179 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f
VZ
180
181 return TRUE;
d3904ceb
RR
182}
183
58614078 184void wxCheckBox::ApplyWidgetStyle()
868a2826 185{
b292e2f5 186 SetWidgetStyle();
2cc0e28f
VZ
187 gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle );
188 gtk_widget_set_style( m_widgetLabel, m_widgetStyle );
f96aa4d9
RR
189}
190
2f073eb2
RR
191bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
192{
193 return (window == GTK_TOGGLE_BUTTON(m_widget)->event_window);
194}
195
196void wxCheckBox::OnInternalIdle()
197{
198 wxCursor cursor = m_cursor;
199 if (g_globalCursor.Ok()) cursor = g_globalCursor;
200
173348db 201 if (GTK_TOGGLE_BUTTON(m_widgetCheckbox)->event_window && cursor.Ok())
2f073eb2
RR
202 {
203 /* I now set the cursor the anew in every OnInternalIdle call
1db8dc4a
VZ
204 as setting the cursor in a parent window also effects the
205 windows above so that checking for the current cursor is
206 not possible. */
207
208 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widgetCheckbox)->event_window, cursor.GetCursor() );
2f073eb2
RR
209 }
210
211 UpdateWindowUI();
212}
213
f68586e5
VZ
214wxSize wxCheckBox::DoGetBestSize() const
215{
db434467 216 return wxControl::DoGetBestSize();
f68586e5
VZ
217}
218
dcf924a3 219#endif