]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobut.cpp
set string length to 0 in AllocBeforeWrite() just to be tidy (replaces patch 1123226)
[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();
31528cd3 59
a4294b78 60 wxString label1(wxStripMenuCodes(label));
31528cd3 61
da494b40 62 wxXmString text( label1 );
31528cd3 63
da494b40 64 WXFontType fontType = m_font.GetFontType(XtDisplay(parentWidget));
31528cd3 65
a4294b78
JS
66 Widget radioButtonWidget = XtVaCreateManagedWidget ("toggle",
67#if wxUSE_GADGETS
2d120f83 68 xmToggleButtonGadgetClass, parentWidget,
a4294b78 69#else
2d120f83 70 xmToggleButtonWidgetClass, parentWidget,
a4294b78 71#endif
da494b40
MB
72 wxFont::GetFontTag(), fontType,
73 XmNlabelString, text(),
2d120f83
JS
74 XmNfillOnSelect, True,
75 XmNindicatorType, XmONE_OF_MANY, // diamond-shape
76 NULL);
31528cd3 77
f96b10c2
MB
78 XtAddCallback (radioButtonWidget,
79 XmNvalueChangedCallback,
80 (XtCallbackProc)wxRadioButtonCallback,
81 (XtPointer)this);
31528cd3 82
a4294b78 83 m_mainWidget = (WXWidget) radioButtonWidget;
31528cd3 84
a4294b78 85 XtManageChild (radioButtonWidget);
31528cd3 86
f96b10c2
MB
87 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
88 pos.x, pos.y, size.x, size.y);
31528cd3 89
0d57be45 90 ChangeBackgroundColour();
31528cd3 91
96be256b 92 //copied from mac/radiobut.cpp (from here till "return true;")
f37907ff
MB
93 m_cycle = this ;
94
95 if (HasFlag(wxRB_GROUP))
96 {
97 AddInCycle( NULL ) ;
98 }
99 else
100 {
101 /* search backward for last group start */
102 wxRadioButton *chief = (wxRadioButton*) NULL;
ac32ba44 103 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
f37907ff
MB
104 while (node)
105 {
106 wxWindow *child = node->GetData();
107 if (child->IsKindOf( CLASSINFO( wxRadioButton ) ) )
108 {
109 chief = (wxRadioButton*) child;
110 if (child->HasFlag(wxRB_GROUP)) break;
111 }
112 node = node->GetPrevious();
113 }
114 AddInCycle( chief ) ;
115 }
96be256b 116 return true;
4bb6408c
JS
117}
118
119void wxRadioButton::SetValue(bool value)
120{
f37907ff
MB
121 if (GetValue() == value)
122 return;
123
96be256b
MB
124 m_inSetValue = true;
125 XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, False);
126 m_inSetValue = false;
f37907ff
MB
127
128 ClearSelections();
4bb6408c
JS
129}
130
131// Get single selection, for single choice list items
132bool wxRadioButton::GetValue() const
133{
a4294b78 134 return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0);
4bb6408c
JS
135}
136
137void wxRadioButton::Command (wxCommandEvent & event)
138{
687706f5 139 SetValue ( (event.GetInt() != 0) );
2d120f83 140 ProcessCommand (event);
4bb6408c
JS
141}
142
0d57be45
JS
143void wxRadioButton::ChangeBackgroundColour()
144{
321db4b6 145 wxWindow::ChangeBackgroundColour();
9838df2c
JS
146
147 // What colour should this be?
eb6fa4b4 148 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c
JS
149
150 XtVaSetValues ((Widget) GetMainWidget(),
151 XmNselectColor, selectPixel,
152 NULL);
0d57be45
JS
153}
154
a4294b78 155void wxRadioButtonCallback (Widget w, XtPointer clientData,
2d120f83 156 XmToggleButtonCallbackStruct * cbs)
a4294b78 157{
2d120f83
JS
158 if (!cbs->set)
159 return;
31528cd3 160
2d120f83
JS
161 wxRadioButton *item = (wxRadioButton *) clientData;
162 if (item->InSetValue())
163 return;
31528cd3 164
f37907ff
MB
165 //based on mac/radiobut.cpp
166 wxRadioButton* old = item->ClearSelections();
96be256b 167 item->SetValue(true);
f37907ff
MB
168
169 if ( old )
170 {
171 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED,
172 old->GetId() );
173 event.SetEventObject(old);
96be256b 174 event.SetInt( false );
f37907ff
MB
175 old->ProcessCommand(event);
176 }
177 wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() );
178 event2.SetEventObject(item);
96be256b 179 event2.SetInt( true );
f37907ff
MB
180 item->ProcessCommand(event2);
181}
182
183wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle)
184{
185 wxRadioButton* next;
186 wxRadioButton* current;
187
188 if (cycle == NULL)
189 {
190 m_cycle = this;
191 return this;
192 }
193 else
194 {
195 current = cycle;
196 while ((next = current->m_cycle) != cycle)
197 current = current->m_cycle;
198 m_cycle = cycle;
199 current->m_cycle = this;
200 return cycle;
201 }
202}
31528cd3 203
f37907ff
MB
204wxRadioButton* wxRadioButton::ClearSelections()
205{
206 wxRadioButton* cycle = NextInCycle();
207 wxRadioButton* old = 0;
208
209 if (cycle)
210 {
211 while (cycle != this)
212 {
213 if ( cycle->GetValue() )
214 {
215 old = cycle;
96be256b 216 cycle->SetValue(false);
f37907ff
MB
217 }
218 cycle = cycle->NextInCycle();
219 }
220 }
221
222 return old;
a4294b78 223}
4bb6408c 224
f37907ff
MB
225void wxRadioButton::RemoveFromCycle()
226{
227 wxRadioButton* curr = NextInCycle();
228
229 while( curr )
230 {
231 if( curr->NextInCycle() == this )
232 {
233 curr->m_cycle = this->m_cycle;
234 return;
235 }
236
237 curr = curr->NextInCycle();
238 }
239}