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