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