]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/checkbox.cpp
Explicit casting/instantiation to resolve ambiguous overload.
[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
11#ifdef __GNUG__
12#pragma implementation "checkbox.h"
13#endif
14
15#include "wx/checkbox.h"
16
83624f79
RR
17#include "gdk/gdk.h"
18#include "gtk/gtk.h"
19
acfd422a
RR
20//-----------------------------------------------------------------------------
21// idle system
22//-----------------------------------------------------------------------------
23
24extern void wxapp_install_idle_handler();
25extern bool g_isIdle;
26
66bd6b93
RR
27//-----------------------------------------------------------------------------
28// data
29//-----------------------------------------------------------------------------
30
31extern bool g_blockEventsOnDrag;
32
c801d85f 33//-----------------------------------------------------------------------------
e1e955e1 34// "clicked"
c801d85f
KB
35//-----------------------------------------------------------------------------
36
66bd6b93 37static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb )
c801d85f 38{
acfd422a
RR
39 if (g_isIdle) wxapp_install_idle_handler();
40
a2053b27 41 if (!cb->m_hasVMT) return;
ff8bfdbb 42
b292e2f5
RR
43 if (cb->m_blockFirstEvent)
44 {
45 cb->m_blockFirstEvent = FALSE;
46 return;
ff8bfdbb
VZ
47 }
48
b292e2f5 49 if (g_blockEventsOnDrag) return;
ff8bfdbb 50
b292e2f5
RR
51 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
52 event.SetInt( cb->GetValue() );
53 event.SetEventObject(cb);
54 cb->GetEventHandler()->ProcessEvent(event);
6de97a3b 55}
c801d85f 56
e1e955e1
RR
57//-----------------------------------------------------------------------------
58// wxCheckBox
c801d85f
KB
59//-----------------------------------------------------------------------------
60
61IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
62
2cc0e28f 63wxCheckBox::wxCheckBox()
c801d85f 64{
6de97a3b 65}
c801d85f 66
2cc0e28f
VZ
67bool wxCheckBox::Create(wxWindow *parent,
68 wxWindowID id,
69 const wxString &label,
70 const wxPoint &pos,
71 const wxSize &size,
72 long style,
73 const wxValidator& validator,
74 const wxString &name )
c801d85f 75{
b292e2f5
RR
76 m_needParent = TRUE;
77 m_acceptsFocus = TRUE;
ff8bfdbb 78
b292e2f5 79 PreCreation( parent, id, pos, size, style, name );
c801d85f 80
2cc0e28f
VZ
81 m_blockFirstEvent = FALSE;
82
b292e2f5 83 SetValidator( validator );
2cc0e28f
VZ
84 wxControl::SetLabel( label );
85
86 if ( style & wxALIGN_RIGHT )
87 {
88 // VZ: as I don't know a way to create a right aligned checkbox with
89 // GTK we will create a checkbox without label and a label at the
90 // left of it
91 m_widgetCheckbox = gtk_check_button_new();
6de97a3b 92
3bce7509 93 m_widgetLabel = gtk_label_new(m_label.mbc_str());
2cc0e28f 94 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
ff8bfdbb 95
2cc0e28f
VZ
96 m_widget = gtk_hbox_new(FALSE, 0);
97 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
98 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
99
100 // VZ: why do I have to do this to make them appear?
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
111 wxSize newSize(size);
112 if (newSize.x == -1)
113 {
114 newSize.x = 25 + gdk_string_measure( m_widgetCheckbox->style->font,
3bce7509 115 m_label.mbc_str() );
2cc0e28f
VZ
116 }
117 if (newSize.y == -1)
118 newSize.y = 26;
ff8bfdbb 119
b292e2f5 120 SetSize( newSize.x, newSize.y );
ff8bfdbb 121
2cc0e28f
VZ
122 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
123 "clicked",
124 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
125 (gpointer *)this );
ff8bfdbb 126
f03fc89f 127 m_parent->DoAddChild( this );
ff8bfdbb 128
b292e2f5 129 PostCreation();
ff8bfdbb 130
b292e2f5
RR
131 SetBackgroundColour( parent->GetBackgroundColour() );
132 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 133 SetFont( parent->GetFont() );
f96aa4d9 134
b292e2f5 135 Show( TRUE );
ff8bfdbb 136
b292e2f5 137 return TRUE;
6de97a3b 138}
c801d85f 139
debe6624 140void wxCheckBox::SetValue( bool state )
c801d85f 141{
3bce7509 142 wxCHECK_RET( m_widgetCheckbox != NULL, _T("invalid checkbox") );
ff8bfdbb 143
8bf45ed1
VZ
144 if ( state == GetValue() )
145 return;
146
147 // for compatibility with wxMSW don't send notification when the check box
148 // state is changed programmatically
b292e2f5 149 m_blockFirstEvent = TRUE;
ae0bdb01 150
2cc0e28f 151 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
6de97a3b 152}
c801d85f 153
83058c58 154bool wxCheckBox::GetValue() const
c801d85f 155{
3bce7509 156 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, _T("invalid checkbox") );
f96aa4d9 157
2cc0e28f 158 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
6de97a3b 159}
c801d85f 160
83058c58
RR
161void wxCheckBox::SetLabel( const wxString& label )
162{
3bce7509 163 wxCHECK_RET( m_widgetLabel != NULL, _T("invalid checkbox") );
f96aa4d9 164
b292e2f5 165 wxControl::SetLabel( label );
ff8bfdbb 166
3bce7509 167 gtk_label_set( GTK_LABEL(m_widgetLabel), GetLabel().mbc_str() );
83058c58
RR
168}
169
f03fc89f 170bool wxCheckBox::Enable( bool enable )
d3904ceb 171{
f03fc89f
VZ
172 if ( !wxControl::Enable( enable ) )
173 return FALSE;
ff8bfdbb 174
2cc0e28f 175 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f
VZ
176
177 return TRUE;
d3904ceb
RR
178}
179
58614078 180void wxCheckBox::ApplyWidgetStyle()
868a2826 181{
b292e2f5 182 SetWidgetStyle();
2cc0e28f
VZ
183 gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle );
184 gtk_widget_set_style( m_widgetLabel, m_widgetStyle );
f96aa4d9
RR
185}
186