]> git.saurik.com Git - wxWidgets.git/blobdiff - src/univ/checkbox.cpp
fixed bug due to missing break in GetColourFromGTKWidget() (part of patch 1160117)
[wxWidgets.git] / src / univ / checkbox.cpp
index 95ddff4c9c272dc0a4fa9b7f4a8098281f5c24bc..48d4fd2b6c0bff761ea4776ba5a551aea33f7235 100644 (file)
@@ -5,8 +5,8 @@
 // Modified by:
 // Created:     25.08.00
 // RCS-ID:      $Id$
-// Copyright:   (c) 2000 Vadim Zeitlin
-// Licence:     wxWindows license
+// Copyright:   (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
@@ -17,7 +17,7 @@
 // headers
 // ----------------------------------------------------------------------------
 
-#ifdef __GNUG__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
     #pragma implementation "univcheckbox.h"
 #endif
 
@@ -54,7 +54,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
 
 void wxCheckBox::Init()
 {
-    m_isPressed = FALSE;
+    m_isPressed = false;
     m_status = Status_Unchecked;
 }
 
@@ -67,15 +67,15 @@ bool wxCheckBox::Create(wxWindow *parent,
                         const wxValidator& validator,
                         const wxString &name)
 {
-    if ( !wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name) )
-        return FALSE;
+    if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
+        return false;
 
     SetLabel(label);
     SetBestSize(size);
 
     CreateInputHandler(wxINP_HANDLER_CHECKBOX);
 
-    return TRUE;
+    return true;
 }
 
 // ----------------------------------------------------------------------------
