| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: checkbox.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | |
| 11 | #ifdef __GNUG__ |
| 12 | #pragma implementation "checkbox.h" |
| 13 | #endif |
| 14 | |
| 15 | #include "wx/defs.h" |
| 16 | |
| 17 | #if wxUSE_CHECKBOX |
| 18 | |
| 19 | #include "wx/checkbox.h" |
| 20 | |
| 21 | #include "wx/gtk/private.h" |
| 22 | |
| 23 | //----------------------------------------------------------------------------- |
| 24 | // idle system |
| 25 | //----------------------------------------------------------------------------- |
| 26 | |
| 27 | extern void wxapp_install_idle_handler(); |
| 28 | extern bool g_isIdle; |
| 29 | |
| 30 | //----------------------------------------------------------------------------- |
| 31 | // data |
| 32 | //----------------------------------------------------------------------------- |
| 33 | |
| 34 | extern bool g_blockEventsOnDrag; |
| 35 | extern wxCursor g_globalCursor; |
| 36 | |
| 37 | //----------------------------------------------------------------------------- |
| 38 | // "clicked" |
| 39 | //----------------------------------------------------------------------------- |
| 40 | |
| 41 | static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb ) |
| 42 | { |
| 43 | if (g_isIdle) wxapp_install_idle_handler(); |
| 44 | |
| 45 | if (!cb->m_hasVMT) return; |
| 46 | |
| 47 | if (g_blockEventsOnDrag) return; |
| 48 | |
| 49 | if (cb->m_blockEvent) return; |
| 50 | |
| 51 | wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId()); |
| 52 | event.SetInt( cb->GetValue() ); |
| 53 | event.SetEventObject(cb); |
| 54 | cb->GetEventHandler()->ProcessEvent(event); |
| 55 | } |
| 56 | |
| 57 | //----------------------------------------------------------------------------- |
| 58 | // wxCheckBox |
| 59 | //----------------------------------------------------------------------------- |
| 60 | |
| 61 | IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl) |
| 62 | |
| 63 | wxCheckBox::wxCheckBox() |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | bool 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 ) |
| 75 | { |
| 76 | m_needParent = TRUE; |
| 77 | m_acceptsFocus = TRUE; |
| 78 | m_blockEvent = FALSE; |
| 79 | |
| 80 | if (!PreCreation( parent, pos, size ) || |
| 81 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 82 | { |
| 83 | wxFAIL_MSG( wxT("wxCheckBox creation failed") ); |
| 84 | return FALSE; |
| 85 | } |
| 86 | |
| 87 | wxControl::SetLabel( label ); |
| 88 | |
| 89 | if ( style & wxALIGN_RIGHT ) |
| 90 | { |
| 91 | // VZ: as I don't know a way to create a right aligned checkbox with |
| 92 | // GTK we will create a checkbox without label and a label at the |
| 93 | // left of it |
| 94 | m_widgetCheckbox = gtk_check_button_new(); |
| 95 | |
| 96 | m_widgetLabel = gtk_label_new(m_label.mbc_str()); |
| 97 | gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5); |
| 98 | |
| 99 | m_widget = gtk_hbox_new(FALSE, 0); |
| 100 | gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3); |
| 101 | gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3); |
| 102 | |
| 103 | gtk_widget_show( m_widgetLabel ); |
| 104 | gtk_widget_show( m_widgetCheckbox ); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | m_widgetCheckbox = gtk_check_button_new_with_label( m_label.mbc_str() ); |
| 109 | m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox ); |
| 110 | m_widget = m_widgetCheckbox; |
| 111 | } |
| 112 | |
| 113 | gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox), |
| 114 | "clicked", |
| 115 | GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback), |
| 116 | (gpointer *)this ); |
| 117 | |
| 118 | m_parent->DoAddChild( this ); |
| 119 | |
| 120 | PostCreation(); |
| 121 | |
| 122 | SetFont( parent->GetFont() ); |
| 123 | |
| 124 | wxSize size_best( DoGetBestSize() ); |
| 125 | wxSize new_size( size ); |
| 126 | if (new_size.x == -1) |
| 127 | new_size.x = size_best.x; |
| 128 | if (new_size.y == -1) |
| 129 | new_size.y = size_best.y; |
| 130 | if ((new_size.x != size.x) || (new_size.y != size.y)) |
| 131 | SetSize( new_size.x, new_size.y ); |
| 132 | |
| 133 | SetBackgroundColour( parent->GetBackgroundColour() ); |
| 134 | SetForegroundColour( parent->GetForegroundColour() ); |
| 135 | |
| 136 | Show( TRUE ); |
| 137 | |
| 138 | return TRUE; |
| 139 | } |
| 140 | |
| 141 | void wxCheckBox::SetValue( bool state ) |
| 142 | { |
| 143 | wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") ); |
| 144 | |
| 145 | if (state == GetValue()) |
| 146 | return; |
| 147 | |
| 148 | m_blockEvent = TRUE; |
| 149 | |
| 150 | gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state ); |
| 151 | |
| 152 | m_blockEvent = FALSE; |
| 153 | } |
| 154 | |
| 155 | bool wxCheckBox::GetValue() const |
| 156 | { |
| 157 | wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") ); |
| 158 | |
| 159 | return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active; |
| 160 | } |
| 161 | |
| 162 | void wxCheckBox::SetLabel( const wxString& label ) |
| 163 | { |
| 164 | wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") ); |
| 165 | |
| 166 | wxControl::SetLabel( label ); |
| 167 | |
| 168 | gtk_label_set( GTK_LABEL(m_widgetLabel), GetLabel().mbc_str() ); |
| 169 | } |
| 170 | |
| 171 | bool wxCheckBox::Enable( bool enable ) |
| 172 | { |
| 173 | if ( !wxControl::Enable( enable ) ) |
| 174 | return FALSE; |
| 175 | |
| 176 | gtk_widget_set_sensitive( m_widgetLabel, enable ); |
| 177 | |
| 178 | return TRUE; |
| 179 | } |
| 180 | |
| 181 | void wxCheckBox::ApplyWidgetStyle() |
| 182 | { |
| 183 | SetWidgetStyle(); |
| 184 | gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle ); |
| 185 | gtk_widget_set_style( m_widgetLabel, m_widgetStyle ); |
| 186 | } |
| 187 | |
| 188 | bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window ) |
| 189 | { |
| 190 | return window == TOGGLE_BUTTON_EVENT_WIN(m_widget); |
| 191 | } |
| 192 | |
| 193 | void wxCheckBox::OnInternalIdle() |
| 194 | { |
| 195 | wxCursor cursor = m_cursor; |
| 196 | if (g_globalCursor.Ok()) cursor = g_globalCursor; |
| 197 | |
| 198 | GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox); |
| 199 | if ( event_window && cursor.Ok() ) |
| 200 | { |
| 201 | /* I now set the cursor the anew in every OnInternalIdle call |
| 202 | as setting the cursor in a parent window also effects the |
| 203 | windows above so that checking for the current cursor is |
| 204 | not possible. */ |
| 205 | |
| 206 | gdk_window_set_cursor( event_window, cursor.GetCursor() ); |
| 207 | } |
| 208 | |
| 209 | UpdateWindowUI(); |
| 210 | } |
| 211 | |
| 212 | wxSize wxCheckBox::DoGetBestSize() const |
| 213 | { |
| 214 | return wxControl::DoGetBestSize(); |
| 215 | } |
| 216 | |
| 217 | #endif |