]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobut.cpp
don't use deprecated toolbar API
[wxWidgets.git] / src / motif / radiobut.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
355b4d3d 2// Name: src/motif/radiobut.cpp
4bb6408c
JS
3// Purpose: wxRadioButton
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
355b4d3d 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
4bb6408c 15#include "wx/radiobut.h"
de6185e2
WS
16
17#ifndef WX_PRECOMP
18 #include "wx/utils.h"
19#endif
a4294b78 20
338dd992
JJ
21#ifdef __VMS__
22#pragma message disable nosimpint
23#endif
a4294b78
JS
24#include <Xm/ToggleB.h>
25#include <Xm/ToggleBG.h>
338dd992
JJ
26#ifdef __VMS__
27#pragma message enable nosimpint
28#endif
a4294b78 29
3096bd2f 30#include "wx/motif/private.h"
a4294b78
JS
31
32void wxRadioButtonCallback (Widget w, XtPointer clientData,
2d120f83 33 XmToggleButtonCallbackStruct * cbs);
4bb6408c 34
4bb6408c 35IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
4bb6408c 36
a4294b78
JS
37wxRadioButton::wxRadioButton()
38{
a4294b78
JS
39}
40
4bb6408c 41bool wxRadioButton::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
42 const wxString& label,
43 const wxPoint& pos,
44 const wxSize& size, long style,
45 const wxValidator& validator,
46 const wxString& name)
4bb6408c 47{
f96b10c2
MB
48 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
49 return false;
105fbe1f 50 PreCreation();
31528cd3 51
a4294b78 52 Widget parentWidget = (Widget) parent->GetClientWidget();
73608949 53 Display* dpy = XtDisplay(parentWidget);
31528cd3 54
32cd189d 55 wxString label1(GetLabelText(label));
31528cd3 56
da494b40 57 wxXmString text( label1 );
31528cd3 58
a4294b78
JS
59 Widget radioButtonWidget = XtVaCreateManagedWidget ("toggle",
60#if wxUSE_GADGETS
2d120f83 61 xmToggleButtonGadgetClass, parentWidget,
a4294b78 62#else
2d120f83 63 xmToggleButtonWidgetClass, parentWidget,
a4294b78 64#endif
73608949 65 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
da494b40 66 XmNlabelString, text(),
2d120f83
JS
67 XmNfillOnSelect, True,
68 XmNindicatorType, XmONE_OF_MANY, // diamond-shape
69 NULL);
31528cd3 70
f96b10c2
MB
71 XtAddCallback (radioButtonWidget,
72 XmNvalueChangedCallback,
73 (XtCallbackProc)wxRadioButtonCallback,
74 (XtPointer)this);
31528cd3 75
a4294b78 76 m_mainWidget = (WXWidget) radioButtonWidget;
31528cd3 77
a4294b78 78 XtManageChild (radioButtonWidget);
31528cd3 79
105fbe1f 80 PostCreation();
f96b10c2
MB
81 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
82 pos.x, pos.y, size.x, size.y);
31528cd3 83
96be256b 84 //copied from mac/radiobut.cpp (from here till "return true;")
f37907ff 85 m_cycle = this ;
002ceb38 86
f37907ff
MB
87 if (HasFlag(wxRB_GROUP))
88 {
89 AddInCycle( NULL ) ;
90 }
91 else
92 {
93 /* search backward for last group start */
94 wxRadioButton *chief = (wxRadioButton*) NULL;
ac32ba44 95 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
f37907ff
MB
96 while (node)
97 {
98 wxWindow *child = node->GetData();
99 if (child->IsKindOf( CLASSINFO( wxRadioButton ) ) )
100 {
101 chief = (wxRadioButton*) child;
102 if (child->HasFlag(wxRB_GROUP)) break;
103 }
104 node = node->GetPrevious();
105 }
106 AddInCycle( chief ) ;
107 }
96be256b 108 return true;
4bb6408c
JS
109}
110
111void wxRadioButton::SetValue(bool value)
112{
f37907ff
MB
113 if (GetValue() == value)
114 return;
115
96be256b
MB
116 m_inSetValue = true;
117 XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, False);
118 m_inSetValue = false;
f37907ff
MB
119
120 ClearSelections();
4bb6408c
JS
121}
122
123// Get single selection, for single choice list items
124bool wxRadioButton::GetValue() const
125{
a4294b78 126 return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0);
4bb6408c
JS
127}
128
129void wxRadioButton::Command (wxCommandEvent & event)
130{
687706f5 131 SetValue ( (event.GetInt() != 0) );
2d120f83 132 ProcessCommand (event);
4bb6408c
JS
133}
134
0d57be45
JS
135void wxRadioButton::ChangeBackgroundColour()
136{
321db4b6 137 wxWindow::ChangeBackgroundColour();
9838df2c
JS
138
139 // What colour should this be?
f516d986 140 wxColour colour = *wxBLACK;
3e0071d9 141 WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c
JS
142
143 XtVaSetValues ((Widget) GetMainWidget(),
144 XmNselectColor, selectPixel,
145 NULL);
0d57be45
JS
146}
147
355b4d3d 148void wxRadioButtonCallback (Widget WXUNUSED(w), XtPointer clientData,
2d120f83 149 XmToggleButtonCallbackStruct * cbs)
a4294b78 150{
2d120f83
JS
151 if (!cbs->set)
152 return;
31528cd3 153
2d120f83
JS
154 wxRadioButton *item = (wxRadioButton *) clientData;
155 if (item->InSetValue())
156 return;
31528cd3 157
f37907ff
MB
158 //based on mac/radiobut.cpp
159 wxRadioButton* old = item->ClearSelections();
96be256b 160 item->SetValue(true);
f37907ff
MB
161
162 if ( old )
163 {
164 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED,
165 old->GetId() );
166 event.SetEventObject(old);
96be256b 167 event.SetInt( false );
f37907ff
MB
168 old->ProcessCommand(event);
169 }
170 wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() );
171 event2.SetEventObject(item);
96be256b 172 event2.SetInt( true );
f37907ff
MB
173 item->ProcessCommand(event2);
174}
175
176wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle)
177{
f37907ff
MB
178 if (cycle == NULL)
179 {
180 m_cycle = this;
f37907ff
MB
181 }
182 else
183 {
002ceb38
VZ
184 wxRadioButton* current = cycle;
185 while ( current->m_cycle != cycle )
f37907ff
MB
186 current = current->m_cycle;
187 m_cycle = cycle;
188 current->m_cycle = this;
f37907ff 189 }
002ceb38
VZ
190
191 return cycle;
f37907ff 192}
31528cd3 193
f37907ff
MB
194wxRadioButton* wxRadioButton::ClearSelections()
195{
196 wxRadioButton* cycle = NextInCycle();
197 wxRadioButton* old = 0;
198
199 if (cycle)
200 {
201 while (cycle != this)
202 {
203 if ( cycle->GetValue() )
204 {
205 old = cycle;
96be256b 206 cycle->SetValue(false);
f37907ff
MB
207 }
208 cycle = cycle->NextInCycle();
209 }
210 }
211
212 return old;
a4294b78 213}
4bb6408c 214
f37907ff
MB
215void wxRadioButton::RemoveFromCycle()
216{
217 wxRadioButton* curr = NextInCycle();
218
219 while( curr )
220 {
221 if( curr->NextInCycle() == this )
222 {
223 curr->m_cycle = this->m_cycle;
224 return;
225 }
226
227 curr = curr->NextInCycle();
228 }
229}