Added new __WXMOTIF20__ macro, which is true if the Motif version
[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 #ifdef __GNUG__
13 #pragma implementation "radiobut.h"
14 #endif
15
16 #ifdef __VMS
17 #define XtDisplay XTDISPLAY
18 #endif
19
20 #include "wx/defs.h"
21
22 #include "wx/radiobut.h"
23 #include "wx/utils.h"
24
25 #ifdef __VMS__
26 #pragma message disable nosimpint
27 #endif
28 #include <Xm/Label.h>
29 #include <Xm/LabelG.h>
30 #include <Xm/ToggleB.h>
31 #include <Xm/ToggleBG.h>
32 #include <Xm/RowColumn.h>
33 #include <Xm/Form.h>
34 #ifdef __VMS__
35 #pragma message enable nosimpint
36 #endif
37
38 #include "wx/motif/private.h"
39
40 void wxRadioButtonCallback (Widget w, XtPointer clientData,
41 XmToggleButtonCallbackStruct * cbs);
42
43 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
44
45 wxRadioButton::wxRadioButton()
46 {
47 }
48
49 bool wxRadioButton::Create(wxWindow *parent, wxWindowID id,
50 const wxString& label,
51 const wxPoint& pos,
52 const wxSize& size, long style,
53 const wxValidator& validator,
54 const wxString& name)
55 {
56 SetName(name);
57 SetValidator(validator);
58 m_backgroundColour = parent->GetBackgroundColour();
59 m_foregroundColour = parent->GetForegroundColour();
60 m_font = parent->GetFont();
61
62 if (parent) parent->AddChild(this);
63
64 if ( id == -1 )
65 m_windowId = (int)NewControlId();
66 else
67 m_windowId = id;
68
69 m_windowStyle = style ;
70
71 Widget parentWidget = (Widget) parent->GetClientWidget();
72
73 wxString label1(wxStripMenuCodes(label));
74
75 wxXmString text( label1 );
76
77 WXFontType fontType = m_font.GetFontType(XtDisplay(parentWidget));
78
79 Widget radioButtonWidget = XtVaCreateManagedWidget ("toggle",
80 #if wxUSE_GADGETS
81 xmToggleButtonGadgetClass, parentWidget,
82 #else
83 xmToggleButtonWidgetClass, parentWidget,
84 #endif
85 wxFont::GetFontTag(), fontType,
86 XmNlabelString, text(),
87 XmNfillOnSelect, True,
88 XmNindicatorType, XmONE_OF_MANY, // diamond-shape
89 NULL);
90
91 XtAddCallback (radioButtonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxRadioButtonCallback,
92 (XtPointer) this);
93
94 m_mainWidget = (WXWidget) radioButtonWidget;
95
96 XtManageChild (radioButtonWidget);
97
98 SetCanAddEventHandler(TRUE);
99 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
100
101 ChangeBackgroundColour();
102
103 //copied from mac/radiobut.cpp (from here till "return TRUE;")
104 m_cycle = this ;
105
106 if (HasFlag(wxRB_GROUP))
107 {
108 AddInCycle( NULL ) ;
109 }
110 else
111 {
112 /* search backward for last group start */
113 wxRadioButton *chief = (wxRadioButton*) NULL;
114 wxWindowList::Node *node = parent->GetChildren().GetLast();
115 while (node)
116 {
117 wxWindow *child = node->GetData();
118 if (child->IsKindOf( CLASSINFO( wxRadioButton ) ) )
119 {
120 chief = (wxRadioButton*) child;
121 if (child->HasFlag(wxRB_GROUP)) break;
122 }
123 node = node->GetPrevious();
124 }
125 AddInCycle( chief ) ;
126 }
127 return TRUE;
128 }
129
130 void wxRadioButton::SetValue(bool value)
131 {
132 if (GetValue() == value)
133 return;
134
135 m_inSetValue = TRUE;
136 XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, FALSE);
137 m_inSetValue = FALSE;
138
139 ClearSelections();
140 }
141
142 // Get single selection, for single choice list items
143 bool wxRadioButton::GetValue() const
144 {
145 return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0);
146 }
147
148 void wxRadioButton::Command (wxCommandEvent & event)
149 {
150 SetValue ( (event.m_commandInt != 0) );
151 ProcessCommand (event);
152 }
153
154 void wxRadioButton::ChangeFont(bool keepOriginalSize)
155 {
156 wxWindow::ChangeFont(keepOriginalSize);
157 }
158
159 void wxRadioButton::ChangeBackgroundColour()
160 {
161 wxWindow::ChangeBackgroundColour();
162
163 // What colour should this be?
164 int selectPixel = wxBLACK->AllocColour(wxGetDisplay());
165
166 XtVaSetValues ((Widget) GetMainWidget(),
167 XmNselectColor, selectPixel,
168 NULL);
169 }
170
171 void wxRadioButton::ChangeForegroundColour()
172 {
173 wxWindow::ChangeForegroundColour();
174 }
175
176 void wxRadioButtonCallback (Widget w, XtPointer clientData,
177 XmToggleButtonCallbackStruct * cbs)
178 {
179 if (!cbs->set)
180 return;
181
182 wxRadioButton *item = (wxRadioButton *) clientData;
183 if (item->InSetValue())
184 return;
185
186 //based on mac/radiobut.cpp
187 wxRadioButton* old = item->ClearSelections();
188 item->SetValue(TRUE);
189
190 if ( old )
191 {
192 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED,
193 old->GetId() );
194 event.SetEventObject(old);
195 event.SetInt( FALSE );
196 old->ProcessCommand(event);
197 }
198 wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() );
199 event2.SetEventObject(item);
200 event2.SetInt( TRUE );
201 item->ProcessCommand(event2);
202 }
203
204 wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle)
205 {
206 wxRadioButton* next;
207 wxRadioButton* current;
208
209 if (cycle == NULL)
210 {
211 m_cycle = this;
212 return this;
213 }
214 else
215 {
216 current = cycle;
217 while ((next = current->m_cycle) != cycle)
218 current = current->m_cycle;
219 m_cycle = cycle;
220 current->m_cycle = this;
221 return cycle;
222 }
223 }
224
225 wxRadioButton* wxRadioButton::ClearSelections()
226 {
227 wxRadioButton* cycle = NextInCycle();
228 wxRadioButton* old = 0;
229
230 if (cycle)
231 {
232 while (cycle != this)
233 {
234 if ( cycle->GetValue() )
235 {
236 old = cycle;
237 cycle->SetValue(FALSE);
238 }
239 cycle = cycle->NextInCycle();
240 }
241 }
242
243 return old;
244 }
245
246 void wxRadioButton::RemoveFromCycle()
247 {
248 wxRadioButton* curr = NextInCycle();
249
250 while( curr )
251 {
252 if( curr->NextInCycle() == this )
253 {
254 curr->m_cycle = this->m_cycle;
255 return;
256 }
257
258 curr = curr->NextInCycle();
259 }
260 }