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