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