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