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