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"
23 #pragma message disable nosimpint
25 #include <Xm/ToggleB.h>
26 #include <Xm/ToggleBG.h>
28 #pragma message enable nosimpint
31 #include "wx/motif/private.h"
33 void wxRadioButtonCallback (Widget w
, XtPointer clientData
,
34 XmToggleButtonCallbackStruct
* cbs
);
36 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
, wxControl
)
38 wxRadioButton::wxRadioButton()
42 bool wxRadioButton::Create(wxWindow
*parent
, wxWindowID id
,
43 const wxString
& label
,
45 const wxSize
& size
, long style
,
46 const wxValidator
& validator
,
49 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
52 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
53 Display
* dpy
= XtDisplay(parentWidget
);
55 wxString
label1(wxStripMenuCodes(label
));
57 wxXmString
text( label1
);
59 Widget radioButtonWidget
= XtVaCreateManagedWidget ("toggle",
61 xmToggleButtonGadgetClass
, parentWidget
,
63 xmToggleButtonWidgetClass
, parentWidget
,
65 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
66 XmNlabelString
, text(),
67 XmNfillOnSelect
, True
,
68 XmNindicatorType
, XmONE_OF_MANY
, // diamond-shape
71 XtAddCallback (radioButtonWidget
,
72 XmNvalueChangedCallback
,
73 (XtCallbackProc
)wxRadioButtonCallback
,
76 m_mainWidget
= (WXWidget
) radioButtonWidget
;
78 XtManageChild (radioButtonWidget
);
80 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
81 pos
.x
, pos
.y
, size
.x
, size
.y
);
83 ChangeBackgroundColour();
85 //copied from mac/radiobut.cpp (from here till "return true;")
88 if (HasFlag(wxRB_GROUP
))
94 /* search backward for last group start */
95 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
96 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
99 wxWindow
*child
= node
->GetData();
100 if (child
->IsKindOf( CLASSINFO( wxRadioButton
) ) )
102 chief
= (wxRadioButton
*) child
;
103 if (child
->HasFlag(wxRB_GROUP
)) break;
105 node
= node
->GetPrevious();
107 AddInCycle( chief
) ;
112 void wxRadioButton::SetValue(bool value
)
114 if (GetValue() == value
)
118 XmToggleButtonSetState ((Widget
) m_mainWidget
, (Boolean
) value
, False
);
119 m_inSetValue
= false;
124 // Get single selection, for single choice list items
125 bool wxRadioButton::GetValue() const
127 return (XmToggleButtonGetState ((Widget
) m_mainWidget
) != 0);
130 void wxRadioButton::Command (wxCommandEvent
& event
)
132 SetValue ( (event
.GetInt() != 0) );
133 ProcessCommand (event
);
136 void wxRadioButton::ChangeBackgroundColour()
138 wxWindow::ChangeBackgroundColour();
140 // What colour should this be?
141 wxColour colour
= *wxBLACK
;
142 int selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
144 XtVaSetValues ((Widget
) GetMainWidget(),
145 XmNselectColor
, selectPixel
,
149 void wxRadioButtonCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
150 XmToggleButtonCallbackStruct
* cbs
)
155 wxRadioButton
*item
= (wxRadioButton
*) clientData
;
156 if (item
->InSetValue())
159 //based on mac/radiobut.cpp
160 wxRadioButton
* old
= item
->ClearSelections();
161 item
->SetValue(true);
165 wxCommandEvent
event(wxEVT_COMMAND_RADIOBUTTON_SELECTED
,
167 event
.SetEventObject(old
);
168 event
.SetInt( false );
169 old
->ProcessCommand(event
);
171 wxCommandEvent
event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED
, item
->GetId() );
172 event2
.SetEventObject(item
);
173 event2
.SetInt( true );
174 item
->ProcessCommand(event2
);
177 wxRadioButton
* wxRadioButton::AddInCycle(wxRadioButton
*cycle
)
185 wxRadioButton
* current
= cycle
;
186 while ( current
->m_cycle
!= cycle
)
187 current
= current
->m_cycle
;
189 current
->m_cycle
= this;
195 wxRadioButton
* wxRadioButton::ClearSelections()
197 wxRadioButton
* cycle
= NextInCycle();
198 wxRadioButton
* old
= 0;
202 while (cycle
!= this)
204 if ( cycle
->GetValue() )
207 cycle
->SetValue(false);
209 cycle
= cycle
->NextInCycle();
216 void wxRadioButton::RemoveFromCycle()
218 wxRadioButton
* curr
= NextInCycle();
222 if( curr
->NextInCycle() == this )
224 curr
->m_cycle
= this->m_cycle
;
228 curr
= curr
->NextInCycle();