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();
59 Display
* dpy
= XtDisplay(parentWidget
);
61 wxString
label1(wxStripMenuCodes(label
));
63 wxXmString
text( label1
);
65 Widget radioButtonWidget
= XtVaCreateManagedWidget ("toggle",
67 xmToggleButtonGadgetClass
, parentWidget
,
69 xmToggleButtonWidgetClass
, parentWidget
,
71 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
72 XmNlabelString
, text(),
73 XmNfillOnSelect
, True
,
74 XmNindicatorType
, XmONE_OF_MANY
, // diamond-shape
77 XtAddCallback (radioButtonWidget
,
78 XmNvalueChangedCallback
,
79 (XtCallbackProc
)wxRadioButtonCallback
,
82 m_mainWidget
= (WXWidget
) radioButtonWidget
;
84 XtManageChild (radioButtonWidget
);
86 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
87 pos
.x
, pos
.y
, size
.x
, size
.y
);
89 ChangeBackgroundColour();
91 //copied from mac/radiobut.cpp (from here till "return true;")
94 if (HasFlag(wxRB_GROUP
))
100 /* search backward for last group start */
101 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
102 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
105 wxWindow
*child
= node
->GetData();
106 if (child
->IsKindOf( CLASSINFO( wxRadioButton
) ) )
108 chief
= (wxRadioButton
*) child
;
109 if (child
->HasFlag(wxRB_GROUP
)) break;
111 node
= node
->GetPrevious();
113 AddInCycle( chief
) ;
118 void wxRadioButton::SetValue(bool value
)
120 if (GetValue() == value
)
124 XmToggleButtonSetState ((Widget
) m_mainWidget
, (Boolean
) value
, False
);
125 m_inSetValue
= false;
130 // Get single selection, for single choice list items
131 bool wxRadioButton::GetValue() const
133 return (XmToggleButtonGetState ((Widget
) m_mainWidget
) != 0);
136 void wxRadioButton::Command (wxCommandEvent
& event
)
138 SetValue ( (event
.GetInt() != 0) );
139 ProcessCommand (event
);
142 void wxRadioButton::ChangeBackgroundColour()
144 wxWindow::ChangeBackgroundColour();
146 // What colour should this be?
147 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
149 XtVaSetValues ((Widget
) GetMainWidget(),
150 XmNselectColor
, selectPixel
,
154 void wxRadioButtonCallback (Widget w
, XtPointer clientData
,
155 XmToggleButtonCallbackStruct
* cbs
)
160 wxRadioButton
*item
= (wxRadioButton
*) clientData
;
161 if (item
->InSetValue())
164 //based on mac/radiobut.cpp
165 wxRadioButton
* old
= item
->ClearSelections();
166 item
->SetValue(true);
170 wxCommandEvent
event(wxEVT_COMMAND_RADIOBUTTON_SELECTED
,
172 event
.SetEventObject(old
);
173 event
.SetInt( false );
174 old
->ProcessCommand(event
);
176 wxCommandEvent
event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED
, item
->GetId() );
177 event2
.SetEventObject(item
);
178 event2
.SetInt( true );
179 item
->ProcessCommand(event2
);
182 wxRadioButton
* wxRadioButton::AddInCycle(wxRadioButton
*cycle
)
185 wxRadioButton
* current
;
195 while ((next
= current
->m_cycle
) != cycle
)
196 current
= current
->m_cycle
;
198 current
->m_cycle
= this;
203 wxRadioButton
* wxRadioButton::ClearSelections()
205 wxRadioButton
* cycle
= NextInCycle();
206 wxRadioButton
* old
= 0;
210 while (cycle
!= this)
212 if ( cycle
->GetValue() )
215 cycle
->SetValue(false);
217 cycle
= cycle
->NextInCycle();
224 void wxRadioButton::RemoveFromCycle()
226 wxRadioButton
* curr
= NextInCycle();
230 if( curr
->NextInCycle() == this )
232 curr
->m_cycle
= this->m_cycle
;
236 curr
= curr
->NextInCycle();