From: Mattia Barbon Date: Tue, 6 Aug 2002 19:54:46 +0000 (+0000) Subject: Patch [ 588732 ] makes wxRB_GROUP/radiobuttons working X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/f37907ff76827b0f77f61448abfd0acabf029843 Patch [ 588732 ] makes wxRB_GROUP/radiobuttons working 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 --- diff --git a/include/wx/motif/radiobut.h b/include/wx/motif/radiobut.h index 514a731c90..ebb0d1ad1e 100644 --- a/include/wx/motif/radiobut.h +++ b/include/wx/motif/radiobut.h @@ -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 diff --git a/src/motif/radiobut.cpp b/src/motif/radiobut.cpp index 9d77c21388..46906f1a93 100644 --- a/src/motif/radiobut.cpp +++ b/src/motif/radiobut.cpp @@ -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(); + } +}