]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobut.cpp
Added wxRenderer method for drawing selection
[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
4bb6408c 19#include "wx/radiobut.h"
a4294b78
JS
20#include "wx/utils.h"
21
338dd992
JJ
22#ifdef __VMS__
23#pragma message disable nosimpint
24#endif
a4294b78
JS
25#include <Xm/ToggleB.h>
26#include <Xm/ToggleBG.h>
338dd992
JJ
27#ifdef __VMS__
28#pragma message enable nosimpint
29#endif
a4294b78 30
3096bd2f 31#include "wx/motif/private.h"
a4294b78
JS
32
33void wxRadioButtonCallback (Widget w, XtPointer clientData,
2d120f83 34 XmToggleButtonCallbackStruct * cbs);
4bb6408c 35
4bb6408c 36IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
4bb6408c 37
a4294b78
JS
38wxRadioButton::wxRadioButton()
39{
a4294b78
JS
40}
41
4bb6408c 42bool wxRadioButton::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
43 const wxString& label,
44 const wxPoint& pos,
45 const wxSize& size, long style,
46 const wxValidator& validator,
47 const wxString& name)
4bb6408c 48{
f96b10c2
MB
49 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
50 return false;
31528cd3 51
a4294b78 52 Widget parentWidget = (Widget) parent->GetClientWidget();
73608949 53 Display* dpy = XtDisplay(parentWidget);
31528cd3 54
a4294b78 55 wxString label1(wxStripMenuCodes(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
f96b10c2
MB
80 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
81 pos.x, pos.y, size.x, size.y);
31528cd3 82
0d57be45 83 ChangeBackgroundColour();
31528cd3 84
96be256b 85 //copied from mac/radiobut.cpp (from here till "return true;")
f37907ff 86 m_cycle = this ;
002ceb38 87
f37907ff
MB
88 if (HasFlag(wxRB_GROUP))
89 {
90 AddInCycle( NULL ) ;
91 }
92 else
93 {
94 /* search backward for last group start */
95 wxRadioButton *chief = (wxRadioButton*) NULL;
ac32ba44 96 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
f37907ff
MB
97 while (node)
98 {
99 wxWindow *child = node->GetData();
100 if (child->IsKindOf( CLASSINFO( wxRadioButton ) ) )
101 {
102 chief = (wxRadioButton*) child;
103 if (child->HasFlag(wxRB_GROUP)) break;
104 }
105 node = node->GetPrevious();
106 }
107 AddInCycle( chief ) ;
108 }
96be256b 109 return true;
4bb6408c
JS
110}
111
112void wxRadioButton::SetValue(bool value)
113{
f37907ff
MB
114 if (GetValue() == value)
115 return;
116
96be256b
MB
117 m_inSetValue = true;
118 XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, False);
119 m_inSetValue = false;
f37907ff
MB
120
121 ClearSelections();
4bb6408c
JS
122}
123
124// Get single selection, for single choice list items
125bool wxRadioButton::GetValue() const
126{
a4294b78 127 return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0);
4bb6408c
JS
128}
129
130void wxRadioButton::Command (wxCommandEvent & event)
131{
687706f5 132 SetValue ( (event.GetInt() != 0) );
2d120f83 133 ProcessCommand (event);
4bb6408c
JS
134}
135
0d57be45
JS
136void wxRadioButton::ChangeBackgroundColour()
137{
321db4b6 138 wxWindow::ChangeBackgroundColour();
9838df2c
JS
139
140 // What colour should this be?
f516d986
VZ
141 wxColour colour = *wxBLACK;
142 int selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c
JS
143
144 XtVaSetValues ((Widget) GetMainWidget(),
145 XmNselectColor, selectPixel,
146 NULL);
0d57be45
JS
147}
148
355b4d3d 149void wxRadioButtonCallback (Widget WXUNUSED(w), XtPointer clientData,
2d120f83 150 XmToggleButtonCallbackStruct * cbs)
a4294b78 151{
2d120f83
JS
152 if (!cbs->set)
153 return;
31528cd3 154
2d120f83
JS
155 wxRadioButton *item = (wxRadioButton *) clientData;
156 if (item->InSetValue())
157 return;
31528cd3 158
f37907ff
MB
159 //based on mac/radiobut.cpp
160 wxRadioButton* old = item->ClearSelections();
96be256b 161 item->SetValue(true);
f37907ff
MB
162
163 if ( old )
164 {
165 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED,
166 old->GetId() );
167 event.SetEventObject(old);
96be256b 168 event.SetInt( false );
f37907ff
MB
169 old->ProcessCommand(event);
170 }
171 wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() );
172 event2.SetEventObject(item);
96be256b 173 event2.SetInt( true );
f37907ff
MB
174 item->ProcessCommand(event2);
175}
176
177wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle)
178{
f37907ff
MB
179 if (cycle == NULL)
180 {
181 m_cycle = this;
f37907ff
MB
182 }
183 else
184 {
002ceb38
VZ
185 wxRadioButton* current = cycle;
186 while ( current->m_cycle != cycle )
f37907ff
MB
187 current = current->m_cycle;
188 m_cycle = cycle;
189 current->m_cycle = this;
f37907ff 190 }
002ceb38
VZ
191
192 return cycle;
f37907ff 193}
31528cd3 194
f37907ff
MB
195wxRadioButton* wxRadioButton::ClearSelections()
196{
197 wxRadioButton* cycle = NextInCycle();
198 wxRadioButton* old = 0;
199
200 if (cycle)
201 {
202 while (cycle != this)
203 {
204 if ( cycle->GetValue() )
205 {
206 old = cycle;
96be256b 207 cycle->SetValue(false);
f37907ff
MB
208 }
209 cycle = cycle->NextInCycle();
210 }
211 }
212
213 return old;
a4294b78 214}
4bb6408c 215
f37907ff
MB
216void wxRadioButton::RemoveFromCycle()
217{
218 wxRadioButton* curr = NextInCycle();
219
220 while( curr )
221 {
222 if( curr->NextInCycle() == this )
223 {
224 curr->m_cycle = this->m_cycle;
225 return;
226 }
227
228 curr = curr->NextInCycle();
229 }
230}