Fix crash on exit with Lesstif (and possibly Motif 1.x).
[wxWidgets.git] / src / motif / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: radiobut.cpp
3 // Purpose: wxRadioButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "radiobut.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __VMS
20 #define XtDisplay XTDISPLAY
21 #endif
22
23 #include "wx/defs.h"
24
25 #include "wx/radiobut.h"
26 #include "wx/utils.h"
27
28 #ifdef __VMS__
29 #pragma message disable nosimpint
30 #endif
31 #include <Xm/ToggleB.h>
32 #include <Xm/ToggleBG.h>
33 #ifdef __VMS__
34 #pragma message enable nosimpint
35 #endif
36
37 #include "wx/motif/private.h"
38
39 void wxRadioButtonCallback (Widget w, XtPointer clientData,
40 XmToggleButtonCallbackStruct * cbs);
41
42 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
43
44 wxRadioButton::wxRadioButton()
45 {
46 }
47
48 bool wxRadioButton::Create(wxWindow *parent, wxWindowID id,
49 const wxString& label,
50 const wxPoint& pos,
51 const wxSize& size, long style,
52 const wxValidator& validator,
53 const wxString& name)
54 {
55 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
56 return false;
57
58 Widget parentWidget = (Widget) parent->GetClientWidget();
59 Display* dpy = XtDisplay(parentWidget);
60
61 wxString label1(wxStripMenuCodes(label));
62
63 wxXmString text( label1 );
64
65 Widget radioButtonWidget = XtVaCreateManagedWidget ("toggle",
66 #if wxUSE_GADGETS
67 xmToggleButtonGadgetClass, parentWidget,
68 #else
69 xmToggleButtonWidgetClass, parentWidget,
70 #endif
71 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
72 XmNlabelString, text(),
73 XmNfillOnSelect, True,
74 XmNindicatorType, XmONE_OF_MANY, // diamond-shape
75 NULL);
76
77 XtAddCallback (radioButtonWidget,
78 XmNvalueChangedCallback,
79 (XtCallbackProc)wxRadioButtonCallback,
80 (XtPointer)this);
81
82 m_mainWidget = (WXWidget) radioButtonWidget;
83
84 XtManageChild (radioButtonWidget);
85
86 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
87 pos.x, pos.y, size.x, size.y);
88
89 ChangeBackgroundColour();
90
91 //copied from mac/radiobut.cpp (from here till "return true;")
92 m_cycle = this ;
93
94 if (HasFlag(wxRB_GROUP))
95 {
96 AddInCycle( NULL ) ;
97 }
98 else
99 {
100 /* search backward for last group start */
101 wxRadioButton *chief = (wxRadioButton*) NULL;
102 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
103 while (node)
104 {
105 wxWindow *child = node->GetData();
106 if (child->IsKindOf( CLASSINFO( wxRadioButton ) ) )
107 {
108 chief = (wxRadioButton*) child;
109 if (child->HasFlag(wxRB_GROUP)) break;
110 }
111 node = node->GetPrevious();
112 }
113 AddInCycle( chief ) ;
114 }
115 return true;
116 }
117
118 void wxRadioButton::SetValue(bool value)
119 {
120 if (GetValue() == value)
121 return;
122
123 m_inSetValue = true;
124 XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, False);
125 m_inSetValue = false;
126
127 ClearSelections();
128 }
129
130 // Get single selection, for single choice list items
131 bool wxRadioButton::GetValue() const
132 {
133 return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0);
134 }
135
136 void wxRadioButton::Command (wxCommandEvent & event)
137 {
138 SetValue ( (event.GetInt() != 0) );
139 ProcessCommand (event);
140 }
141
142 void wxRadioButton::ChangeBackgroundColour()
143 {
144 wxWindow::ChangeBackgroundColour();
145
146 // What colour should this be?
147 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
148
149 XtVaSetValues ((Widget) GetMainWidget(),
150 XmNselectColor, selectPixel,
151 NULL);
152 }
153
154 void wxRadioButtonCallback (Widget w, XtPointer clientData,
155 XmToggleButtonCallbackStruct * cbs)
156 {
157 if (!cbs->set)
158 return;
159
160 wxRadioButton *item = (wxRadioButton *) clientData;
161 if (item->InSetValue())
162 return;
163
164 //based on mac/radiobut.cpp
165 wxRadioButton* old = item->ClearSelections();
166 item->SetValue(true);
167
168 if ( old )
169 {
170 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED,
171 old->GetId() );
172 event.SetEventObject(old);
173 event.SetInt( false );
174 old->ProcessCommand(event);
175 }
176 wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() );
177 event2.SetEventObject(item);
178 event2.SetInt( true );
179 item->ProcessCommand(event2);
180 }
181
182 wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle)
183 {
184 wxRadioButton* next;
185 wxRadioButton* current;
186
187 if (cycle == NULL)
188 {
189 m_cycle = this;
190 return this;
191 }
192 else
193 {
194 current = cycle;
195 while ((next = current->m_cycle) != cycle)
196 current = current->m_cycle;
197 m_cycle = cycle;
198 current->m_cycle = this;
199 return cycle;
200 }
201 }
202
203 wxRadioButton* wxRadioButton::ClearSelections()
204 {
205 wxRadioButton* cycle = NextInCycle();
206 wxRadioButton* old = 0;
207
208 if (cycle)
209 {
210 while (cycle != this)
211 {
212 if ( cycle->GetValue() )
213 {
214 old = cycle;
215 cycle->SetValue(false);
216 }
217 cycle = cycle->NextInCycle();
218 }
219 }
220
221 return old;
222 }
223
224 void wxRadioButton::RemoveFromCycle()
225 {
226 wxRadioButton* curr = NextInCycle();
227
228 while( curr )
229 {
230 if( curr->NextInCycle() == this )
231 {
232 curr->m_cycle = this->m_cycle;
233 return;
234 }
235
236 curr = curr->NextInCycle();
237 }
238 }