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