1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/radiobut.cpp
3 // Purpose: wxRadioButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #define XtDisplay XTDISPLAY
19 #include "wx/radiobut.h"
26 #pragma message disable nosimpint
28 #include <Xm/ToggleB.h>
29 #include <Xm/ToggleBG.h>
31 #pragma message enable nosimpint
34 #include "wx/motif/private.h"
36 void wxRadioButtonCallback (Widget w
, XtPointer clientData
,
37 XmToggleButtonCallbackStruct
* cbs
);
39 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
, wxControl
)
41 wxRadioButton::wxRadioButton()
45 bool wxRadioButton::Create(wxWindow
*parent
, wxWindowID id
,
46 const wxString
& label
,
48 const wxSize
& size
, long style
,
49 const wxValidator
& validator
,
52 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
55 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
56 Display
* dpy
= XtDisplay(parentWidget
);
58 wxString
label1(wxStripMenuCodes(label
));
60 wxXmString
text( label1
);
62 Widget radioButtonWidget
= XtVaCreateManagedWidget ("toggle",
64 xmToggleButtonGadgetClass
, parentWidget
,
66 xmToggleButtonWidgetClass
, parentWidget
,
68 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
69 XmNlabelString
, text(),
70 XmNfillOnSelect
, True
,
71 XmNindicatorType
, XmONE_OF_MANY
, // diamond-shape
74 XtAddCallback (radioButtonWidget
,
75 XmNvalueChangedCallback
,
76 (XtCallbackProc
)wxRadioButtonCallback
,
79 m_mainWidget
= (WXWidget
) radioButtonWidget
;
81 XtManageChild (radioButtonWidget
);
83 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
84 pos
.x
, pos
.y
, size
.x
, size
.y
);
86 ChangeBackgroundColour();
88 //copied from mac/radiobut.cpp (from here till "return true;")
91 if (HasFlag(wxRB_GROUP
))
97 /* search backward for last group start */
98 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
99 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
102 wxWindow
*child
= node
->GetData();
103 if (child
->IsKindOf( CLASSINFO( wxRadioButton
) ) )
105 chief
= (wxRadioButton
*) child
;
106 if (child
->HasFlag(wxRB_GROUP
)) break;
108 node
= node
->GetPrevious();
110 AddInCycle( chief
) ;
115 void wxRadioButton::SetValue(bool value
)
117 if (GetValue() == value
)
121 XmToggleButtonSetState ((Widget
) m_mainWidget
, (Boolean
) value
, False
);
122 m_inSetValue
= false;
127 // Get single selection, for single choice list items
128 bool wxRadioButton::GetValue() const
130 return (XmToggleButtonGetState ((Widget
) m_mainWidget
) != 0);
133 void wxRadioButton::Command (wxCommandEvent
& event
)
135 SetValue ( (event
.GetInt() != 0) );
136 ProcessCommand (event
);
139 void wxRadioButton::ChangeBackgroundColour()
141 wxWindow::ChangeBackgroundColour();
143 // What colour should this be?
144 wxColour colour
= *wxBLACK
;
145 int selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
147 XtVaSetValues ((Widget
) GetMainWidget(),
148 XmNselectColor
, selectPixel
,
152 void wxRadioButtonCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
153 XmToggleButtonCallbackStruct
* cbs
)
158 wxRadioButton
*item
= (wxRadioButton
*) clientData
;
159 if (item
->InSetValue())
162 //based on mac/radiobut.cpp
163 wxRadioButton
* old
= item
->ClearSelections();
164 item
->SetValue(true);
168 wxCommandEvent
event(wxEVT_COMMAND_RADIOBUTTON_SELECTED
,
170 event
.SetEventObject(old
);
171 event
.SetInt( false );
172 old
->ProcessCommand(event
);
174 wxCommandEvent
event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED
, item
->GetId() );
175 event2
.SetEventObject(item
);
176 event2
.SetInt( true );
177 item
->ProcessCommand(event2
);
180 wxRadioButton
* wxRadioButton::AddInCycle(wxRadioButton
*cycle
)
188 wxRadioButton
* current
= cycle
;
189 while ( current
->m_cycle
!= cycle
)
190 current
= current
->m_cycle
;
192 current
->m_cycle
= this;
198 wxRadioButton
* wxRadioButton::ClearSelections()
200 wxRadioButton
* cycle
= NextInCycle();
201 wxRadioButton
* old
= 0;
205 while (cycle
!= this)
207 if ( cycle
->GetValue() )
210 cycle
->SetValue(false);
212 cycle
= cycle
->NextInCycle();
219 void wxRadioButton::RemoveFromCycle()
221 wxRadioButton
* curr
= NextInCycle();
225 if( curr
->NextInCycle() == this )
227 curr
->m_cycle
= this->m_cycle
;
231 curr
= curr
->NextInCycle();