| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/gtk1/checkbox.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Copyright: (c) 1998 Robert Roebling |
| 6 | // Licence: wxWindows licence |
| 7 | ///////////////////////////////////////////////////////////////////////////// |
| 8 | |
| 9 | // For compilers that support precompilation, includes "wx.h". |
| 10 | #include "wx/wxprec.h" |
| 11 | |
| 12 | #if wxUSE_CHECKBOX |
| 13 | |
| 14 | #include "wx/checkbox.h" |
| 15 | |
| 16 | #include "wx/gtk1/private.h" |
| 17 | |
| 18 | //----------------------------------------------------------------------------- |
| 19 | // idle system |
| 20 | //----------------------------------------------------------------------------- |
| 21 | |
| 22 | extern void wxapp_install_idle_handler(); |
| 23 | extern bool g_isIdle; |
| 24 | |
| 25 | //----------------------------------------------------------------------------- |
| 26 | // data |
| 27 | //----------------------------------------------------------------------------- |
| 28 | |
| 29 | extern bool g_blockEventsOnDrag; |
| 30 | extern wxCursor g_globalCursor; |
| 31 | extern wxWindowGTK *g_delayedFocus; |
| 32 | |
| 33 | //----------------------------------------------------------------------------- |
| 34 | // "clicked" |
| 35 | //----------------------------------------------------------------------------- |
| 36 | |
| 37 | extern "C" { |
| 38 | static void gtk_checkbox_toggled_callback(GtkWidget *WXUNUSED(widget), |
| 39 | wxCheckBox *cb) |
| 40 | { |
| 41 | if (g_isIdle) wxapp_install_idle_handler(); |
| 42 | |
| 43 | if (!cb->m_hasVMT) return; |
| 44 | |
| 45 | if (g_blockEventsOnDrag) return; |
| 46 | |
| 47 | if (cb->m_blockEvent) return; |
| 48 | |
| 49 | wxCommandEvent event(wxEVT_CHECKBOX, cb->GetId()); |
| 50 | event.SetInt(cb->GetValue()); |
| 51 | event.SetEventObject(cb); |
| 52 | cb->HandleWindowEvent(event); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | //----------------------------------------------------------------------------- |
| 57 | // wxCheckBox |
| 58 | //----------------------------------------------------------------------------- |
| 59 | |
| 60 | wxCheckBox::wxCheckBox() |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | bool 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 ) |
| 72 | { |
| 73 | m_needParent = true; |
| 74 | m_acceptsFocus = true; |
| 75 | m_blockEvent = false; |
| 76 | |
| 77 | WXValidateStyle(&style); |
| 78 | if (!PreCreation( parent, pos, size ) || |
| 79 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 80 | { |
| 81 | wxFAIL_MSG( wxT("wxCheckBox creation failed") ); |
| 82 | return false; |
| 83 | } |
| 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(); |
| 91 | |
| 92 | m_widgetLabel = gtk_label_new(""); |
| 93 | gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5); |
| 94 | |
| 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 | |
| 99 | gtk_widget_show( m_widgetLabel ); |
| 100 | gtk_widget_show( m_widgetCheckbox ); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | m_widgetCheckbox = gtk_check_button_new_with_label(""); |
| 105 | m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox ); |
| 106 | m_widget = m_widgetCheckbox; |
| 107 | } |
| 108 | SetLabel( label ); |
| 109 | |
| 110 | gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox), |
| 111 | "toggled", |
| 112 | GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback), |
| 113 | (gpointer *)this ); |
| 114 | |
| 115 | m_parent->DoAddChild( this ); |
| 116 | |
| 117 | PostCreation(size); |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | void wxCheckBox::SetValue( bool state ) |
| 123 | { |
| 124 | wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") ); |
| 125 | |
| 126 | if (state == GetValue()) |
| 127 | return; |
| 128 | |
| 129 | m_blockEvent = true; |
| 130 | |
| 131 | gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state ); |
| 132 | |
| 133 | m_blockEvent = false; |
| 134 | } |
| 135 | |
| 136 | bool wxCheckBox::GetValue() const |
| 137 | { |
| 138 | wxCHECK_MSG( m_widgetCheckbox != NULL, false, wxT("invalid checkbox") ); |
| 139 | |
| 140 | return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active; |
| 141 | } |
| 142 | |
| 143 | void wxCheckBox::SetLabel( const wxString& label ) |
| 144 | { |
| 145 | wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") ); |
| 146 | |
| 147 | GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label); |
| 148 | } |
| 149 | |
| 150 | bool wxCheckBox::Enable( bool enable ) |
| 151 | { |
| 152 | if ( !wxControl::Enable( enable ) ) |
| 153 | return false; |
| 154 | |
| 155 | gtk_widget_set_sensitive( m_widgetLabel, enable ); |
| 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style) |
| 161 | { |
| 162 | gtk_widget_modify_style(m_widgetCheckbox, style); |
| 163 | gtk_widget_modify_style(m_widgetLabel, style); |
| 164 | } |
| 165 | |
| 166 | bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window ) |
| 167 | { |
| 168 | return window == TOGGLE_BUTTON_EVENT_WIN(m_widget); |
| 169 | } |
| 170 | |
| 171 | void wxCheckBox::OnInternalIdle() |
| 172 | { |
| 173 | wxCursor cursor = m_cursor; |
| 174 | if (g_globalCursor.IsOk()) cursor = g_globalCursor; |
| 175 | |
| 176 | GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox); |
| 177 | if ( event_window && cursor.IsOk() ) |
| 178 | { |
| 179 | /* I now set the cursor the anew in every OnInternalIdle call |
| 180 | as setting the cursor in a parent window also effects the |
| 181 | windows above so that checking for the current cursor is |
| 182 | not possible. */ |
| 183 | |
| 184 | gdk_window_set_cursor( event_window, cursor.GetCursor() ); |
| 185 | } |
| 186 | |
| 187 | if (g_delayedFocus == this) |
| 188 | { |
| 189 | if (GTK_WIDGET_REALIZED(m_widget)) |
| 190 | { |
| 191 | gtk_widget_grab_focus( m_widget ); |
| 192 | g_delayedFocus = NULL; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (wxUpdateUIEvent::CanUpdate(this)) |
| 197 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); |
| 198 | } |
| 199 | |
| 200 | wxSize wxCheckBox::DoGetBestSize() const |
| 201 | { |
| 202 | return wxControl::DoGetBestSize(); |
| 203 | } |
| 204 | |
| 205 | // static |
| 206 | wxVisualAttributes |
| 207 | wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
| 208 | { |
| 209 | return GetDefaultAttributesFromGTKWidget(gtk_check_button_new); |
| 210 | } |
| 211 | |
| 212 | #endif |