]>
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 | ||
bcd055ae JJ |
15 | #ifdef __VMS |
16 | #define XtDisplay XTDISPLAY | |
17 | #endif | |
18 | ||
4bb6408c | 19 | #include "wx/radiobut.h" |
de6185e2 WS |
20 | |
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/utils.h" | |
23 | #endif | |
a4294b78 | 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; | |
105fbe1f | 54 | PreCreation(); |
31528cd3 | 55 | |
a4294b78 | 56 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
73608949 | 57 | Display* dpy = XtDisplay(parentWidget); |
31528cd3 | 58 | |
32cd189d | 59 | wxString label1(GetLabelText(label)); |
31528cd3 | 60 | |
da494b40 | 61 | wxXmString text( label1 ); |
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 |
73608949 | 69 | wxFont::GetFontTag(), m_font.GetFontTypeC(dpy), |
da494b40 | 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 | |
105fbe1f | 84 | PostCreation(); |
f96b10c2 MB |
85 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, |
86 | pos.x, pos.y, size.x, size.y); | |
31528cd3 | 87 | |
96be256b | 88 | //copied from mac/radiobut.cpp (from here till "return true;") |
f37907ff | 89 | m_cycle = this ; |
002ceb38 | 90 | |
f37907ff MB |
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; | |
ac32ba44 | 99 | wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast(); |
f37907ff MB |
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 | } | |
96be256b | 112 | return true; |
4bb6408c JS |
113 | } |
114 | ||
115 | void wxRadioButton::SetValue(bool value) | |
116 | { | |
f37907ff MB |
117 | if (GetValue() == value) |
118 | return; | |
119 | ||
96be256b MB |
120 | m_inSetValue = true; |
121 | XmToggleButtonSetState ((Widget) m_mainWidget, (Boolean) value, False); | |
122 | m_inSetValue = false; | |
f37907ff MB |
123 | |
124 | ClearSelections(); | |
4bb6408c JS |
125 | } |
126 | ||
127 | // Get single selection, for single choice list items | |
128 | bool wxRadioButton::GetValue() const | |
129 | { | |
a4294b78 | 130 | return (XmToggleButtonGetState ((Widget) m_mainWidget) != 0); |
4bb6408c JS |
131 | } |
132 | ||
133 | void wxRadioButton::Command (wxCommandEvent & event) | |
134 | { | |
687706f5 | 135 | SetValue ( (event.GetInt() != 0) ); |
2d120f83 | 136 | ProcessCommand (event); |
4bb6408c JS |
137 | } |
138 | ||
0d57be45 JS |
139 | void wxRadioButton::ChangeBackgroundColour() |
140 | { | |
321db4b6 | 141 | wxWindow::ChangeBackgroundColour(); |
9838df2c JS |
142 | |
143 | // What colour should this be? | |
f516d986 | 144 | wxColour colour = *wxBLACK; |
3e0071d9 | 145 | WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget)); |
9838df2c JS |
146 | |
147 | XtVaSetValues ((Widget) GetMainWidget(), | |
148 | XmNselectColor, selectPixel, | |
149 | NULL); | |
0d57be45 JS |
150 | } |
151 | ||
355b4d3d | 152 | void wxRadioButtonCallback (Widget WXUNUSED(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(); | |
96be256b | 164 | item->SetValue(true); |
f37907ff MB |
165 | |
166 | if ( old ) | |
167 | { | |
168 | wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, | |
169 | old->GetId() ); | |
170 | event.SetEventObject(old); | |
96be256b | 171 | event.SetInt( false ); |
f37907ff MB |
172 | old->ProcessCommand(event); |
173 | } | |
174 | wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, item->GetId() ); | |
175 | event2.SetEventObject(item); | |
96be256b | 176 | event2.SetInt( true ); |
f37907ff MB |
177 | item->ProcessCommand(event2); |
178 | } | |
179 | ||
180 | wxRadioButton* wxRadioButton::AddInCycle(wxRadioButton *cycle) | |
181 | { | |
f37907ff MB |
182 | if (cycle == NULL) |
183 | { | |
184 | m_cycle = this; | |
f37907ff MB |
185 | } |
186 | else | |
187 | { | |
002ceb38 VZ |
188 | wxRadioButton* current = cycle; |
189 | while ( current->m_cycle != cycle ) | |
f37907ff MB |
190 | current = current->m_cycle; |
191 | m_cycle = cycle; | |
192 | current->m_cycle = this; | |
f37907ff | 193 | } |
002ceb38 VZ |
194 | |
195 | return cycle; | |
f37907ff | 196 | } |
31528cd3 | 197 | |
f37907ff MB |
198 | wxRadioButton* 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; | |
96be256b | 210 | cycle->SetValue(false); |
f37907ff MB |
211 | } |
212 | cycle = cycle->NextInCycle(); | |
213 | } | |
214 | } | |
215 | ||
216 | return old; | |
a4294b78 | 217 | } |
4bb6408c | 218 | |
f37907ff MB |
219 | void 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 | } |