added support for gcc precompiled headers
[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 #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 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
99
100 ChangeBackgroundColour();
101
102 //copied from mac/radiobut.cpp (from here till "return TRUE;")
103 m_cycle = this ;
104
105 if (HasFlag(wxRB_GROUP))
106 {
107 AddInCycle( NULL ) ;
108 }
109 else
110 {
111 /* search backward for last group start */
112 wxRadioButton *chief = (wxRadioButton*) NULL;
113 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
114 while (node)
115 {
116 wxWindow *child = node->GetData();
117 if (child->IsKindOf( CLASSINFO( wxRadioButton ) ) )
118 {
119 chief = (wxRadioButton*) child;
120 if (child->HasFlag(wxRB_GROUP)) break;
121 }
122 node = node->GetPrevious();
123 }
124 AddInCycle( chief ) ;
125 }
126 return TRUE;
127 }
128
129 void wxRadioButton::SetValue(bool value)
130 {
131 if (GetValue() == value)
132 return;
133
134 m_inSetValue = TRUE;
135 XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, FALSE);
136 m_inSetValue = FALSE;
137
138 ClearSelections();
139 }
140
141 // Get single selection, for single choice list items
142 bool wxRadioButton::GetValue() const
143 {
144 return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0);
145 }
146
147 void wxRadioButton::Command (wxCommandEvent & event)
148 {
149 SetValue ( (event.m_commandInt != 0) );
150 ProcessCommand (event);
151 }
152
153 void wxRadioButton::ChangeFont(bool keepOriginalSize)
154 {
155 wxWindow::ChangeFont(keepOriginalSize);
156 }
157
158 void wxRadioButton::ChangeBackgroundColour()
159 {
160 wxWindow::ChangeBackgroundColour();
161
162 // What colour should this be?
163 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
164
165 XtVaSetValues ((Widget) GetMainWidget(),
166 XmNselectColor, selectPixel,
167 NULL);
168 }
169
170 void wxRadioButton::ChangeForegroundColour()
171 {
172 wxWindow::ChangeForegroundColour();
173 }
174
175 void wxRadioButtonCallback (Widget w, XtPointer clientData,
176 XmToggleButtonCallbackStruct * cbs)
177 {
178 if (!cbs->set)
179 return;
180
181 wxRadioButton *item = (wxRadioButton *) clientData;
182 if (item->InSetValue())
183 return;
184
185 //based on mac/radiobut.cpp
186 wxRadioButton* old = item->ClearSelections();
187 item->SetValue(TRUE);
188
189 if ( old )
190 {
191 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED,
192 old->GetId() );
193 event.SetEventObject(old);
194 event.SetInt( FALSE );
195 old->ProcessCommand(event);
196 }
197 wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() );
198 event2.SetEventObject(item);
199 event2.SetInt( TRUE );
200 item->ProcessCommand(event2);
201 }
202
203 wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle)
204 {
205 wxRadioButton* next;
206 wxRadioButton* current;
207
208 if (cycle == NULL)
209 {
210 m_cycle = this;
211 return this;
212 }
213 else
214 {
215 current = cycle;
216 while ((next = current->m_cycle) != cycle)
217 current = current->m_cycle;
218 m_cycle = cycle;
219 current->m_cycle = this;
220 return cycle;
221 }
222 }
223
224 wxRadioButton* wxRadioButton::ClearSelections()
225 {
226 wxRadioButton* cycle = NextInCycle();
227 wxRadioButton* old = 0;
228
229 if (cycle)
230 {
231 while (cycle != this)
232 {
233 if ( cycle->GetValue() )
234 {
235 old = cycle;
236 cycle->SetValue(FALSE);
237 }
238 cycle = cycle->NextInCycle();
239 }
240 }
241
242 return old;
243 }
244
245 void wxRadioButton::RemoveFromCycle()
246 {
247 wxRadioButton* curr = NextInCycle();
248
249 while( curr )
250 {
251 if( curr->NextInCycle() == this )
252 {
253 curr->m_cycle = this->m_cycle;
254 return;
255 }
256
257 curr = curr->NextInCycle();
258 }
259 }