1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "radiobox.h"
17 #define XtDisplay XTDISPLAY
20 #include "wx/radiobox.h"
24 #pragma message disable nosimpint
27 #include <Xm/LabelG.h>
28 #include <Xm/ToggleB.h>
29 #include <Xm/ToggleBG.h>
30 #include <Xm/RowColumn.h>
34 #pragma message enable nosimpint
37 #include "wx/motif/private.h"
39 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
40 XmToggleButtonCallbackStruct
* cbs
);
42 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
45 wxRadioBox::wxRadioBox()
47 m_selectedButton
= -1;
52 m_formWidget
= (WXWidget
) 0;
53 m_frameWidget
= (WXWidget
) 0;
54 m_labelWidget
= (WXWidget
) 0;
55 m_radioButtons
= (WXWidget
*) NULL
;
56 m_radioButtonLabels
= (wxString
*) NULL
;
59 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
60 const wxPoint
& pos
, const wxSize
& size
,
61 int n
, const wxString choices
[],
62 int majorDim
, long style
,
63 const wxValidator
& val
, const wxString
& name
)
65 m_selectedButton
= -1;
67 m_formWidget
= (WXWidget
) 0;
68 m_frameWidget
= (WXWidget
) 0;
69 m_labelWidget
= (WXWidget
) 0;
70 m_radioButtons
= (WXWidget
*) NULL
;
71 m_radioButtonLabels
= (wxString
*) NULL
;
72 m_backgroundColour
= parent
->GetBackgroundColour();
73 m_foregroundColour
= parent
->GetForegroundColour();
74 m_font
= parent
->GetFont();
79 parent
->AddChild(this);
81 m_windowStyle
= (long&)style
;
84 m_windowId
= NewControlId();
88 m_noRowsOrCols
= majorDim
;
93 m_majorDim
= majorDim
;
95 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
97 wxString
label1(wxStripMenuCodes(title
));
99 Widget formWidget
= XtVaCreateManagedWidget (name
.c_str(),
100 xmFormWidgetClass
, parentWidget
,
105 m_formWidget
= (WXWidget
) formWidget
;
107 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
110 wxXmString
text(label1
);
111 (void)XtVaCreateManagedWidget(label1
.c_str(),
113 style
& wxCOLOURED
? xmLabelWidgetClass
114 : xmLabelGadgetClass
,
117 xmLabelWidgetClass
, formWidget
,
119 XmNfontList
, fontList
,
120 XmNlabelString
, text(),
124 Widget frameWidget
= XtVaCreateManagedWidget ("frame",
125 xmFrameWidgetClass
, formWidget
,
126 XmNshadowType
, XmSHADOW_IN
,
127 // XmNmarginHeight, 0,
128 // XmNmarginWidth, 0,
131 m_frameWidget
= (WXWidget
) frameWidget
;
135 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
137 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
138 XmHORIZONTAL
: XmVERTICAL
));
139 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
141 Widget radioBoxWidget
= XmCreateRadioBox (frameWidget
, "radioBoxWidget", args
, 2);
142 m_mainWidget
= (WXWidget
) radioBoxWidget
;
146 XtVaSetValues ((Widget
) m_labelWidget
,
147 XmNtopAttachment
, XmATTACH_FORM
,
148 XmNleftAttachment
, XmATTACH_FORM
,
149 XmNalignment
, XmALIGNMENT_BEGINNING
,
152 XtVaSetValues (radioBoxWidget
,
153 XmNtopAttachment
, m_labelWidget
? XmATTACH_WIDGET
: XmATTACH_FORM
,
154 XmNtopWidget
, m_labelWidget
? (Widget
) m_labelWidget
: formWidget
,
155 XmNbottomAttachment
, XmATTACH_FORM
,
156 XmNleftAttachment
, XmATTACH_FORM
,
157 XmNrightAttachment
, XmATTACH_FORM
,
160 // if (style & wxFLAT)
161 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
163 m_radioButtons
= new WXWidget
[n
];
164 m_radioButtonLabels
= new wxString
[n
];
166 for (i
= 0; i
< n
; i
++)
168 wxString
str(wxStripMenuCodes(choices
[i
]));
169 m_radioButtonLabels
[i
] = str
;
170 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
172 xmToggleButtonGadgetClass
, radioBoxWidget
,
174 xmToggleButtonWidgetClass
, radioBoxWidget
,
176 XmNfontList
, fontList
,
179 #pragma message disable voiincconext
180 // VMS gives here the compiler warning:
181 // conversion from pointer to function to void* permitted
184 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
185 (XtCallbackProc
) this);
187 #pragma message enable voiincconext
193 m_font
= parent
->GetFont();
196 // XtManageChild((Widget) m_formWidget);
197 XtManageChild (radioBoxWidget
);
199 SetCanAddEventHandler(TRUE
);
200 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
202 ChangeBackgroundColour();
208 wxRadioBox::~wxRadioBox()
210 delete[] m_radioButtonLabels
;
211 delete[] m_radioButtons
;
213 DetachWidget(m_formWidget
);
214 DetachWidget(m_mainWidget
);
217 XtDestroyWidget((Widget
) m_labelWidget
);
218 XtDestroyWidget((Widget
) m_mainWidget
);
219 XtDestroyWidget((Widget
) m_formWidget
);
221 m_mainWidget
= (WXWidget
) 0;
222 m_formWidget
= (WXWidget
) 0;
223 m_labelWidget
= (WXWidget
) 0;
226 wxString
wxRadioBox::GetLabel(int item
) const
228 if (item
< 0 || item
>= m_noItems
)
229 return wxEmptyString
;
231 Widget widget
= (Widget
) m_radioButtons
[item
];
234 XtVaGetValues (widget
,
235 XmNlabelString
, &text
,
238 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
240 // Should we free 'text'???
249 return wxEmptyString
;
253 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
255 if (item
< 0 || item
>= m_noItems
)
258 Widget widget
= (Widget
) m_radioButtons
[item
];
261 wxString
label1(wxStripMenuCodes(label
));
262 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
263 XtVaSetValues (widget
,
264 XmNlabelString
, text
,
265 XmNlabelType
, XmSTRING
,
271 int wxRadioBox::FindString(const wxString
& s
) const
274 for (i
= 0; i
< m_noItems
; i
++)
275 if (s
== m_radioButtonLabels
[i
])
280 void wxRadioBox::SetSelection(int n
)
282 if ((n
< 0) || (n
>= m_noItems
))
285 m_selectedButton
= n
;
289 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
292 for (i
= 0; i
< m_noItems
; i
++)
294 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
296 m_inSetValue
= FALSE
;
299 // Get single selection, for single choice list items
300 int wxRadioBox::GetSelection() const
302 return m_selectedButton
;
305 // Find string for position
306 wxString
wxRadioBox::GetString(int n
) const
308 if ((n
< 0) || (n
>= m_noItems
))
309 return wxEmptyString
;
310 return m_radioButtonLabels
[n
];
313 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
315 bool managed
= XtIsManaged((Widget
) m_formWidget
);
318 XtUnmanageChild ((Widget
) m_formWidget
);
320 int xx
= x
; int yy
= y
;
321 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
323 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
324 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
326 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
327 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
330 // Must set the actual RadioBox to be desired size MINUS label size
331 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
334 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
337 actualHeight
= height
- labelHeight
;
341 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
345 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
348 XtManageChild ((Widget
) m_formWidget
);
351 // Enable a specific button
352 void wxRadioBox::Enable(int n
, bool enable
)
354 if ((n
< 0) || (n
>= m_noItems
))
357 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
360 // Enable all controls
361 bool wxRadioBox::Enable(bool enable
)
363 if ( !wxControl::Enable(enable
) )
367 for (i
= 0; i
< m_noItems
; i
++)
368 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
373 bool wxRadioBox::Show(bool show
)
375 // TODO: show/hide all children
376 return wxControl::Show(show
);
379 // Show a specific button
380 void wxRadioBox::Show(int n
, bool show
)
382 // This method isn't complete, and we try do do our best...
383 // It's main purpose isn't for allowing Show/Unshow dynamically,
384 // but rather to provide a way to design wxRadioBox such:
386 // o Val1 o Val2 o Val3
388 // o Val7 o Val8 o Val9
390 // In my case, this is a 'direction' box, and the Show(5,False) is
391 // coupled with an Enable(5,False)
393 if ((n
< 0) || (n
>= m_noItems
))
396 XtVaSetValues ((Widget
) m_radioButtons
[n
],
397 XmNindicatorOn
, (unsigned char) show
,
400 // Please note that this is all we can do: removing the label
401 // if switching to unshow state. However, when switching
402 // to the on state, it's the prog. resp. to call SetLabel(item,...)
405 wxRadioBox::SetLabel (n
, " ");
408 // For single selection items only
409 wxString
wxRadioBox::GetStringSelection () const
411 int sel
= GetSelection ();
413 return this->GetString (sel
);
418 bool wxRadioBox::SetStringSelection (const wxString
& s
)
420 int sel
= FindString (s
);
430 void wxRadioBox::Command (wxCommandEvent
& event
)
432 SetSelection (event
.m_commandInt
);
433 ProcessCommand (event
);
436 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
438 wxWindow::ChangeFont(keepOriginalSize
);
440 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
443 for (i
= 0; i
< m_noItems
; i
++)
445 WXWidget radioButton
= m_radioButtons
[i
];
447 XtVaSetValues ((Widget
) radioButton
,
448 XmNfontList
, fontList
,
449 XmNtopAttachment
, XmATTACH_FORM
,
454 void wxRadioBox::ChangeBackgroundColour()
456 wxWindow::ChangeBackgroundColour();
458 DoChangeBackgroundColour((Widget
) m_frameWidget
, m_backgroundColour
);
460 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
463 for (i
= 0; i
< m_noItems
; i
++)
465 WXWidget radioButton
= m_radioButtons
[i
];
467 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
469 XtVaSetValues ((Widget
) radioButton
,
470 XmNselectColor
, selectPixel
,
475 void wxRadioBox::ChangeForegroundColour()
477 wxWindow::ChangeForegroundColour();
480 for (i
= 0; i
< m_noItems
; i
++)
482 WXWidget radioButton
= m_radioButtons
[i
];
484 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
488 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
489 XmToggleButtonCallbackStruct
* cbs
)
494 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
497 for (i
= 0; i
< item
->Number(); i
++)
498 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
502 if (item
->InSetValue())
505 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
506 event
.m_commandInt
= sel
;
507 event
.SetEventObject(item
);
508 item
->ProcessCommand (event
);