]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/checkbox.cpp
1. wxStaticLine implemented (generic (ugly) and MSW versions)
[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
83624f79
RR
19#include "gdk/gdk.h"
20#include "gtk/gtk.h"
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
33extern bool g_blockEventsOnDrag;
34
c801d85f 35//-----------------------------------------------------------------------------
e1e955e1 36// "clicked"
c801d85f
KB
37//-----------------------------------------------------------------------------
38
66bd6b93 39static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb )
c801d85f 40{
acfd422a
RR
41 if (g_isIdle) wxapp_install_idle_handler();
42
a2053b27 43 if (!cb->m_hasVMT) return;
ff8bfdbb 44
b292e2f5
RR
45 if (cb->m_blockFirstEvent)
46 {
47 cb->m_blockFirstEvent = FALSE;
48 return;
ff8bfdbb
VZ
49 }
50
b292e2f5 51 if (g_blockEventsOnDrag) return;
ff8bfdbb 52
b292e2f5
RR
53 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
54 event.SetInt( cb->GetValue() );
55 event.SetEventObject(cb);
56 cb->GetEventHandler()->ProcessEvent(event);
6de97a3b 57}
c801d85f 58
e1e955e1
RR
59//-----------------------------------------------------------------------------
60// wxCheckBox
c801d85f
KB
61//-----------------------------------------------------------------------------
62
63IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
64
2cc0e28f 65wxCheckBox::wxCheckBox()
c801d85f 66{
6de97a3b 67}
c801d85f 68
2cc0e28f
VZ
69bool wxCheckBox::Create(wxWindow *parent,
70 wxWindowID id,
71 const wxString &label,
72 const wxPoint &pos,
73 const wxSize &size,
74 long style,
75 const wxValidator& validator,
76 const wxString &name )
c801d85f 77{
b292e2f5
RR
78 m_needParent = TRUE;
79 m_acceptsFocus = TRUE;
ff8bfdbb 80
b292e2f5 81 PreCreation( parent, id, pos, size, style, name );
c801d85f 82
2cc0e28f
VZ
83 m_blockFirstEvent = FALSE;
84
ce4169a4 85#if wxUSE_VALIDATORS
b292e2f5 86 SetValidator( validator );
ce4169a4
RR
87#endif
88
2cc0e28f
VZ
89 wxControl::SetLabel( label );
90
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
3bce7509 98 m_widgetLabel = gtk_label_new(m_label.mbc_str());
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
105 // VZ: why do I have to do this to make them appear?
106 gtk_widget_show( m_widgetLabel );
107 gtk_widget_show( m_widgetCheckbox );
108 }
109 else
110 {
3bce7509 111 m_widgetCheckbox = gtk_check_button_new_with_label( m_label.mbc_str() );
2cc0e28f
VZ
112 m_widgetLabel = GTK_BUTTON( m_widgetCheckbox )->child;
113 m_widget = m_widgetCheckbox;
114 }
115
116 wxSize newSize(size);
117 if (newSize.x == -1)
118 {
119 newSize.x = 25 + gdk_string_measure( m_widgetCheckbox->style->font,
3bce7509 120 m_label.mbc_str() );
2cc0e28f
VZ
121 }
122 if (newSize.y == -1)
123 newSize.y = 26;
ff8bfdbb 124
b292e2f5 125 SetSize( newSize.x, newSize.y );
ff8bfdbb 126
2cc0e28f
VZ
127 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
128 "clicked",
129 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
130 (gpointer *)this );
ff8bfdbb 131
f03fc89f 132 m_parent->DoAddChild( this );
ff8bfdbb 133
b292e2f5 134 PostCreation();
ff8bfdbb 135
b292e2f5
RR
136 SetBackgroundColour( parent->GetBackgroundColour() );
137 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 138 SetFont( parent->GetFont() );
f96aa4d9 139
b292e2f5 140 Show( TRUE );
ff8bfdbb 141
b292e2f5 142 return TRUE;
6de97a3b 143}
c801d85f 144
debe6624 145void wxCheckBox::SetValue( bool state )
c801d85f 146{
3bce7509 147 wxCHECK_RET( m_widgetCheckbox != NULL, _T("invalid checkbox") );
ff8bfdbb 148
8bf45ed1
VZ
149 if ( state == GetValue() )
150 return;
151
152 // for compatibility with wxMSW don't send notification when the check box
153 // state is changed programmatically
b292e2f5 154 m_blockFirstEvent = TRUE;
ae0bdb01 155
2cc0e28f 156 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
6de97a3b 157}
c801d85f 158
83058c58 159bool wxCheckBox::GetValue() const
c801d85f 160{
3bce7509 161 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, _T("invalid checkbox") );
f96aa4d9 162
2cc0e28f 163 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
6de97a3b 164}
c801d85f 165
83058c58
RR
166void wxCheckBox::SetLabel( const wxString& label )
167{
3bce7509 168 wxCHECK_RET( m_widgetLabel != NULL, _T("invalid checkbox") );
f96aa4d9 169
b292e2f5 170 wxControl::SetLabel( label );
ff8bfdbb 171
3bce7509 172 gtk_label_set( GTK_LABEL(m_widgetLabel), GetLabel().mbc_str() );
83058c58
RR
173}
174
f03fc89f 175bool wxCheckBox::Enable( bool enable )
d3904ceb 176{
f03fc89f
VZ
177 if ( !wxControl::Enable( enable ) )
178 return FALSE;
ff8bfdbb 179
2cc0e28f 180 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f
VZ
181
182 return TRUE;
d3904ceb
RR
183}
184
58614078 185void wxCheckBox::ApplyWidgetStyle()
868a2826 186{
b292e2f5 187 SetWidgetStyle();
2cc0e28f
VZ
188 gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle );
189 gtk_widget_set_style( m_widgetLabel, m_widgetStyle );
f96aa4d9
RR
190}
191
dcf924a3 192#endif