]> git.saurik.com Git - wxWidgets.git/commitdiff
Patch [ 588732 ] makes wxRB_GROUP/radiobuttons working
authorMattia Barbon <mbarbon@cpan.org>
Tue, 6 Aug 2002 19:54:46 +0000 (19:54 +0000)
committerMattia Barbon <mbarbon@cpan.org>
Tue, 6 Aug 2002 19:54:46 +0000 (19:54 +0000)
from Marcin Wojdyr (wojdyr)

Applied with some modifications (remove buttons from the chain on
destruction, or the program will segfault).

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16382 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/motif/radiobut.h
src/motif/radiobut.cpp

index 514a731c9033902f3a49d78d6792be4c0d8d1bc7..ebb0d1ad1e4afe0f3d8937a0fb7289dce49ea69c 100644 (file)
@@ -26,6 +26,8 @@ class WXDLLEXPORT wxRadioButton: public wxControl
 protected:
 public:
     wxRadioButton();
+    ~wxRadioButton() { RemoveFromCycle(); }
+
     inline wxRadioButton(wxWindow *parent, wxWindowID id,
         const wxString& label,
         const wxPoint& pos = wxDefaultPosition,
@@ -52,6 +54,17 @@ public:
     virtual void ChangeFont(bool keepOriginalSize = TRUE);
     virtual void ChangeBackgroundColour();
     virtual void ChangeForegroundColour();
+
+    // *this function is an implementation detail*
+    // clears the selection in the readiobuttons in the cycle
+    // and returns the old selection (if any)
+    wxRadioButton* ClearSelections();
+private:
+    wxRadioButton* AddInCycle(wxRadioButton* cycle);
+    void RemoveFromCycle();
+    wxRadioButton* NextInCycle() { return m_cycle; }
+
+    wxRadioButton *m_cycle;
 };
 
 // Not implemented
index 9d77c21388a01c6b2de3990fcab1f57c2c86b104..46906f1a93dddbc394c02f181a5529267095ea2b 100644 (file)
@@ -101,14 +101,43 @@ bool wxRadioButton::Create(wxWindow *parent, wxWindowID id,
 
     ChangeBackgroundColour();
 
+    //copied from mac/radiobut.cpp (from here till "return TRUE;")
+    m_cycle = this ;
+  
+    if (HasFlag(wxRB_GROUP))
+    {
+        AddInCycle( NULL ) ;
+    }
+    else
+    {
+        /* search backward for last group start */
+        wxRadioButton *chief = (wxRadioButton*) NULL;
+        wxWindowList::Node *node = parent->GetChildren().GetLast();
+        while (node)
+        {
+            wxWindow *child = node->GetData();
+            if (child->IsKindOf( CLASSINFO( wxRadioButton ) ) )
+            {
+                chief = (wxRadioButton*) child;
+                if (child->HasFlag(wxRB_GROUP)) break;
+            }
+            node = node->GetPrevious();
+        }
+        AddInCycle( chief ) ;
+    }
     return TRUE;
 }
 
 void wxRadioButton::SetValue(bool value)
 {
+    if (GetValue() == value)
+        return;
+
     m_inSetValue = TRUE;
     XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, FALSE);
     m_inSetValue = FALSE;
+
+    ClearSelections();
 }
 
 // Get single selection, for single choice list items
@@ -155,9 +184,78 @@ void wxRadioButtonCallback (Widget w, XtPointer clientData,
     if (item->InSetValue())
         return;
 
-    wxCommandEvent event (wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId());
-    event.SetEventObject(item);
+    //based on mac/radiobut.cpp
+    wxRadioButton* old = item->ClearSelections();
+    item->SetValue(TRUE);
+
+    if ( old )
+    {
+        wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED,
+                             old->GetId() );
+        event.SetEventObject(old);
+        event.SetInt( FALSE );
+        old->ProcessCommand(event);
+    }
+    wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() );
+    event2.SetEventObject(item);
+    event2.SetInt( TRUE );
+    item->ProcessCommand(event2);
+}
+
+wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle)
+{
+    wxRadioButton* next;
+    wxRadioButton* current;
+       
+    if (cycle == NULL)
+    {
+        m_cycle = this;
+        return this;
+    }
+    else
+    {
+        current = cycle;
+        while ((next = current->m_cycle) != cycle) 
+            current = current->m_cycle;
+        m_cycle = cycle;
+        current->m_cycle = this;
+        return cycle;
+    }
+}
 
-    item->ProcessCommand (event);
+wxRadioButton* wxRadioButton::ClearSelections()
+{
+    wxRadioButton* cycle = NextInCycle();
+    wxRadioButton* old = 0;
+
+    if (cycle)
+    {
+        while (cycle != this)
+        {
+            if ( cycle->GetValue() )
+            {
+                old = cycle;
+                cycle->SetValue(FALSE);
+            }
+            cycle = cycle->NextInCycle();
+        }
+    }
+
+    return old;
 }
 
+void wxRadioButton::RemoveFromCycle()
+{
+    wxRadioButton* curr = NextInCycle();
+
+    while( curr )
+    {
+        if( curr->NextInCycle() == this )
+        {
+            curr->m_cycle = this->m_cycle;
+            return;
+        }
+
+        curr = curr->NextInCycle();
+    }
+}