]>
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 | ||
14f355c2 | 11 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c801d85f KB |
12 | #pragma implementation "checkbox.h" |
13 | #endif | |
14 | ||
14f355c2 VS |
15 | // For compilers that support precompilation, includes "wx.h". |
16 | #include "wx/wxprec.h" | |
17 | ||
1e6feb95 | 18 | #include "wx/defs.h" |
c801d85f | 19 | |
dcf924a3 RR |
20 | #if wxUSE_CHECKBOX |
21 | ||
1e6feb95 VZ |
22 | #include "wx/checkbox.h" |
23 | ||
9e691f46 | 24 | #include "wx/gtk/private.h" |
83624f79 | 25 | |
acfd422a RR |
26 | //----------------------------------------------------------------------------- |
27 | // idle system | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | extern void wxapp_install_idle_handler(); | |
31 | extern bool g_isIdle; | |
32 | ||
66bd6b93 RR |
33 | //----------------------------------------------------------------------------- |
34 | // data | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
d7fa7eaa RR |
37 | extern bool g_blockEventsOnDrag; |
38 | extern wxCursor g_globalCursor; | |
39 | extern wxWindowGTK *g_delayedFocus; | |
66bd6b93 | 40 | |
c801d85f | 41 | //----------------------------------------------------------------------------- |
e1e955e1 | 42 | // "clicked" |
c801d85f KB |
43 | //----------------------------------------------------------------------------- |
44 | ||
66bd6b93 | 45 | static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb ) |
c801d85f | 46 | { |
acfd422a RR |
47 | if (g_isIdle) wxapp_install_idle_handler(); |
48 | ||
a2053b27 | 49 | if (!cb->m_hasVMT) return; |
ff8bfdbb | 50 | |
b292e2f5 | 51 | if (g_blockEventsOnDrag) return; |
9864c56d RR |
52 | |
53 | if (cb->m_blockEvent) return; | |
ff8bfdbb | 54 | |
b292e2f5 RR |
55 | wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId()); |
56 | event.SetInt( cb->GetValue() ); | |
57 | event.SetEventObject(cb); | |
58 | cb->GetEventHandler()->ProcessEvent(event); | |
6de97a3b | 59 | } |
c801d85f | 60 | |
e1e955e1 RR |
61 | //----------------------------------------------------------------------------- |
62 | // wxCheckBox | |
c801d85f KB |
63 | //----------------------------------------------------------------------------- |
64 | ||
65 | IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl) | |
66 | ||
2cc0e28f | 67 | wxCheckBox::wxCheckBox() |
c801d85f | 68 | { |
6de97a3b | 69 | } |
c801d85f | 70 | |
2cc0e28f VZ |
71 | bool wxCheckBox::Create(wxWindow *parent, |
72 | wxWindowID id, | |
73 | const wxString &label, | |
74 | const wxPoint &pos, | |
75 | const wxSize &size, | |
76 | long style, | |
77 | const wxValidator& validator, | |
78 | const wxString &name ) | |
c801d85f | 79 | { |
b292e2f5 RR |
80 | m_needParent = TRUE; |
81 | m_acceptsFocus = TRUE; | |
9864c56d | 82 | m_blockEvent = FALSE; |
ff8bfdbb | 83 | |
4dcaf11a RR |
84 | if (!PreCreation( parent, pos, size ) || |
85 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
86 | { | |
223d09f6 | 87 | wxFAIL_MSG( wxT("wxCheckBox creation failed") ); |
1db8dc4a | 88 | return FALSE; |
4dcaf11a | 89 | } |
ce4169a4 | 90 | |
2cc0e28f VZ |
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 | |
eaafd2f8 | 98 | m_widgetLabel = gtk_label_new(""); |
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 | ||
2cc0e28f VZ |
105 | gtk_widget_show( m_widgetLabel ); |
106 | gtk_widget_show( m_widgetCheckbox ); | |
107 | } | |
108 | else | |
109 | { | |
eaafd2f8 | 110 | m_widgetCheckbox = gtk_check_button_new_with_label(""); |
9e691f46 | 111 | m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox ); |
2cc0e28f VZ |
112 | m_widget = m_widgetCheckbox; |
113 | } | |
eaafd2f8 | 114 | SetLabel( label ); |
2cc0e28f | 115 | |
2cc0e28f VZ |
116 | gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox), |
117 | "clicked", | |
118 | GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback), | |
119 | (gpointer *)this ); | |
ff8bfdbb | 120 | |
f03fc89f | 121 | m_parent->DoAddChild( this ); |
ff8bfdbb | 122 | |
abdeb9e7 | 123 | PostCreation(size); |
ff8bfdbb | 124 | |
b292e2f5 | 125 | return TRUE; |
6de97a3b | 126 | } |
c801d85f | 127 | |
debe6624 | 128 | void wxCheckBox::SetValue( bool state ) |
c801d85f | 129 | { |
223d09f6 | 130 | wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") ); |
ff8bfdbb | 131 | |
953704c1 | 132 | if (state == GetValue()) |
8bf45ed1 VZ |
133 | return; |
134 | ||
9864c56d | 135 | m_blockEvent = TRUE; |
ae0bdb01 | 136 | |
2cc0e28f | 137 | gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state ); |
1db8dc4a | 138 | |
9864c56d | 139 | m_blockEvent = FALSE; |
6de97a3b | 140 | } |
c801d85f | 141 | |
83058c58 | 142 | bool wxCheckBox::GetValue() const |
c801d85f | 143 | { |
223d09f6 | 144 | wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") ); |
f96aa4d9 | 145 | |
2cc0e28f | 146 | return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active; |
6de97a3b | 147 | } |
c801d85f | 148 | |
83058c58 RR |
149 | void wxCheckBox::SetLabel( const wxString& label ) |
150 | { | |
223d09f6 | 151 | wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") ); |
f96aa4d9 | 152 | |
b292e2f5 | 153 | wxControl::SetLabel( label ); |
ff8bfdbb | 154 | |
eaafd2f8 VS |
155 | #ifdef __WXGTK20__ |
156 | wxString label2 = PrepareLabelMnemonics( label ); | |
157 | gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel), wxGTK_CONV( label2 ) ); | |
158 | #else | |
fab591c5 | 159 | gtk_label_set( GTK_LABEL(m_widgetLabel), wxGTK_CONV( GetLabel() ) ); |
eaafd2f8 | 160 | #endif |
83058c58 RR |
161 | } |
162 | ||
f03fc89f | 163 | bool wxCheckBox::Enable( bool enable ) |
d3904ceb | 164 | { |
f03fc89f VZ |
165 | if ( !wxControl::Enable( enable ) ) |
166 | return FALSE; | |
ff8bfdbb | 167 | |
2cc0e28f | 168 | gtk_widget_set_sensitive( m_widgetLabel, enable ); |
f03fc89f VZ |
169 | |
170 | return TRUE; | |
d3904ceb RR |
171 | } |
172 | ||
58614078 | 173 | void wxCheckBox::ApplyWidgetStyle() |
868a2826 | 174 | { |
b292e2f5 | 175 | SetWidgetStyle(); |
2cc0e28f VZ |
176 | gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle ); |
177 | gtk_widget_set_style( m_widgetLabel, m_widgetStyle ); | |
f96aa4d9 RR |
178 | } |
179 | ||
2f073eb2 RR |
180 | bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window ) |
181 | { | |
9e691f46 | 182 | return window == TOGGLE_BUTTON_EVENT_WIN(m_widget); |
2f073eb2 RR |
183 | } |
184 | ||
185 | void wxCheckBox::OnInternalIdle() | |
186 | { | |
187 | wxCursor cursor = m_cursor; | |
188 | if (g_globalCursor.Ok()) cursor = g_globalCursor; | |
189 | ||
9e691f46 VZ |
190 | GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox); |
191 | if ( event_window && cursor.Ok() ) | |
2f073eb2 RR |
192 | { |
193 | /* I now set the cursor the anew in every OnInternalIdle call | |
1db8dc4a VZ |
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 | ||
9e691f46 | 198 | gdk_window_set_cursor( event_window, cursor.GetCursor() ); |
2f073eb2 RR |
199 | } |
200 | ||
d7fa7eaa RR |
201 | if (g_delayedFocus == this) |
202 | { | |
203 | if (GTK_WIDGET_REALIZED(m_widget)) | |
204 | { | |
205 | gtk_widget_grab_focus( m_widget ); | |
206 | g_delayedFocus = NULL; | |
207 | } | |
208 | } | |
209 | ||
e39af974 JS |
210 | if (wxUpdateUIEvent::CanUpdate(this)) |
211 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
2f073eb2 RR |
212 | } |
213 | ||
f68586e5 VZ |
214 | wxSize wxCheckBox::DoGetBestSize() const |
215 | { | |
db434467 | 216 | return wxControl::DoGetBestSize(); |
f68586e5 VZ |
217 | } |
218 | ||
9d522606 RD |
219 | // static |
220 | wxVisualAttributes | |
221 | wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
222 | { | |
223 | return GetDefaultAttributesFromGTKWidget(gtk_check_button_new); | |
224 | } | |
225 | ||
dcf924a3 | 226 | #endif |