]>
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 | ||
12 | #ifdef __GNUG__ | |
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> | |
33 | #include <Xm/Form.h> | |
9838df2c | 34 | #include <Xm/Frame.h> |
338dd992 JJ |
35 | #ifdef __VMS__ |
36 | #pragma message enable nosimpint | |
37 | #endif | |
a4294b78 | 38 | |
3096bd2f | 39 | #include "wx/motif/private.h" |
a4294b78 JS |
40 | |
41 | void wxRadioBoxCallback (Widget w, XtPointer clientData, | |
af0bb3b1 | 42 | XmToggleButtonCallbackStruct * cbs); |
4bb6408c | 43 | |
4bb6408c | 44 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) |
4bb6408c JS |
45 | |
46 | // Radio box item | |
47 | wxRadioBox::wxRadioBox() | |
48 | { | |
49 | m_selectedButton = -1; | |
50 | m_noItems = 0; | |
51 | m_noRowsOrCols = 0; | |
52 | m_majorDim = 0 ; | |
a4294b78 JS |
53 | |
54 | m_formWidget = (WXWidget) 0; | |
9838df2c | 55 | m_frameWidget = (WXWidget) 0; |
a4294b78 JS |
56 | m_labelWidget = (WXWidget) 0; |
57 | m_radioButtons = (WXWidget*) NULL; | |
58 | m_radioButtonLabels = (wxString*) NULL; | |
4bb6408c JS |
59 | } |
60 | ||
61 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title, | |
62 | const wxPoint& pos, const wxSize& size, | |
63 | int n, const wxString choices[], | |
64 | int majorDim, long style, | |
65 | const wxValidator& val, const wxString& name) | |
66 | { | |
67 | m_selectedButton = -1; | |
68 | m_noItems = n; | |
9838df2c JS |
69 | m_formWidget = (WXWidget) 0; |
70 | m_frameWidget = (WXWidget) 0; | |
a4294b78 JS |
71 | m_labelWidget = (WXWidget) 0; |
72 | m_radioButtons = (WXWidget*) NULL; | |
73 | m_radioButtonLabels = (wxString*) NULL; | |
0d57be45 JS |
74 | m_backgroundColour = parent->GetBackgroundColour(); |
75 | m_foregroundColour = parent->GetForegroundColour(); | |
da175b2c | 76 | m_font = parent->GetFont(); |
4bb6408c JS |
77 | |
78 | SetName(name); | |
79 | SetValidator(val); | |
80 | ||
81 | parent->AddChild(this); | |
82 | ||
83 | m_windowStyle = (long&)style; | |
84 | ||
85 | if (id == -1) | |
86 | m_windowId = NewControlId(); | |
87 | else | |
88 | m_windowId = id; | |
89 | ||
90 | m_noRowsOrCols = majorDim; | |
91 | ||
92 | if (majorDim==0) | |
93 | m_majorDim = n ; | |
94 | else | |
95 | m_majorDim = majorDim ; | |
96 | ||
a4294b78 JS |
97 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
98 | ||
99 | wxString label1(wxStripMenuCodes(title)); | |
100 | ||
31528cd3 | 101 | Widget formWidget = XtVaCreateManagedWidget (name.c_str(), |
af0bb3b1 VZ |
102 | xmFormWidgetClass, parentWidget, |
103 | XmNmarginHeight, 0, | |
104 | XmNmarginWidth, 0, | |
105 | NULL); | |
a4294b78 JS |
106 | |
107 | m_formWidget = (WXWidget) formWidget; | |
108 | ||
da175b2c | 109 | XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget)); |
a4294b78 JS |
110 | if (label1 != "") |
111 | { | |
31528cd3 VZ |
112 | wxXmString text(label1); |
113 | (void)XtVaCreateManagedWidget(label1.c_str(), | |
a4294b78 | 114 | #if wxUSE_GADGETS |
31528cd3 VZ |
115 | style & wxCOLOURED ? xmLabelWidgetClass |
116 | : xmLabelGadgetClass, | |
af0bb3b1 | 117 | formWidget, |
a4294b78 | 118 | #else |
af0bb3b1 | 119 | xmLabelWidgetClass, formWidget, |
a4294b78 | 120 | #endif |
ea57084d | 121 | XmNfontList, fontList, |
98300330 | 122 | XmNlabelString, text(), |
af0bb3b1 | 123 | NULL); |
a4294b78 JS |
124 | } |
125 | ||
9838df2c | 126 | Widget frameWidget = XtVaCreateManagedWidget ("frame", |
af0bb3b1 | 127 | xmFrameWidgetClass, formWidget, |
9838df2c | 128 | XmNshadowType, XmSHADOW_IN, |
af0bb3b1 VZ |
129 | // XmNmarginHeight, 0, |
130 | // XmNmarginWidth, 0, | |
131 | NULL); | |
9838df2c JS |
132 | |
133 | m_frameWidget = (WXWidget) frameWidget; | |
134 | ||
a4294b78 JS |
135 | Arg args[3]; |
136 | ||
978db70e | 137 | m_majorDim = (n + m_majorDim - 1) / m_majorDim; |
a4294b78 JS |
138 | |
139 | XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ? | |
af0bb3b1 | 140 | XmHORIZONTAL : XmVERTICAL)); |
98300330 | 141 | XtSetArg (args[1], XmNnumColumns, m_majorDim); |
a4294b78 | 142 | |
9838df2c | 143 | Widget radioBoxWidget = XmCreateRadioBox (frameWidget, "radioBoxWidget", args, 2); |
a4294b78 | 144 | m_mainWidget = (WXWidget) radioBoxWidget; |
4bb6408c | 145 | |
a4294b78 JS |
146 | |
147 | if (m_labelWidget) | |
af0bb3b1 VZ |
148 | XtVaSetValues ((Widget) m_labelWidget, |
149 | XmNtopAttachment, XmATTACH_FORM, | |
150 | XmNleftAttachment, XmATTACH_FORM, | |
151 | XmNalignment, XmALIGNMENT_BEGINNING, | |
152 | NULL); | |
a4294b78 JS |
153 | |
154 | XtVaSetValues (radioBoxWidget, | |
af0bb3b1 VZ |
155 | XmNtopAttachment, m_labelWidget ? XmATTACH_WIDGET : XmATTACH_FORM, |
156 | XmNtopWidget, m_labelWidget ? (Widget) m_labelWidget : formWidget, | |
157 | XmNbottomAttachment, XmATTACH_FORM, | |
158 | XmNleftAttachment, XmATTACH_FORM, | |
159 | XmNrightAttachment, XmATTACH_FORM, | |
160 | NULL); | |
a4294b78 JS |
161 | |
162 | // if (style & wxFLAT) | |
163 | // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL); | |
164 | ||
165 | m_radioButtons = new WXWidget[n]; | |
166 | m_radioButtonLabels = new wxString[n]; | |
167 | int i; | |
168 | for (i = 0; i < n; i++) | |
169 | { | |
170 | wxString str(wxStripMenuCodes(choices[i])); | |
171 | m_radioButtonLabels[i] = str; | |
172 | m_radioButtons[i] = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) str, | |
173 | #if wxUSE_GADGETS | |
af0bb3b1 | 174 | xmToggleButtonGadgetClass, radioBoxWidget, |
a4294b78 | 175 | #else |
af0bb3b1 | 176 | xmToggleButtonWidgetClass, radioBoxWidget, |
a4294b78 | 177 | #endif |
ea57084d | 178 | XmNfontList, fontList, |
af0bb3b1 | 179 | NULL); |
a4294b78 | 180 | XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback, |
d7dcb939 | 181 | (XtPointer) this); |
a4294b78 JS |
182 | } |
183 | SetSelection (0); | |
184 | ||
da175b2c | 185 | m_font = parent->GetFont(); |
4b5f3fe6 JS |
186 | ChangeFont(FALSE); |
187 | ||
9838df2c | 188 | // XtManageChild((Widget) m_formWidget); |
a4294b78 JS |
189 | XtManageChild (radioBoxWidget); |
190 | ||
191 | SetCanAddEventHandler(TRUE); | |
192 | AttachWidget (parent, m_mainWidget, m_formWidget, pos.x, pos.y, size.x, size.y); | |
193 | ||
0d57be45 | 194 | ChangeBackgroundColour(); |
a4294b78 JS |
195 | |
196 | return TRUE; | |
4bb6408c JS |
197 | } |
198 | ||
199 | ||
200 | wxRadioBox::~wxRadioBox() | |
201 | { | |
a4294b78 JS |
202 | delete[] m_radioButtonLabels; |
203 | delete[] m_radioButtons; | |
15d5ab67 | 204 | |
b412f9be | 205 | DetachWidget(m_formWidget); |
15d5ab67 JS |
206 | DetachWidget(m_mainWidget); |
207 | ||
208 | if (m_labelWidget) | |
209 | XtDestroyWidget((Widget) m_labelWidget); | |
210 | XtDestroyWidget((Widget) m_mainWidget); | |
211 | XtDestroyWidget((Widget) m_formWidget); | |
212 | ||
213 | m_mainWidget = (WXWidget) 0; | |
214 | m_formWidget = (WXWidget) 0; | |
215 | m_labelWidget = (WXWidget) 0; | |
4bb6408c JS |
216 | } |
217 | ||
18128cbb | 218 | void wxRadioBox::SetString(int item, const wxString& label) |
4bb6408c | 219 | { |
a4294b78 JS |
220 | if (item < 0 || item >= m_noItems) |
221 | return; | |
222 | ||
223 | Widget widget = (Widget) m_radioButtons[item]; | |
224 | if (label != "") | |
225 | { | |
226 | wxString label1(wxStripMenuCodes(label)); | |
227 | XmString text = XmStringCreateSimple ((char*) (const char*) label1); | |
228 | XtVaSetValues (widget, | |
af0bb3b1 VZ |
229 | XmNlabelString, text, |
230 | XmNlabelType, XmSTRING, | |
231 | NULL); | |
a4294b78 JS |
232 | XmStringFree (text); |
233 | } | |
4bb6408c JS |
234 | } |
235 | ||
236 | int wxRadioBox::FindString(const wxString& s) const | |
237 | { | |
a4294b78 JS |
238 | int i; |
239 | for (i = 0; i < m_noItems; i++) | |
240 | if (s == m_radioButtonLabels[i]) | |
241 | return i; | |
4bb6408c JS |
242 | return -1; |
243 | } | |
244 | ||
245 | void wxRadioBox::SetSelection(int n) | |
246 | { | |
247 | if ((n < 0) || (n >= m_noItems)) | |
248 | return; | |
4bb6408c JS |
249 | |
250 | m_selectedButton = n; | |
a4294b78 JS |
251 | |
252 | m_inSetValue = TRUE; | |
253 | ||
254 | XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE); | |
255 | ||
256 | int i; | |
257 | for (i = 0; i < m_noItems; i++) | |
258 | if (i != n) | |
259 | XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE); | |
260 | ||
261 | m_inSetValue = FALSE; | |
4bb6408c JS |
262 | } |
263 | ||
264 | // Get single selection, for single choice list items | |
265 | int wxRadioBox::GetSelection() const | |
266 | { | |
267 | return m_selectedButton; | |
268 | } | |
269 | ||
270 | // Find string for position | |
271 | wxString wxRadioBox::GetString(int n) const | |
272 | { | |
a4294b78 JS |
273 | if ((n < 0) || (n >= m_noItems)) |
274 | return wxEmptyString; | |
275 | return m_radioButtonLabels[n]; | |
4bb6408c JS |
276 | } |
277 | ||
bfc6fde4 | 278 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
4bb6408c | 279 | { |
a4294b78 | 280 | bool managed = XtIsManaged((Widget) m_formWidget); |
4bb6408c | 281 | |
a4294b78 JS |
282 | if (managed) |
283 | XtUnmanageChild ((Widget) m_formWidget); | |
4bb6408c | 284 | |
a4294b78 JS |
285 | int xx = x; int yy = y; |
286 | AdjustForParentClientOrigin(xx, yy, sizeFlags); | |
4bb6408c | 287 | |
a4294b78 JS |
288 | if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
289 | XtVaSetValues ((Widget) m_formWidget, XmNleftAttachment, XmATTACH_SELF, | |
af0bb3b1 | 290 | XmNx, xx, NULL); |
a4294b78 JS |
291 | if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
292 | XtVaSetValues ((Widget) m_formWidget, XmNtopAttachment, XmATTACH_SELF, | |
af0bb3b1 | 293 | XmNy, yy, NULL); |
4bb6408c | 294 | |
a4294b78 JS |
295 | // Must set the actual RadioBox to be desired size MINUS label size |
296 | Dimension labelWidth = 0, labelHeight = 0, actualWidth = 0, actualHeight = 0; | |
4bb6408c | 297 | |
a4294b78 JS |
298 | if (m_labelWidget) |
299 | XtVaGetValues ((Widget) m_labelWidget, XmNwidth, &labelWidth, XmNheight, &labelHeight, NULL); | |
4bb6408c | 300 | |
a4294b78 JS |
301 | actualWidth = width; |
302 | actualHeight = height - labelHeight; | |
303 | ||
304 | if (width > -1) | |
305 | { | |
306 | XtVaSetValues ((Widget) m_mainWidget, XmNwidth, actualWidth, NULL); | |
307 | } | |
308 | if (height > -1) | |
309 | { | |
310 | XtVaSetValues ((Widget) m_mainWidget, XmNheight, actualHeight, NULL); | |
311 | } | |
312 | if (managed) | |
313 | XtManageChild ((Widget) m_formWidget); | |
4bb6408c JS |
314 | } |
315 | ||
316 | // Enable a specific button | |
a4294b78 | 317 | void wxRadioBox::Enable(int n, bool enable) |
4bb6408c | 318 | { |
a4294b78 JS |
319 | if ((n < 0) || (n >= m_noItems)) |
320 | return; | |
321 | ||
322 | XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable); | |
4bb6408c JS |
323 | } |
324 | ||
325 | // Enable all controls | |
af0bb3b1 | 326 | bool wxRadioBox::Enable(bool enable) |
4bb6408c | 327 | { |
af0bb3b1 VZ |
328 | if ( !wxControl::Enable(enable) ) |
329 | return FALSE; | |
4bb6408c | 330 | |
a4294b78 JS |
331 | int i; |
332 | for (i = 0; i < m_noItems; i++) | |
333 | XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable); | |
af0bb3b1 VZ |
334 | |
335 | return TRUE; | |
4bb6408c JS |
336 | } |
337 | ||
9838df2c JS |
338 | bool wxRadioBox::Show(bool show) |
339 | { | |
340 | // TODO: show/hide all children | |
341 | return wxControl::Show(show); | |
342 | } | |
343 | ||
4bb6408c | 344 | // Show a specific button |
a4294b78 | 345 | void wxRadioBox::Show(int n, bool show) |
4bb6408c | 346 | { |
a4294b78 JS |
347 | // This method isn't complete, and we try do do our best... |
348 | // It's main purpose isn't for allowing Show/Unshow dynamically, | |
349 | // but rather to provide a way to design wxRadioBox such: | |
350 | // | |
351 | // o Val1 o Val2 o Val3 | |
352 | // o Val4 o Val6 | |
353 | // o Val7 o Val8 o Val9 | |
354 | // | |
355 | // In my case, this is a 'direction' box, and the Show(5,False) is | |
356 | // coupled with an Enable(5,False) | |
357 | // | |
358 | if ((n < 0) || (n >= m_noItems)) | |
359 | return; | |
360 | ||
361 | XtVaSetValues ((Widget) m_radioButtons[n], | |
362 | XmNindicatorOn, (unsigned char) show, | |
363 | NULL); | |
364 | ||
365 | // Please note that this is all we can do: removing the label | |
366 | // if switching to unshow state. However, when switching | |
18128cbb | 367 | // to the on state, it's the prog. resp. to call SetString(item,...) |
a4294b78 JS |
368 | // after this call!! |
369 | if (!show) | |
18128cbb | 370 | wxRadioBox::SetString (n, " "); |
4bb6408c JS |
371 | } |
372 | ||
373 | // For single selection items only | |
374 | wxString wxRadioBox::GetStringSelection () const | |
375 | { | |
376 | int sel = GetSelection (); | |
377 | if (sel > -1) | |
378 | return this->GetString (sel); | |
379 | else | |
380 | return wxString(""); | |
381 | } | |
382 | ||
383 | bool wxRadioBox::SetStringSelection (const wxString& s) | |
384 | { | |
385 | int sel = FindString (s); | |
386 | if (sel > -1) | |
387 | { | |
388 | SetSelection (sel); | |
389 | return TRUE; | |
390 | } | |
391 | else | |
392 | return FALSE; | |
393 | } | |
394 | ||
395 | void wxRadioBox::Command (wxCommandEvent & event) | |
396 | { | |
397 | SetSelection (event.m_commandInt); | |
398 | ProcessCommand (event); | |
399 | } | |
400 | ||
4b5f3fe6 | 401 | void wxRadioBox::ChangeFont(bool keepOriginalSize) |
0d57be45 | 402 | { |
94b49b93 JS |
403 | wxWindow::ChangeFont(keepOriginalSize); |
404 | ||
da175b2c | 405 | XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) GetTopWidget())); |
94b49b93 JS |
406 | |
407 | int i; | |
408 | for (i = 0; i < m_noItems; i++) | |
409 | { | |
410 | WXWidget radioButton = m_radioButtons[i]; | |
411 | ||
412 | XtVaSetValues ((Widget) radioButton, | |
413 | XmNfontList, fontList, | |
af0bb3b1 | 414 | XmNtopAttachment, XmATTACH_FORM, |
94b49b93 JS |
415 | NULL); |
416 | } | |
0d57be45 JS |
417 | } |
418 | ||
419 | void wxRadioBox::ChangeBackgroundColour() | |
420 | { | |
94b49b93 JS |
421 | wxWindow::ChangeBackgroundColour(); |
422 | ||
9838df2c JS |
423 | DoChangeBackgroundColour((Widget) m_frameWidget, m_backgroundColour); |
424 | ||
425 | int selectPixel = wxBLACK->AllocColour(wxGetDisplay()); | |
426 | ||
94b49b93 JS |
427 | int i; |
428 | for (i = 0; i < m_noItems; i++) | |
429 | { | |
430 | WXWidget radioButton = m_radioButtons[i]; | |
431 | ||
432 | DoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE); | |
9838df2c JS |
433 | |
434 | XtVaSetValues ((Widget) radioButton, | |
435 | XmNselectColor, selectPixel, | |
436 | NULL); | |
94b49b93 | 437 | } |
0d57be45 JS |
438 | } |
439 | ||
440 | void wxRadioBox::ChangeForegroundColour() | |
441 | { | |
94b49b93 JS |
442 | wxWindow::ChangeForegroundColour(); |
443 | ||
444 | int i; | |
445 | for (i = 0; i < m_noItems; i++) | |
446 | { | |
447 | WXWidget radioButton = m_radioButtons[i]; | |
448 | ||
449 | DoChangeForegroundColour(radioButton, m_foregroundColour); | |
450 | } | |
0d57be45 JS |
451 | } |
452 | ||
18128cbb MB |
453 | static int CalcOtherDim( int items, int dim ) |
454 | { | |
455 | return items / dim + ( items % dim ? 1 : 0 ); | |
456 | } | |
457 | ||
458 | int wxRadioBox::GetRowCount() const | |
459 | { | |
460 | return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols | |
461 | : CalcOtherDim( GetCount(), m_noRowsOrCols ); | |
462 | } | |
463 | ||
464 | int wxRadioBox::GetColumnCount() const | |
465 | { | |
466 | return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols | |
467 | : CalcOtherDim( GetCount(), m_noRowsOrCols ); | |
468 | } | |
469 | ||
a4294b78 | 470 | void wxRadioBoxCallback (Widget w, XtPointer clientData, |
af0bb3b1 | 471 | XmToggleButtonCallbackStruct * cbs) |
a4294b78 JS |
472 | { |
473 | if (!cbs->set) | |
474 | return; | |
475 | ||
476 | wxRadioBox *item = (wxRadioBox *) clientData; | |
477 | int sel = -1; | |
478 | int i; | |
3c85ada9 | 479 | for (i = 0; i < item->GetCount(); i++) |
a4294b78 JS |
480 | if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w)) |
481 | sel = i; | |
482 | item->SetSel(sel); | |
483 | ||
484 | if (item->InSetValue()) | |
485 | return; | |
486 | ||
487 | wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId()); | |
488 | event.m_commandInt = sel; | |
489 | event.SetEventObject(item); | |
490 | item->ProcessCommand (event); | |
491 | } | |
4bb6408c | 492 |