]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobut.cpp
Native spline drawing.
[wxWidgets.git] / src / motif / radiobut.cpp
CommitLineData
4bb6408c
JS
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
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4bb6408c
JS
13#pragma implementation "radiobut.h"
14#endif
15
1248b41f
MB
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
bcd055ae
JJ
19#ifdef __VMS
20#define XtDisplay XTDISPLAY
21#endif
22
f6045f99
GD
23#include "wx/defs.h"
24
4bb6408c 25#include "wx/radiobut.h"
a4294b78
JS
26#include "wx/utils.h"
27
338dd992
JJ
28#ifdef __VMS__
29#pragma message disable nosimpint
30#endif
a4294b78
JS
31#include <Xm/ToggleB.h>
32#include <Xm/ToggleBG.h>
338dd992
JJ
33#ifdef __VMS__
34#pragma message enable nosimpint
35#endif
a4294b78 36
3096bd2f 37#include "wx/motif/private.h"
a4294b78
JS
38
39void wxRadioButtonCallback (Widget w, XtPointer clientData,
2d120f83 40 XmToggleButtonCallbackStruct * cbs);
4bb6408c 41
4bb6408c 42IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
4bb6408c 43
a4294b78
JS
44wxRadioButton::wxRadioButton()
45{
a4294b78
JS
46}
47
4bb6408c 48bool wxRadioButton::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
49 const wxString& label,
50 const wxPoint& pos,
51 const wxSize& size, long style,
52 const wxValidator& validator,
53 const wxString& name)
4bb6408c 54{
f96b10c2
MB
55 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
56 return false;
31528cd3 57
a4294b78 58 Widget parentWidget = (Widget) parent->GetClientWidget();
73608949 59 Display* dpy = XtDisplay(parentWidget);
31528cd3 60
a4294b78 61 wxString label1(wxStripMenuCodes(label));
31528cd3 62
da494b40 63 wxXmString text( label1 );
31528cd3 64
a4294b78
JS
65 Widget radioButtonWidget = XtVaCreateManagedWidget ("toggle",
66#if wxUSE_GADGETS
2d120f83 67 xmToggleButtonGadgetClass, parentWidget,
a4294b78 68#else
2d120f83 69 xmToggleButtonWidgetClass, parentWidget,
a4294b78 70#endif
73608949 71 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
da494b40 72 XmNlabelString, text(),
2d120f83
JS
73 XmNfillOnSelect, True,
74 XmNindicatorType, XmONE_OF_MANY, // diamond-shape
75 NULL);
31528cd3 76
f96b10c2
MB
77 XtAddCallback (radioButtonWidget,
78 XmNvalueChangedCallback,
79 (XtCallbackProc)wxRadioButtonCallback,
80 (XtPointer)this);
31528cd3 81
a4294b78 82 m_mainWidget = (WXWidget) radioButtonWidget;
31528cd3 83
a4294b78 84 XtManageChild (radioButtonWidget);
31528cd3 85
f96b10c2
MB
86 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
87 pos.x, pos.y, size.x, size.y);
31528cd3 88
0d57be45 89 ChangeBackgroundColour();
31528cd3 90
96be256b 91 //copied from mac/radiobut.cpp (from here till "return true;")
f37907ff
MB
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;
ac32ba44 102 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
f37907ff
MB
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 }
96be256b 115 return true;
4bb6408c
JS
116}
117
118void wxRadioButton::SetValue(bool value)
119{
f37907ff
MB
120 if (GetValue() == value)
121 return;
122
96be256b
MB
123 m_inSetValue = true;
124 XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, False);
125 m_inSetValue = false;
f37907ff
MB
126
127 ClearSelections();
4bb6408c
JS
128}
129
130// Get single selection, for single choice list items
131bool wxRadioButton::GetValue() const
132{
a4294b78 133 return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0);
4bb6408c
JS
134}
135
136void wxRadioButton::Command (wxCommandEvent & event)
137{
687706f5 138 SetValue ( (event.GetInt() != 0) );
2d120f83 139 ProcessCommand (event);
4bb6408c
JS
140}
141
0d57be45
JS
142void wxRadioButton::ChangeBackgroundColour()
143{
321db4b6 144 wxWindow::ChangeBackgroundColour();
9838df2c
JS
145
146 // What colour should this be?
eb6fa4b4 147 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c
JS
148
149 XtVaSetValues ((Widget) GetMainWidget(),
150 XmNselectColor, selectPixel,
151 NULL);
0d57be45
JS
152}
153
a4294b78 154void wxRadioButtonCallback (Widget w, XtPointer clientData,
2d120f83 155 XmToggleButtonCallbackStruct * cbs)
a4294b78 156{
2d120f83
JS
157 if (!cbs->set)
158 return;
31528cd3 159
2d120f83
JS
160 wxRadioButton *item = (wxRadioButton *) clientData;
161 if (item->InSetValue())
162 return;
31528cd3 163
f37907ff
MB
164 //based on mac/radiobut.cpp
165 wxRadioButton* old = item->ClearSelections();
96be256b 166 item->SetValue(true);
f37907ff
MB
167
168 if ( old )
169 {
170 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED,
171 old->GetId() );
172 event.SetEventObject(old);
96be256b 173 event.SetInt( false );
f37907ff
MB
174 old->ProcessCommand(event);
175 }
176 wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() );
177 event2.SetEventObject(item);
96be256b 178 event2.SetInt( true );
f37907ff
MB
179 item->ProcessCommand(event2);
180}
181
182wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle)
183{
184 wxRadioButton* next;
185 wxRadioButton* current;
186
187 if (cycle == NULL)
188 {
189 m_cycle = this;
190 return this;
191 }
192 else
193 {
194 current = cycle;
195 while ((next = current->m_cycle) != cycle)
196 current = current->m_cycle;
197 m_cycle = cycle;
198 current->m_cycle = this;
199 return cycle;
200 }
201}
31528cd3 202
f37907ff
MB
203wxRadioButton* wxRadioButton::ClearSelections()
204{
205 wxRadioButton* cycle = NextInCycle();
206 wxRadioButton* old = 0;
207
208 if (cycle)
209 {
210 while (cycle != this)
211 {
212 if ( cycle->GetValue() )
213 {
214 old = cycle;
96be256b 215 cycle->SetValue(false);
f37907ff
MB
216 }
217 cycle = cycle->NextInCycle();
218 }
219 }
220
221 return old;
a4294b78 222}
4bb6408c 223
f37907ff
MB
224void wxRadioButton::RemoveFromCycle()
225{
226 wxRadioButton* curr = NextInCycle();
227
228 while( curr )
229 {
230 if( curr->NextInCycle() == this )
231 {
232 curr->m_cycle = this->m_cycle;
233 return;
234 }
235
236 curr = curr->NextInCycle();
237 }
238}