create stock GDI objects on demand; use const with GDI objects appropriately (patch...
[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 wxColour colour = *wxBLACK;
144 int selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
145
146 XtVaSetValues ((Widget) GetMainWidget(),
147 XmNselectColor, selectPixel,
148 NULL);
149 }
150
151 void wxRadioButtonCallback (Widget WXUNUSED(w), XtPointer clientData,
152 XmToggleButtonCallbackStruct * cbs)
153 {
154 if (!cbs->set)
155 return;
156
157 wxRadioButton *item = (wxRadioButton *) clientData;
158 if (item->InSetValue())
159 return;
160
161 //based on mac/radiobut.cpp
162 wxRadioButton* old = item->ClearSelections();
163 item->SetValue(true);
164
165 if ( old )
166 {
167 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED,
168 old->GetId() );
169 event.SetEventObject(old);
170 event.SetInt( false );
171 old->ProcessCommand(event);
172 }
173 wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() );
174 event2.SetEventObject(item);
175 event2.SetInt( true );
176 item->ProcessCommand(event2);
177 }
178
179 wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle)
180 {
181 if (cycle == NULL)
182 {
183 m_cycle = this;
184 }
185 else
186 {
187 wxRadioButton* current = cycle;
188 while ( current->m_cycle != cycle )
189 current = current->m_cycle;
190 m_cycle = cycle;
191 current->m_cycle = this;
192 }
193
194 return cycle;
195 }
196
197 wxRadioButton* wxRadioButton::ClearSelections()
198 {
199 wxRadioButton* cycle = NextInCycle();
200 wxRadioButton* old = 0;
201
202 if (cycle)
203 {
204 while (cycle != this)
205 {
206 if ( cycle->GetValue() )
207 {
208 old = cycle;
209 cycle->SetValue(false);
210 }
211 cycle = cycle->NextInCycle();
212 }
213 }
214
215 return old;
216 }
217
218 void wxRadioButton::RemoveFromCycle()
219 {
220 wxRadioButton* curr = NextInCycle();
221
222 while( curr )
223 {
224 if( curr->NextInCycle() == this )
225 {
226 curr->m_cycle = this->m_cycle;
227 return;
228 }
229
230 curr = curr->NextInCycle();
231 }
232 }