]> git.saurik.com Git - wxWidgets.git/blob - src/motif/radiobut.cpp
Added ability to execute more code after the widget is created
[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
60 wxString label1(wxStripMenuCodes(label));
61
62 wxXmString text( label1 );
63
64 WXFontType fontType = m_font.GetFontType(XtDisplay(parentWidget));
65
66 Widget radioButtonWidget = XtVaCreateManagedWidget ("toggle",
67 #if wxUSE_GADGETS
68 xmToggleButtonGadgetClass, parentWidget,
69 #else
70 xmToggleButtonWidgetClass, parentWidget,
71 #endif
72 wxFont::GetFontTag(), fontType,
73 XmNlabelString, text(),
74 XmNfillOnSelect, True,
75 XmNindicatorType, XmONE_OF_MANY, // diamond-shape
76 NULL);
77
78 XtAddCallback (radioButtonWidget,
79 XmNvalueChangedCallback,
80 (XtCallbackProc)wxRadioButtonCallback,
81 (XtPointer)this);
82
83 m_mainWidget = (WXWidget) radioButtonWidget;
84
85 XtManageChild (radioButtonWidget);
86
87 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
88 pos.x, pos.y, size.x, size.y);
89
90 ChangeBackgroundColour();
91
92 //copied from mac/radiobut.cpp (from here till "return TRUE;")
93 m_cycle = this ;
94
95 if (HasFlag(wxRB_GROUP))
96 {
97 AddInCycle( NULL ) ;
98 }
99 else
100 {
101 /* search backward for last group start */
102 wxRadioButton *chief = (wxRadioButton*) NULL;
103 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
104 while (node)
105 {
106 wxWindow *child = node->GetData();
107 if (child->IsKindOf( CLASSINFO( wxRadioButton ) ) )
108 {
109 chief = (wxRadioButton*) child;
110 if (child->HasFlag(wxRB_GROUP)) break;
111 }
112 node = node->GetPrevious();
113 }
114 AddInCycle( chief ) ;
115 }
116 return TRUE;
117 }
118
119 void wxRadioButton::SetValue(bool value)
120 {
121 if (GetValue() == value)
122 return;
123
124 m_inSetValue = TRUE;
125 XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, FALSE);
126 m_inSetValue = FALSE;
127
128 ClearSelections();
129 }
130
131 // Get single selection, for single choice list items
132 bool wxRadioButton::GetValue() const
133 {
134 return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0);
135 }
136
137 void wxRadioButton::Command (wxCommandEvent & event)
138 {
139 SetValue ( (event.m_commandInt != 0) );
140 ProcessCommand (event);
141 }
142
143 void wxRadioButton::ChangeBackgroundColour()
144 {
145 wxWindow::ChangeBackgroundColour();
146
147 // What colour should this be?
148 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
149
150 XtVaSetValues ((Widget) GetMainWidget(),
151 XmNselectColor, selectPixel,
152 NULL);
153 }
154
155 void wxRadioButtonCallback (Widget w, XtPointer clientData,
156 XmToggleButtonCallbackStruct * cbs)
157 {
158 if (!cbs->set)
159 return;
160
161 wxRadioButton *item = (wxRadioButton *) clientData;
162 if (item->InSetValue())
163 return;
164
165 //based on mac/radiobut.cpp
166 wxRadioButton* old = item->ClearSelections();
167 item->SetValue(TRUE);
168
169 if ( old )
170 {
171 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED,
172 old->GetId() );
173 event.SetEventObject(old);
174 event.SetInt( FALSE );
175 old->ProcessCommand(event);
176 }
177 wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() );
178 event2.SetEventObject(item);
179 event2.SetInt( TRUE );
180 item->ProcessCommand(event2);
181 }
182
183 wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle)
184 {
185 wxRadioButton* next;
186 wxRadioButton* current;
187
188 if (cycle == NULL)
189 {
190 m_cycle = this;
191 return this;
192 }
193 else
194 {
195 current = cycle;
196 while ((next = current->m_cycle) != cycle)
197 current = current->m_cycle;
198 m_cycle = cycle;
199 current->m_cycle = this;
200 return cycle;
201 }
202 }
203
204 wxRadioButton* wxRadioButton::ClearSelections()
205 {
206 wxRadioButton* cycle = NextInCycle();
207 wxRadioButton* old = 0;
208
209 if (cycle)
210 {
211 while (cycle != this)
212 {
213 if ( cycle->GetValue() )
214 {
215 old = cycle;
216 cycle->SetValue(FALSE);
217 }
218 cycle = cycle->NextInCycle();
219 }
220 }
221
222 return old;
223 }
224
225 void wxRadioButton::RemoveFromCycle()
226 {
227 wxRadioButton* curr = NextInCycle();
228
229 while( curr )
230 {
231 if( curr->NextInCycle() == this )
232 {
233 curr->m_cycle = this->m_cycle;
234 return;
235 }
236
237 curr = curr->NextInCycle();
238 }
239 }