1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxRadioButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "radiobut.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtDisplay XTDISPLAY
25 #include "wx/radiobut.h"
29 #pragma message disable nosimpint
31 #include <Xm/ToggleB.h>
32 #include <Xm/ToggleBG.h>
34 #pragma message enable nosimpint
37 #include "wx/motif/private.h"
39 void wxRadioButtonCallback (Widget w
, XtPointer clientData
,
40 XmToggleButtonCallbackStruct
* cbs
);
42 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
, wxControl
)
44 wxRadioButton::wxRadioButton()
48 bool wxRadioButton::Create(wxWindow
*parent
, wxWindowID id
,
49 const wxString
& label
,
51 const wxSize
& size
, long style
,
52 const wxValidator
& validator
,
55 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
58 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
60 wxString
label1(wxStripMenuCodes(label
));
62 wxXmString
text( label1
);
64 WXFontType fontType
= m_font
.GetFontType(XtDisplay(parentWidget
));
66 Widget radioButtonWidget
= XtVaCreateManagedWidget ("toggle",
68 xmToggleButtonGadgetClass
, parentWidget
,
70 xmToggleButtonWidgetClass
, parentWidget
,
72 wxFont::GetFontTag(), fontType
,
73 XmNlabelString
, text(),
74 XmNfillOnSelect
, True
,
75 XmNindicatorType
, XmONE_OF_MANY
, // diamond-shape
78 XtAddCallback (radioButtonWidget
,
79 XmNvalueChangedCallback
,
80 (XtCallbackProc
)wxRadioButtonCallback
,
83 m_mainWidget
= (WXWidget
) radioButtonWidget
;
85 XtManageChild (radioButtonWidget
);
87 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
88 pos
.x
, pos
.y
, size
.x
, size
.y
);
90 ChangeBackgroundColour();
92 //copied from mac/radiobut.cpp (from here till "return true;")
95 if (HasFlag(wxRB_GROUP
))
101 /* search backward for last group start */
102 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
103 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
106 wxWindow
*child
= node
->GetData();
107 if (child
->IsKindOf( CLASSINFO( wxRadioButton
) ) )
109 chief
= (wxRadioButton
*) child
;
110 if (child
->HasFlag(wxRB_GROUP
)) break;
112 node
= node
->GetPrevious();
114 AddInCycle( chief
) ;
119 void wxRadioButton::SetValue(bool value
)
121 if (GetValue() == value
)
125 XmToggleButtonSetState ((Widget
) m_mainWidget
, (Boolean
) value
, False
);
126 m_inSetValue
= false;
131 // Get single selection, for single choice list items
132 bool wxRadioButton::GetValue() const
134 return (XmToggleButtonGetState ((Widget
) m_mainWidget
) != 0);
137 void wxRadioButton::Command (wxCommandEvent
& event
)
139 SetValue ( (event
.GetInt() != 0) );
140 ProcessCommand (event
);
143 void wxRadioButton::ChangeBackgroundColour()
145 wxWindow::ChangeBackgroundColour();
147 // What colour should this be?
148 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
150 XtVaSetValues ((Widget
) GetMainWidget(),
151 XmNselectColor
, selectPixel
,
155 void wxRadioButtonCallback (Widget w
, XtPointer clientData
,
156 XmToggleButtonCallbackStruct
* cbs
)
161 wxRadioButton
*item
= (wxRadioButton
*) clientData
;
162 if (item
->InSetValue())
165 //based on mac/radiobut.cpp
166 wxRadioButton
* old
= item
->ClearSelections();
167 item
->SetValue(true);
171 wxCommandEvent
event(wxEVT_COMMAND_RADIOBUTTON_SELECTED
,
173 event
.SetEventObject(old
);
174 event
.SetInt( false );
175 old
->ProcessCommand(event
);
177 wxCommandEvent
event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED
, item
->GetId() );
178 event2
.SetEventObject(item
);
179 event2
.SetInt( true );
180 item
->ProcessCommand(event2
);
183 wxRadioButton
* wxRadioButton::AddInCycle(wxRadioButton
*cycle
)
186 wxRadioButton
* current
;
196 while ((next
= current
->m_cycle
) != cycle
)
197 current
= current
->m_cycle
;
199 current
->m_cycle
= this;
204 wxRadioButton
* wxRadioButton::ClearSelections()
206 wxRadioButton
* cycle
= NextInCycle();
207 wxRadioButton
* old
= 0;
211 while (cycle
!= this)
213 if ( cycle
->GetValue() )
216 cycle
->SetValue(false);
218 cycle
= cycle
->NextInCycle();
225 void wxRadioButton::RemoveFromCycle()
227 wxRadioButton
* curr
= NextInCycle();
231 if( curr
->NextInCycle() == this )
233 curr
->m_cycle
= this->m_cycle
;
237 curr
= curr
->NextInCycle();