]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: radiobox.cpp | |
3 | // Purpose: wxRadioBox | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
af0bb3b1 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
4bb6408c JS |
13 | #pragma implementation "radiobox.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/radiobox.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/Label.h> |
29 | #include <Xm/LabelG.h> | |
30 | #include <Xm/ToggleB.h> | |
31 | #include <Xm/ToggleBG.h> | |
32 | #include <Xm/RowColumn.h> | |
9838df2c | 33 | #include <Xm/Frame.h> |
338dd992 JJ |
34 | #ifdef __VMS__ |
35 | #pragma message enable nosimpint | |
36 | #endif | |
a4294b78 | 37 | |
3096bd2f | 38 | #include "wx/motif/private.h" |
a4294b78 JS |
39 | |
40 | void wxRadioBoxCallback (Widget w, XtPointer clientData, | |
af0bb3b1 | 41 | XmToggleButtonCallbackStruct * cbs); |
4bb6408c | 42 | |
4bb6408c | 43 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) |
4bb6408c JS |
44 | |
45 | // Radio box item | |
3bdb8629 | 46 | void wxRadioBox::Init() |
4bb6408c JS |
47 | { |
48 | m_selectedButton = -1; | |
49 | m_noItems = 0; | |
50 | m_noRowsOrCols = 0; | |
51 | m_majorDim = 0 ; | |
52 | } | |
53 | ||
54 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title, | |
55 | const wxPoint& pos, const wxSize& size, | |
56 | int n, const wxString choices[], | |
57 | int majorDim, long style, | |
58 | const wxValidator& val, const wxString& name) | |
59 | { | |
3bdb8629 MB |
60 | if( !CreateControl( parent, id, pos, size, style, val, name ) ) |
61 | return false; | |
4bb6408c | 62 | |
3bdb8629 | 63 | m_noItems = n; |
4bb6408c JS |
64 | m_noRowsOrCols = majorDim; |
65 | ||
66 | if (majorDim==0) | |
67 | m_majorDim = n ; | |
68 | else | |
69 | m_majorDim = majorDim ; | |
70 | ||
a4294b78 JS |
71 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
72 | ||
34774093 | 73 | m_mainWidget = XtVaCreateWidget ("radioboxframe", |
2ad99bc1 | 74 | xmFrameWidgetClass, parentWidget, |
2ad99bc1 JS |
75 | XmNresizeHeight, True, |
76 | XmNresizeWidth, True, | |
af0bb3b1 | 77 | NULL); |
a4294b78 | 78 | |
2ad99bc1 | 79 | wxString label1(wxStripMenuCodes(title)); |
a4294b78 | 80 | |
da494b40 | 81 | WXFontType fontType = m_font.GetFontType(XtDisplay(parentWidget)); |
2ad99bc1 | 82 | |
a4294b78 JS |
83 | if (label1 != "") |
84 | { | |
31528cd3 | 85 | wxXmString text(label1); |
3dd709d8 MB |
86 | m_labelWidget = (WXWidget) |
87 | XtVaCreateManagedWidget( label1.c_str(), | |
a4294b78 | 88 | #if wxUSE_GADGETS |
da494b40 MB |
89 | style & wxCOLOURED ? xmLabelWidgetClass |
90 | : xmLabelGadgetClass, | |
91 | (Widget)m_mainWidget, | |
a4294b78 | 92 | #else |
da494b40 MB |
93 | xmLabelWidgetClass, |
94 | (Widget)m_mainWidget, | |
a4294b78 | 95 | #endif |
da494b40 MB |
96 | wxFont::GetFontTag(), fontType, |
97 | XmNlabelString, text(), | |
11a2ce5a MB |
98 | // XmNframeChildType is not in Motif 1.2, nor in Lesstif, |
99 | // if it was compiled with 1.2 compatibility | |
34774093 | 100 | // TODO: check this still looks OK for Motif 1.2. |
11a2ce5a | 101 | #if (XmVersion > 1200) |
da494b40 | 102 | XmNframeChildType, XmFRAME_TITLE_CHILD, |
2b5f62a0 | 103 | #else |
da494b40 | 104 | XmNchildType, XmFRAME_TITLE_CHILD, |
34774093 | 105 | #endif |
da494b40 MB |
106 | XmNchildVerticalAlignment, |
107 | XmALIGNMENT_CENTER, | |
108 | NULL); | |
a4294b78 JS |
109 | } |
110 | ||
111 | Arg args[3]; | |
112 | ||
978db70e | 113 | m_majorDim = (n + m_majorDim - 1) / m_majorDim; |
a4294b78 JS |
114 | |
115 | XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ? | |
af0bb3b1 | 116 | XmHORIZONTAL : XmVERTICAL)); |
98300330 | 117 | XtSetArg (args[1], XmNnumColumns, m_majorDim); |
a4294b78 | 118 | |
2ad99bc1 | 119 | Widget radioBoxWidget = XmCreateRadioBox ((Widget)m_mainWidget, "radioBoxWidget", args, 2); |
a4294b78 | 120 | |
3bdb8629 MB |
121 | m_radioButtons.reserve(n); |
122 | m_radioButtonLabels.reserve(n); | |
a4294b78 | 123 | |
a4294b78 JS |
124 | int i; |
125 | for (i = 0; i < n; i++) | |
126 | { | |
127 | wxString str(wxStripMenuCodes(choices[i])); | |
3bdb8629 MB |
128 | m_radioButtonLabels.push_back(str); |
129 | Widget radioItem = XtVaCreateManagedWidget (wxConstCast(str.c_str(), char), | |
a4294b78 | 130 | #if wxUSE_GADGETS |
af0bb3b1 | 131 | xmToggleButtonGadgetClass, radioBoxWidget, |
a4294b78 | 132 | #else |
da494b40 | 133 | xmToggleButtonWidgetClass, radioBoxWidget, |
a4294b78 | 134 | #endif |
da494b40 | 135 | wxFont::GetFontTag(), fontType, |
af0bb3b1 | 136 | NULL); |
3bdb8629 MB |
137 | m_radioButtons.push_back((WXWidget)radioItem); |
138 | XtAddCallback (radioItem, XmNvalueChangedCallback, | |
139 | (XtCallbackProc) wxRadioBoxCallback, | |
140 | (XtPointer) this); | |
a4294b78 | 141 | } |
a4294b78 | 142 | |
4b5f3fe6 JS |
143 | ChangeFont(FALSE); |
144 | ||
2ad99bc1 JS |
145 | SetSelection (0); |
146 | ||
147 | XtRealizeWidget((Widget)m_mainWidget); | |
a4294b78 | 148 | XtManageChild (radioBoxWidget); |
2ad99bc1 | 149 | XtManageChild ((Widget)m_mainWidget); |
a4294b78 | 150 | |
2ad99bc1 | 151 | AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y); |
a4294b78 | 152 | |
0d57be45 | 153 | ChangeBackgroundColour(); |
a4294b78 JS |
154 | |
155 | return TRUE; | |
4bb6408c JS |
156 | } |
157 | ||
158 | ||
159 | wxRadioBox::~wxRadioBox() | |
160 | { | |
15d5ab67 | 161 | DetachWidget(m_mainWidget); |
15d5ab67 | 162 | XtDestroyWidget((Widget) m_mainWidget); |
15d5ab67 JS |
163 | |
164 | m_mainWidget = (WXWidget) 0; | |
4bb6408c JS |
165 | } |
166 | ||
18128cbb | 167 | void wxRadioBox::SetString(int item, const wxString& label) |
4bb6408c | 168 | { |
a4294b78 JS |
169 | if (item < 0 || item >= m_noItems) |
170 | return; | |
171 | ||
172 | Widget widget = (Widget) m_radioButtons[item]; | |
173 | if (label != "") | |
174 | { | |
175 | wxString label1(wxStripMenuCodes(label)); | |
da494b40 | 176 | wxXmString text( label1 ); |
3bdb8629 | 177 | m_radioButtonLabels[item] = label1; |
a4294b78 | 178 | XtVaSetValues (widget, |
da494b40 | 179 | XmNlabelString, text(), |
af0bb3b1 VZ |
180 | XmNlabelType, XmSTRING, |
181 | NULL); | |
a4294b78 | 182 | } |
4bb6408c JS |
183 | } |
184 | ||
185 | int wxRadioBox::FindString(const wxString& s) const | |
186 | { | |
a4294b78 JS |
187 | int i; |
188 | for (i = 0; i < m_noItems; i++) | |
189 | if (s == m_radioButtonLabels[i]) | |
190 | return i; | |
4bb6408c JS |
191 | return -1; |
192 | } | |
193 | ||
194 | void wxRadioBox::SetSelection(int n) | |
195 | { | |
196 | if ((n < 0) || (n >= m_noItems)) | |
197 | return; | |
4bb6408c JS |
198 | |
199 | m_selectedButton = n; | |
a4294b78 JS |
200 | |
201 | m_inSetValue = TRUE; | |
202 | ||
203 | XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE); | |
204 | ||
205 | int i; | |
206 | for (i = 0; i < m_noItems; i++) | |
207 | if (i != n) | |
208 | XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE); | |
209 | ||
210 | m_inSetValue = FALSE; | |
4bb6408c JS |
211 | } |
212 | ||
213 | // Get single selection, for single choice list items | |
214 | int wxRadioBox::GetSelection() const | |
215 | { | |
216 | return m_selectedButton; | |
217 | } | |
218 | ||
219 | // Find string for position | |
220 | wxString wxRadioBox::GetString(int n) const | |
221 | { | |
a4294b78 JS |
222 | if ((n < 0) || (n >= m_noItems)) |
223 | return wxEmptyString; | |
224 | return m_radioButtonLabels[n]; | |
4bb6408c JS |
225 | } |
226 | ||
bfc6fde4 | 227 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
4bb6408c | 228 | { |
2ad99bc1 | 229 | bool managed = XtIsManaged((Widget) m_mainWidget); |
4bb6408c | 230 | |
a4294b78 | 231 | if (managed) |
2ad99bc1 | 232 | XtUnmanageChild ((Widget) m_mainWidget); |
4bb6408c | 233 | |
a4294b78 JS |
234 | int xx = x; int yy = y; |
235 | AdjustForParentClientOrigin(xx, yy, sizeFlags); | |
4bb6408c | 236 | |
a4294b78 | 237 | if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
2ad99bc1 | 238 | XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL); |
a4294b78 | 239 | if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
2ad99bc1 | 240 | XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL); |
4bb6408c | 241 | |
2ad99bc1 JS |
242 | if (width > 0) |
243 | XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL); | |
244 | if (height > 0) | |
245 | XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL); | |
a4294b78 | 246 | |
a4294b78 | 247 | if (managed) |
2ad99bc1 | 248 | XtManageChild ((Widget) m_mainWidget); |
4bb6408c JS |
249 | } |
250 | ||
251 | // Enable a specific button | |
a4294b78 | 252 | void wxRadioBox::Enable(int n, bool enable) |
4bb6408c | 253 | { |
a4294b78 JS |
254 | if ((n < 0) || (n >= m_noItems)) |
255 | return; | |
256 | ||
257 | XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable); | |
4bb6408c JS |
258 | } |
259 | ||
260 | // Enable all controls | |
af0bb3b1 | 261 | bool wxRadioBox::Enable(bool enable) |
4bb6408c | 262 | { |
af0bb3b1 VZ |
263 | if ( !wxControl::Enable(enable) ) |
264 | return FALSE; | |
4bb6408c | 265 | |
a4294b78 JS |
266 | int i; |
267 | for (i = 0; i < m_noItems; i++) | |
268 | XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable); | |
af0bb3b1 VZ |
269 | |
270 | return TRUE; | |
4bb6408c JS |
271 | } |
272 | ||
9838df2c JS |
273 | bool wxRadioBox::Show(bool show) |
274 | { | |
275 | // TODO: show/hide all children | |
276 | return wxControl::Show(show); | |
277 | } | |
278 | ||
4bb6408c | 279 | // Show a specific button |
a4294b78 | 280 | void wxRadioBox::Show(int n, bool show) |
4bb6408c | 281 | { |
a4294b78 JS |
282 | // This method isn't complete, and we try do do our best... |
283 | // It's main purpose isn't for allowing Show/Unshow dynamically, | |
284 | // but rather to provide a way to design wxRadioBox such: | |
285 | // | |
286 | // o Val1 o Val2 o Val3 | |
287 | // o Val4 o Val6 | |
288 | // o Val7 o Val8 o Val9 | |
289 | // | |
290 | // In my case, this is a 'direction' box, and the Show(5,False) is | |
291 | // coupled with an Enable(5,False) | |
292 | // | |
293 | if ((n < 0) || (n >= m_noItems)) | |
294 | return; | |
295 | ||
296 | XtVaSetValues ((Widget) m_radioButtons[n], | |
297 | XmNindicatorOn, (unsigned char) show, | |
298 | NULL); | |
299 | ||
300 | // Please note that this is all we can do: removing the label | |
301 | // if switching to unshow state. However, when switching | |
18128cbb | 302 | // to the on state, it's the prog. resp. to call SetString(item,...) |
a4294b78 JS |
303 | // after this call!! |
304 | if (!show) | |
18128cbb | 305 | wxRadioBox::SetString (n, " "); |
4bb6408c JS |
306 | } |
307 | ||
308 | // For single selection items only | |
309 | wxString wxRadioBox::GetStringSelection () const | |
310 | { | |
311 | int sel = GetSelection (); | |
312 | if (sel > -1) | |
313 | return this->GetString (sel); | |
314 | else | |
315 | return wxString(""); | |
316 | } | |
317 | ||
318 | bool wxRadioBox::SetStringSelection (const wxString& s) | |
319 | { | |
320 | int sel = FindString (s); | |
321 | if (sel > -1) | |
322 | { | |
323 | SetSelection (sel); | |
324 | return TRUE; | |
325 | } | |
326 | else | |
327 | return FALSE; | |
328 | } | |
329 | ||
330 | void wxRadioBox::Command (wxCommandEvent & event) | |
331 | { | |
332 | SetSelection (event.m_commandInt); | |
333 | ProcessCommand (event); | |
334 | } | |
335 | ||
4b5f3fe6 | 336 | void wxRadioBox::ChangeFont(bool keepOriginalSize) |
0d57be45 | 337 | { |
94b49b93 JS |
338 | wxWindow::ChangeFont(keepOriginalSize); |
339 | ||
da494b40 MB |
340 | WXFontType fontType = |
341 | m_font.GetFontType(XtDisplay((Widget) GetTopWidget())); | |
94b49b93 JS |
342 | |
343 | int i; | |
344 | for (i = 0; i < m_noItems; i++) | |
345 | { | |
346 | WXWidget radioButton = m_radioButtons[i]; | |
347 | ||
348 | XtVaSetValues ((Widget) radioButton, | |
da494b40 | 349 | wxFont::GetFontTag(), fontType, |
94b49b93 JS |
350 | NULL); |
351 | } | |
0d57be45 JS |
352 | } |
353 | ||
354 | void wxRadioBox::ChangeBackgroundColour() | |
355 | { | |
94b49b93 JS |
356 | wxWindow::ChangeBackgroundColour(); |
357 | ||
eb6fa4b4 | 358 | int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget)); |
9838df2c | 359 | |
94b49b93 JS |
360 | int i; |
361 | for (i = 0; i < m_noItems; i++) | |
362 | { | |
363 | WXWidget radioButton = m_radioButtons[i]; | |
364 | ||
a8680e3e | 365 | wxDoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE); |
9838df2c JS |
366 | |
367 | XtVaSetValues ((Widget) radioButton, | |
368 | XmNselectColor, selectPixel, | |
369 | NULL); | |
94b49b93 | 370 | } |
0d57be45 JS |
371 | } |
372 | ||
373 | void wxRadioBox::ChangeForegroundColour() | |
374 | { | |
94b49b93 JS |
375 | wxWindow::ChangeForegroundColour(); |
376 | ||
377 | int i; | |
378 | for (i = 0; i < m_noItems; i++) | |
379 | { | |
380 | WXWidget radioButton = m_radioButtons[i]; | |
381 | ||
a8680e3e | 382 | wxDoChangeForegroundColour(radioButton, m_foregroundColour); |
94b49b93 | 383 | } |
0d57be45 JS |
384 | } |
385 | ||
18128cbb MB |
386 | static int CalcOtherDim( int items, int dim ) |
387 | { | |
388 | return items / dim + ( items % dim ? 1 : 0 ); | |
389 | } | |
390 | ||
391 | int wxRadioBox::GetRowCount() const | |
392 | { | |
393 | return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols | |
394 | : CalcOtherDim( GetCount(), m_noRowsOrCols ); | |
395 | } | |
396 | ||
397 | int wxRadioBox::GetColumnCount() const | |
398 | { | |
399 | return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols | |
400 | : CalcOtherDim( GetCount(), m_noRowsOrCols ); | |
401 | } | |
402 | ||
a4294b78 | 403 | void wxRadioBoxCallback (Widget w, XtPointer clientData, |
af0bb3b1 | 404 | XmToggleButtonCallbackStruct * cbs) |
a4294b78 JS |
405 | { |
406 | if (!cbs->set) | |
407 | return; | |
408 | ||
409 | wxRadioBox *item = (wxRadioBox *) clientData; | |
410 | int sel = -1; | |
411 | int i; | |
3bdb8629 | 412 | const wxWidgetArray& buttons = item->GetRadioButtons(); |
3c85ada9 | 413 | for (i = 0; i < item->GetCount(); i++) |
3bdb8629 | 414 | if (((Widget)buttons[i]) == w) |
a4294b78 JS |
415 | sel = i; |
416 | item->SetSel(sel); | |
417 | ||
418 | if (item->InSetValue()) | |
419 | return; | |
420 | ||
421 | wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId()); | |
2ad99bc1 JS |
422 | event.SetInt(sel); |
423 | event.SetString(item->GetStringSelection()); | |
a4294b78 JS |
424 | event.SetEventObject(item); |
425 | item->ProcessCommand (event); | |
426 | } | |
4bb6408c | 427 |