]>
Commit | Line | Data |
---|---|---|
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 | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
4bb6408c JS |
13 | #pragma implementation "radiobut.h" |
14 | #endif | |
15 | ||
bcd055ae JJ |
16 | #ifdef __VMS |
17 | #define XtDisplay XTDISPLAY | |
18 | #endif | |
19 | ||
f6045f99 GD |
20 | #include "wx/defs.h" |
21 | ||
4bb6408c | 22 | #include "wx/radiobut.h" |
a4294b78 JS |
23 | #include "wx/utils.h" |
24 | ||
338dd992 JJ |
25 | #ifdef __VMS__ |
26 | #pragma message disable nosimpint | |
27 | #endif | |
a4294b78 JS |
28 | #include <Xm/ToggleB.h> |
29 | #include <Xm/ToggleBG.h> | |
338dd992 JJ |
30 | #ifdef __VMS__ |
31 | #pragma message enable nosimpint | |
32 | #endif | |
a4294b78 | 33 | |
3096bd2f | 34 | #include "wx/motif/private.h" |
a4294b78 JS |
35 | |
36 | void wxRadioButtonCallback (Widget w, XtPointer clientData, | |
2d120f83 | 37 | XmToggleButtonCallbackStruct * cbs); |
4bb6408c | 38 | |
4bb6408c | 39 | IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl) |
4bb6408c | 40 | |
a4294b78 JS |
41 | wxRadioButton::wxRadioButton() |
42 | { | |
a4294b78 JS |
43 | } |
44 | ||
4bb6408c | 45 | bool wxRadioButton::Create(wxWindow *parent, wxWindowID id, |
2d120f83 JS |
46 | const wxString& label, |
47 | const wxPoint& pos, | |
48 | const wxSize& size, long style, | |
49 | const wxValidator& validator, | |
50 | const wxString& name) | |
4bb6408c | 51 | { |
f96b10c2 MB |
52 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) |
53 | return false; | |
31528cd3 | 54 | |
a4294b78 | 55 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
31528cd3 | 56 | |
a4294b78 | 57 | wxString label1(wxStripMenuCodes(label)); |
31528cd3 | 58 | |
da494b40 | 59 | wxXmString text( label1 ); |
31528cd3 | 60 | |
da494b40 | 61 | WXFontType fontType = m_font.GetFontType(XtDisplay(parentWidget)); |
31528cd3 | 62 | |
a4294b78 JS |
63 | Widget radioButtonWidget = XtVaCreateManagedWidget ("toggle", |
64 | #if wxUSE_GADGETS | |
2d120f83 | 65 | xmToggleButtonGadgetClass, parentWidget, |
a4294b78 | 66 | #else |
2d120f83 | 67 | xmToggleButtonWidgetClass, parentWidget, |
a4294b78 | 68 | #endif |
da494b40 MB |
69 | wxFont::GetFontTag(), fontType, |
70 | XmNlabelString, text(), | |
2d120f83 JS |
71 | XmNfillOnSelect, True, |
72 | XmNindicatorType, XmONE_OF_MANY, // diamond-shape | |
73 | NULL); | |
31528cd3 | 74 | |
f96b10c2 MB |
75 | XtAddCallback (radioButtonWidget, |
76 | XmNvalueChangedCallback, | |
77 | (XtCallbackProc)wxRadioButtonCallback, | |
78 | (XtPointer)this); | |
31528cd3 | 79 | |
a4294b78 | 80 | m_mainWidget = (WXWidget) radioButtonWidget; |
31528cd3 | 81 | |
a4294b78 | 82 | XtManageChild (radioButtonWidget); |
31528cd3 | 83 | |
f96b10c2 MB |
84 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, |
85 | pos.x, pos.y, size.x, size.y); | |
31528cd3 | 86 | |
0d57be45 | 87 | ChangeBackgroundColour(); |
31528cd3 | 88 | |
f37907ff MB |
89 | //copied from mac/radiobut.cpp (from here till "return TRUE;") |
90 | m_cycle = this ; | |
91 | ||
92 | if (HasFlag(wxRB_GROUP)) | |
93 | { | |
94 | AddInCycle( NULL ) ; | |
95 | } | |
96 | else | |
97 | { | |
98 | /* search backward for last group start */ | |
99 | wxRadioButton *chief = (wxRadioButton*) NULL; | |
ac32ba44 | 100 | wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast(); |
f37907ff MB |
101 | while (node) |
102 | { | |
103 | wxWindow *child = node->GetData(); | |
104 | if (child->IsKindOf( CLASSINFO( wxRadioButton ) ) ) | |
105 | { | |
106 | chief = (wxRadioButton*) child; | |
107 | if (child->HasFlag(wxRB_GROUP)) break; | |
108 | } | |
109 | node = node->GetPrevious(); | |
110 | } | |
111 | AddInCycle( chief ) ; | |
112 | } | |
a4294b78 | 113 | return TRUE; |
4bb6408c JS |
114 | } |
115 | ||
116 | void wxRadioButton::SetValue(bool value) | |
117 | { | |
f37907ff MB |
118 | if (GetValue() == value) |
119 | return; | |
120 | ||
a4294b78 JS |
121 | m_inSetValue = TRUE; |
122 | XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, FALSE); | |
123 | m_inSetValue = FALSE; | |
f37907ff MB |
124 | |
125 | ClearSelections(); | |
4bb6408c JS |
126 | } |
127 | ||
128 | // Get single selection, for single choice list items | |
129 | bool wxRadioButton::GetValue() const | |
130 | { | |
a4294b78 | 131 | return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0); |
4bb6408c JS |
132 | } |
133 | ||
134 | void wxRadioButton::Command (wxCommandEvent & event) | |
135 | { | |
2d120f83 JS |
136 | SetValue ( (event.m_commandInt != 0) ); |
137 | ProcessCommand (event); | |
4bb6408c JS |
138 | } |
139 | ||
0d57be45 JS |
140 | void wxRadioButton::ChangeBackgroundColour() |
141 | { | |
321db4b6 | 142 | wxWindow::ChangeBackgroundColour(); |
9838df2c JS |
143 | |
144 | // What colour should this be? | |
eb6fa4b4 | 145 | int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget)); |
9838df2c JS |
146 | |
147 | XtVaSetValues ((Widget) GetMainWidget(), | |
148 | XmNselectColor, selectPixel, | |
149 | NULL); | |
0d57be45 JS |
150 | } |
151 | ||
a4294b78 | 152 | void wxRadioButtonCallback (Widget w, XtPointer clientData, |
2d120f83 | 153 | XmToggleButtonCallbackStruct * cbs) |
a4294b78 | 154 | { |
2d120f83 JS |
155 | if (!cbs->set) |
156 | return; | |
31528cd3 | 157 | |
2d120f83 JS |
158 | wxRadioButton *item = (wxRadioButton *) clientData; |
159 | if (item->InSetValue()) | |
160 | return; | |
31528cd3 | 161 | |
f37907ff MB |
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 | ||
180 | wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle) | |
181 | { | |
182 | wxRadioButton* next; | |
183 | wxRadioButton* current; | |
184 | ||
185 | if (cycle == NULL) | |
186 | { | |
187 | m_cycle = this; | |
188 | return this; | |
189 | } | |
190 | else | |
191 | { | |
192 | current = cycle; | |
193 | while ((next = current->m_cycle) != cycle) | |
194 | current = current->m_cycle; | |
195 | m_cycle = cycle; | |
196 | current->m_cycle = this; | |
197 | return cycle; | |
198 | } | |
199 | } | |
31528cd3 | 200 | |
f37907ff MB |
201 | wxRadioButton* wxRadioButton::ClearSelections() |
202 | { | |
203 | wxRadioButton* cycle = NextInCycle(); | |
204 | wxRadioButton* old = 0; | |
205 | ||
206 | if (cycle) | |
207 | { | |
208 | while (cycle != this) | |
209 | { | |
210 | if ( cycle->GetValue() ) | |
211 | { | |
212 | old = cycle; | |
213 | cycle->SetValue(FALSE); | |
214 | } | |
215 | cycle = cycle->NextInCycle(); | |
216 | } | |
217 | } | |
218 | ||
219 | return old; | |
a4294b78 | 220 | } |
4bb6408c | 221 | |
f37907ff MB |
222 | void wxRadioButton::RemoveFromCycle() |
223 | { | |
224 | wxRadioButton* curr = NextInCycle(); | |
225 | ||
226 | while( curr ) | |
227 | { | |
228 | if( curr->NextInCycle() == this ) | |
229 | { | |
230 | curr->m_cycle = this->m_cycle; | |
231 | return; | |
232 | } | |
233 | ||
234 | curr = curr->NextInCycle(); | |
235 | } | |
236 | } |