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