]> git.saurik.com Git - wxWidgets.git/blob - src/motif/radiobut.cpp
64 bit compilation fix (pointer can't be cast to int); code simplification (don't...
[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 if (cycle == NULL)
185 {
186 m_cycle = this;
187 }
188 else
189 {
190 wxRadioButton* current = cycle;
191 while ( current->m_cycle != cycle )
192 current = current->m_cycle;
193 m_cycle = cycle;
194 current->m_cycle = this;
195 }
196
197 return cycle;
198 }
199
200 wxRadioButton* wxRadioButton::ClearSelections()
201 {
202 wxRadioButton* cycle = NextInCycle();
203 wxRadioButton* old = 0;
204
205 if (cycle)
206 {
207 while (cycle != this)
208 {
209 if ( cycle->GetValue() )
210 {
211 old = cycle;
212 cycle->SetValue(false);
213 }
214 cycle = cycle->NextInCycle();
215 }
216 }
217
218 return old;
219 }
220
221 void wxRadioButton::RemoveFromCycle()
222 {
223 wxRadioButton* curr = NextInCycle();
224
225 while( curr )
226 {
227 if( curr->NextInCycle() == this )
228 {
229 curr->m_cycle = this->m_cycle;
230 return;
231 }
232
233 curr = curr->NextInCycle();
234 }
235 }