@@ -84,24 +84,12 @@ bool wxCheckBox::Create(wxWindow *parent,
 
 bool wxCheckBox::GetValue() const
 {
-    return m_status == Status_Checked;
+    return (Get3StateValue() != wxCHK_UNCHECKED);
 }
 
 void wxCheckBox::SetValue(bool value)
 {
-    Status status = value ? Status_Checked : Status_Unchecked;
-    if ( status != m_status )
-    {
-        m_status = status;
-
-        if ( m_status == Status_Checked )
-        {
-            // invoke the hook
-            OnCheck();
-        }
-
-        Refresh();
-    }
+    Set3StateValue( value ? wxCHK_CHECKED : wxCHK_UNCHECKED );
 }
 
 void wxCheckBox::OnCheck()
@@ -151,13 +139,19 @@ void wxCheckBox::DoDraw(wxControlRenderer *renderer)
     dc.SetFont(GetFont());
     dc.SetTextForeground(GetForegroundColour());
 
-    if ( m_status == Status_Checked )
-        flags |= wxCONTROL_CHECKED;
+    switch ( Get3StateValue() )
+    {
+        case wxCHK_CHECKED:      flags |= wxCONTROL_CHECKED;      break;
+        case wxCHK_UNDETERMINED: flags |= wxCONTROL_UNDETERMINED; break;
+        default:                 /* do nothing */                 break;
+    }
+
+    wxBitmap bitmap(GetBitmap(GetState(flags), m_status));
 
     renderer->GetRenderer()->
         DrawCheckButton(dc,
                         GetLabel(),
-                        GetBitmap(GetState(flags), m_status),
+                        bitmap,
                         renderer->GetRect(),
                         flags,
                         GetWindowStyle() & wxALIGN_RIGHT ? wxALIGN_RIGHT
@@ -201,11 +195,46 @@ wxSize wxCheckBox::DoGetBestClientSize() const
 // checkbox actions
 // ----------------------------------------------------------------------------
 
+void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
+{
+    Status status;
+    switch ( state )
+    {
+        case wxCHK_UNCHECKED:    status = Status_Unchecked;   break;
+        case wxCHK_CHECKED:      status = Status_Checked; break;
+        default:                 wxFAIL_MSG(_T("Unknown checkbox state"));
+        case wxCHK_UNDETERMINED: status = Status_3rdState;  break;
+    }
+    if ( status != m_status )
+    {
+        m_status = status;
+
+        if ( m_status == Status_Checked )
+        {
+            // invoke the hook
+            OnCheck();
+        }
+
+        Refresh();
+    }
+}
+
+wxCheckBoxState wxCheckBox::DoGet3StateValue() const
+{
+    switch ( m_status )
+    {
+        case Status_Checked:    return wxCHK_CHECKED;
+        case Status_Unchecked:  return wxCHK_UNCHECKED;
+        default:                /* go further */ break;
+    }
+    return wxCHK_UNDETERMINED;
+}
+
 void wxCheckBox::Press()
 {
     if ( !m_isPressed )
     {
-        m_isPressed = TRUE;
+        m_isPressed = true;
 
         Refresh();
     }
@@ -215,7 +244,7 @@ void wxCheckBox::Release()
 {
     if ( m_isPressed )
     {
-        m_isPressed = FALSE;
+        m_isPressed = false;
 
         Refresh();
     }
@@ -223,9 +252,27 @@ void wxCheckBox::Release()
 
 void wxCheckBox::Toggle()
 {
-    m_isPressed = FALSE;
+    m_isPressed = false;
+
+    Status status = m_status;
+
+    switch ( Get3StateValue() )
+    {
+        case wxCHK_CHECKED:
+            Set3StateValue(Is3rdStateAllowedForUser() ? wxCHK_UNDETERMINED : wxCHK_UNCHECKED);
+            break;
+
+        case wxCHK_UNCHECKED:
+            Set3StateValue(wxCHK_CHECKED);
+            break;
 
-    ChangeValue(!GetValue());
+        case wxCHK_UNDETERMINED:
+            Set3StateValue(wxCHK_UNCHECKED);
+            break;
+    }
+
+    if( status != m_status )
+        SendEvent();
 }
 
 void wxCheckBox::ChangeValue(bool value)
@@ -239,7 +286,17 @@ void wxCheckBox::SendEvent()
 {
     wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
     InitCommandEvent(event);
-    event.SetInt(IsChecked());
+    wxCheckBoxState state = Get3StateValue();
+
+    // If the style flag to allow the user setting the undetermined state
+    // is not set, then skip the undetermined state and set it to unchecked.
+    if ( state == wxCHK_UNDETERMINED && !Is3rdStateAllowedForUser() )
+    {
+        state = wxCHK_UNCHECKED;
+        Set3StateValue(state);
+    }
+
+    event.SetInt(state);
     Command(event);
 }
 
@@ -256,15 +313,15 @@ bool wxCheckBox::PerformAction(const wxControlAction& action,
     else if ( action == wxACTION_BUTTON_RELEASE )
         Release();
     if ( action == wxACTION_CHECKBOX_CHECK )
-        ChangeValue(TRUE);
+        ChangeValue(true);
     else if ( action == wxACTION_CHECKBOX_CLEAR )
-        ChangeValue(FALSE);
+        ChangeValue(false);
     else if ( action == wxACTION_CHECKBOX_TOGGLE )
         Toggle();
     else
         return wxControl::PerformAction(action, numArg, strArg);
 
-    return TRUE;
+    return true;
 }
 
 // ----------------------------------------------------------------------------
@@ -276,12 +333,12 @@ wxStdCheckboxInputHandler::wxStdCheckboxInputHandler(wxInputHandler *inphand)
 {
 }
 
-bool wxStdCheckboxInputHandler::HandleActivation(wxControl *control,
-                                                 bool activated)
+bool wxStdCheckboxInputHandler::HandleActivation(wxInputConsumer *consumer,
+                                                 bool WXUNUSED(activated))
 {
     // only the focused checkbox appearance changes when the app gains/loses
     // activation
-    return control->IsFocused();
+    return consumer->GetInputWindow()->IsFocused();
 }
 
 #endif // wxUSE_CHECKBOX