]>
Commit | Line | Data |
---|---|---|
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 | ||
4bb6408c | 15 | #include "wx/radiobut.h" |
de6185e2 WS |
16 | |
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/utils.h" | |
19 | #endif | |
a4294b78 | 20 | |
338dd992 JJ |
21 | #ifdef __VMS__ |
22 | #pragma message disable nosimpint | |
23 | #endif | |
a4294b78 JS |
24 | #include <Xm/ToggleB.h> |
25 | #include <Xm/ToggleBG.h> | |
338dd992 JJ |
26 | #ifdef __VMS__ |
27 | #pragma message enable nosimpint | |
28 | #endif | |
a4294b78 | 29 | |
3096bd2f | 30 | #include "wx/motif/private.h" |
a4294b78 JS |
31 | |
32 | void wxRadioButtonCallback (Widget w, XtPointer clientData, | |
2d120f83 | 33 | XmToggleButtonCallbackStruct * cbs); |
4bb6408c | 34 | |
a4294b78 JS |
35 | wxRadioButton::wxRadioButton() |
36 | { | |
a4294b78 JS |
37 | } |
38 | ||
4bb6408c | 39 | bool wxRadioButton::Create(wxWindow *parent, wxWindowID id, |
2d120f83 JS |
40 | const wxString& label, |
41 | const wxPoint& pos, | |
42 | const wxSize& size, long style, | |
43 | const wxValidator& validator, | |
44 | const wxString& name) | |
4bb6408c | 45 | { |
f96b10c2 MB |
46 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) |
47 | return false; | |
105fbe1f | 48 | PreCreation(); |
31528cd3 | 49 | |
a4294b78 | 50 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
73608949 | 51 | Display* dpy = XtDisplay(parentWidget); |
31528cd3 | 52 | |
32cd189d | 53 | wxString label1(GetLabelText(label)); |
31528cd3 | 54 | |
da494b40 | 55 | wxXmString text( label1 ); |
31528cd3 | 56 | |
a4294b78 JS |
57 | Widget radioButtonWidget = XtVaCreateManagedWidget ("toggle", |
58 | #if wxUSE_GADGETS | |
2d120f83 | 59 | xmToggleButtonGadgetClass, parentWidget, |
a4294b78 | 60 | #else |
2d120f83 | 61 | xmToggleButtonWidgetClass, parentWidget, |
a4294b78 | 62 | #endif |
73608949 | 63 | wxFont::GetFontTag(), m_font.GetFontTypeC(dpy), |
da494b40 | 64 | XmNlabelString, text(), |
2d120f83 JS |
65 | XmNfillOnSelect, True, |
66 | XmNindicatorType, XmONE_OF_MANY, // diamond-shape | |
67 | NULL); | |
31528cd3 | 68 | |
f96b10c2 MB |
69 | XtAddCallback (radioButtonWidget, |
70 | XmNvalueChangedCallback, | |
71 | (XtCallbackProc)wxRadioButtonCallback, | |
72 | (XtPointer)this); | |
31528cd3 | 73 | |
a4294b78 | 74 | m_mainWidget = (WXWidget) radioButtonWidget; |
31528cd3 | 75 | |
a4294b78 | 76 | XtManageChild (radioButtonWidget); |
31528cd3 | 77 | |
105fbe1f | 78 | PostCreation(); |
f96b10c2 MB |
79 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, |
80 | pos.x, pos.y, size.x, size.y); | |
31528cd3 | 81 | |
96be256b | 82 | //copied from mac/radiobut.cpp (from here till "return true;") |
f37907ff | 83 | m_cycle = this ; |
002ceb38 | 84 | |
f37907ff MB |
85 | if (HasFlag(wxRB_GROUP)) |
86 | { | |
87 | AddInCycle( NULL ) ; | |
88 | } | |
89 | else | |
90 | { | |
91 | /* search backward for last group start */ | |
d3b9f782 | 92 | wxRadioButton *chief = NULL; |
ac32ba44 | 93 | wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast(); |
f37907ff MB |
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 | } | |
96be256b | 106 | return true; |
4bb6408c JS |
107 | } |
108 | ||
109 | void wxRadioButton::SetValue(bool value) | |
110 | { | |
f37907ff MB |
111 | if (GetValue() == value) |
112 | return; | |
113 | ||
96be256b MB |
114 | m_inSetValue = true; |
115 | XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, False); | |
116 | m_inSetValue = false; | |
f37907ff MB |
117 | |
118 | ClearSelections(); | |
4bb6408c JS |
119 | } |
120 | ||
121 | // Get single selection, for single choice list items | |
122 | bool wxRadioButton::GetValue() const | |
123 | { | |
a4294b78 | 124 | return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0); |
4bb6408c JS |
125 | } |
126 | ||
127 | void wxRadioButton::Command (wxCommandEvent & event) | |
128 | { | |
687706f5 | 129 | SetValue ( (event.GetInt() != 0) ); |
2d120f83 | 130 | ProcessCommand (event); |
4bb6408c JS |
131 | } |
132 | ||
0d57be45 JS |
133 | void wxRadioButton::ChangeBackgroundColour() |
134 | { | |
321db4b6 | 135 | wxWindow::ChangeBackgroundColour(); |
9838df2c JS |
136 | |
137 | // What colour should this be? | |
f516d986 | 138 | wxColour colour = *wxBLACK; |
3e0071d9 | 139 | WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget)); |
9838df2c JS |
140 | |
141 | XtVaSetValues ((Widget) GetMainWidget(), | |
142 | XmNselectColor, selectPixel, | |
143 | NULL); | |
0d57be45 JS |
144 | } |
145 | ||
355b4d3d | 146 | void wxRadioButtonCallback (Widget WXUNUSED(w), XtPointer clientData, |
2d120f83 | 147 | XmToggleButtonCallbackStruct * cbs) |
a4294b78 | 148 | { |
2d120f83 JS |
149 | if (!cbs->set) |
150 | return; | |
31528cd3 | 151 | |
2d120f83 JS |
152 | wxRadioButton *item = (wxRadioButton *) clientData; |
153 | if (item->InSetValue()) | |
154 | return; | |
31528cd3 | 155 | |
f37907ff MB |
156 | //based on mac/radiobut.cpp |
157 | wxRadioButton* old = item->ClearSelections(); | |
96be256b | 158 | item->SetValue(true); |
f37907ff MB |
159 | |
160 | if ( old ) | |
161 | { | |
ce7fe42e | 162 | wxCommandEvent event(wxEVT_RADIOBUTTON, |
f37907ff MB |
163 | old->GetId() ); |
164 | event.SetEventObject(old); | |
96be256b | 165 | event.SetInt( false ); |
f37907ff MB |
166 | old->ProcessCommand(event); |
167 | } | |
ce7fe42e | 168 | wxCommandEvent event2(wxEVT_RADIOBUTTON, item->GetId() ); |
f37907ff | 169 | event2.SetEventObject(item); |
96be256b | 170 | event2.SetInt( true ); |
f37907ff MB |
171 | item->ProcessCommand(event2); |
172 | } | |
173 | ||
174 | wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle) | |
175 | { | |
f37907ff MB |
176 | if (cycle == NULL) |
177 | { | |
178 | m_cycle = this; | |
f37907ff MB |
179 | } |
180 | else | |
181 | { | |
002ceb38 VZ |
182 | wxRadioButton* current = cycle; |
183 | while ( current->m_cycle != cycle ) | |
f37907ff MB |
184 | current = current->m_cycle; |
185 | m_cycle = cycle; | |
186 | current->m_cycle = this; | |
f37907ff | 187 | } |
002ceb38 VZ |
188 | |
189 | return cycle; | |
f37907ff | 190 | } |
31528cd3 | 191 | |
f37907ff MB |
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; | |
96be256b | 204 | cycle->SetValue(false); |
f37907ff MB |
205 | } |
206 | cycle = cycle->NextInCycle(); | |
207 | } | |
208 | } | |
209 | ||
210 | return old; | |
a4294b78 | 211 | } |
4bb6408c | 212 | |
f37907ff MB |
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 | } |