1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "radiobox.h"
16 #include "wx/radiobox.h"
20 #pragma message disable nosimpint
23 #include <Xm/LabelG.h>
24 #include <Xm/ToggleB.h>
25 #include <Xm/ToggleBG.h>
26 #include <Xm/RowColumn.h>
30 #pragma message enable nosimpint
33 #include "wx/motif/private.h"
35 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
36 XmToggleButtonCallbackStruct
* cbs
);
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
43 wxRadioBox::wxRadioBox()
45 m_selectedButton
= -1;
50 m_formWidget
= (WXWidget
) 0;
51 m_frameWidget
= (WXWidget
) 0;
52 m_labelWidget
= (WXWidget
) 0;
53 m_radioButtons
= (WXWidget
*) NULL
;
54 m_radioButtonLabels
= (wxString
*) NULL
;
57 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
58 const wxPoint
& pos
, const wxSize
& size
,
59 int n
, const wxString choices
[],
60 int majorDim
, long style
,
61 const wxValidator
& val
, const wxString
& name
)
63 m_selectedButton
= -1;
65 m_formWidget
= (WXWidget
) 0;
66 m_frameWidget
= (WXWidget
) 0;
67 m_labelWidget
= (WXWidget
) 0;
68 m_radioButtons
= (WXWidget
*) NULL
;
69 m_radioButtonLabels
= (wxString
*) NULL
;
70 m_backgroundColour
= parent
->GetBackgroundColour();
71 m_foregroundColour
= parent
->GetForegroundColour();
72 m_font
= parent
->GetFont();
77 parent
->AddChild(this);
79 m_windowStyle
= (long&)style
;
82 m_windowId
= NewControlId();
86 m_noRowsOrCols
= majorDim
;
91 m_majorDim
= majorDim
;
93 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
95 wxString
label1(wxStripMenuCodes(title
));
97 Widget formWidget
= XtVaCreateManagedWidget (name
.c_str(),
98 xmFormWidgetClass
, parentWidget
,
103 m_formWidget
= (WXWidget
) formWidget
;
105 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
108 wxXmString
text(label1
);
109 (void)XtVaCreateManagedWidget(label1
.c_str(),
111 style
& wxCOLOURED
? xmLabelWidgetClass
112 : xmLabelGadgetClass
,
115 xmLabelWidgetClass
, formWidget
,
117 XmNfontList
, fontList
,
118 XmNlabelString
, text(),
122 Widget frameWidget
= XtVaCreateManagedWidget ("frame",
123 xmFrameWidgetClass
, formWidget
,
124 XmNshadowType
, XmSHADOW_IN
,
125 // XmNmarginHeight, 0,
126 // XmNmarginWidth, 0,
129 m_frameWidget
= (WXWidget
) frameWidget
;
133 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
135 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
136 XmHORIZONTAL
: XmVERTICAL
));
137 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
139 Widget radioBoxWidget
= XmCreateRadioBox (frameWidget
, "radioBoxWidget", args
, 2);
140 m_mainWidget
= (WXWidget
) radioBoxWidget
;
144 XtVaSetValues ((Widget
) m_labelWidget
,
145 XmNtopAttachment
, XmATTACH_FORM
,
146 XmNleftAttachment
, XmATTACH_FORM
,
147 XmNalignment
, XmALIGNMENT_BEGINNING
,
150 XtVaSetValues (radioBoxWidget
,
151 XmNtopAttachment
, m_labelWidget
? XmATTACH_WIDGET
: XmATTACH_FORM
,
152 XmNtopWidget
, m_labelWidget
? (Widget
) m_labelWidget
: formWidget
,
153 XmNbottomAttachment
, XmATTACH_FORM
,
154 XmNleftAttachment
, XmATTACH_FORM
,
155 XmNrightAttachment
, XmATTACH_FORM
,
158 // if (style & wxFLAT)
159 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
161 m_radioButtons
= new WXWidget
[n
];
162 m_radioButtonLabels
= new wxString
[n
];
164 for (i
= 0; i
< n
; i
++)
166 wxString
str(wxStripMenuCodes(choices
[i
]));
167 m_radioButtonLabels
[i
] = str
;
168 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
170 xmToggleButtonGadgetClass
, radioBoxWidget
,
172 xmToggleButtonWidgetClass
, radioBoxWidget
,
174 XmNfontList
, fontList
,
177 #pragma message disable voiincconext
178 // VMS gives here the compiler warning:
179 // conversion from pointer to function to void* permitted
182 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
183 (XtCallbackProc
) this);
185 #pragma message enable voiincconext
191 m_font
= parent
->GetFont();
194 // XtManageChild((Widget) m_formWidget);
195 XtManageChild (radioBoxWidget
);
197 SetCanAddEventHandler(TRUE
);
198 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
200 ChangeBackgroundColour();
206 wxRadioBox::~wxRadioBox()
208 delete[] m_radioButtonLabels
;
209 delete[] m_radioButtons
;
211 DetachWidget(m_formWidget
);
212 DetachWidget(m_mainWidget
);
215 XtDestroyWidget((Widget
) m_labelWidget
);
216 XtDestroyWidget((Widget
) m_mainWidget
);
217 XtDestroyWidget((Widget
) m_formWidget
);
219 m_mainWidget
= (WXWidget
) 0;
220 m_formWidget
= (WXWidget
) 0;
221 m_labelWidget
= (WXWidget
) 0;
224 wxString
wxRadioBox::GetLabel(int item
) const
226 if (item
< 0 || item
>= m_noItems
)
227 return wxEmptyString
;
229 Widget widget
= (Widget
) m_radioButtons
[item
];
232 XtVaGetValues (widget
,
233 XmNlabelString
, &text
,
236 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
238 // Should we free 'text'???
247 return wxEmptyString
;
251 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
253 if (item
< 0 || item
>= m_noItems
)
256 Widget widget
= (Widget
) m_radioButtons
[item
];
259 wxString
label1(wxStripMenuCodes(label
));
260 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
261 XtVaSetValues (widget
,
262 XmNlabelString
, text
,
263 XmNlabelType
, XmSTRING
,
269 int wxRadioBox::FindString(const wxString
& s
) const
272 for (i
= 0; i
< m_noItems
; i
++)
273 if (s
== m_radioButtonLabels
[i
])
278 void wxRadioBox::SetSelection(int n
)
280 if ((n
< 0) || (n
>= m_noItems
))
283 m_selectedButton
= n
;
287 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
290 for (i
= 0; i
< m_noItems
; i
++)
292 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
294 m_inSetValue
= FALSE
;
297 // Get single selection, for single choice list items
298 int wxRadioBox::GetSelection() const
300 return m_selectedButton
;
303 // Find string for position
304 wxString
wxRadioBox::GetString(int n
) const
306 if ((n
< 0) || (n
>= m_noItems
))
307 return wxEmptyString
;
308 return m_radioButtonLabels
[n
];
311 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
313 bool managed
= XtIsManaged((Widget
) m_formWidget
);
316 XtUnmanageChild ((Widget
) m_formWidget
);
318 int xx
= x
; int yy
= y
;
319 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
321 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
322 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
324 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
325 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
328 // Must set the actual RadioBox to be desired size MINUS label size
329 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
332 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
335 actualHeight
= height
- labelHeight
;
339 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
343 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
346 XtManageChild ((Widget
) m_formWidget
);
349 // Enable a specific button
350 void wxRadioBox::Enable(int n
, bool enable
)
352 if ((n
< 0) || (n
>= m_noItems
))
355 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
358 // Enable all controls
359 bool wxRadioBox::Enable(bool enable
)
361 if ( !wxControl::Enable(enable
) )
365 for (i
= 0; i
< m_noItems
; i
++)
366 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
371 bool wxRadioBox::Show(bool show
)
373 // TODO: show/hide all children
374 return wxControl::Show(show
);
377 // Show a specific button
378 void wxRadioBox::Show(int n
, bool show
)
380 // This method isn't complete, and we try do do our best...
381 // It's main purpose isn't for allowing Show/Unshow dynamically,
382 // but rather to provide a way to design wxRadioBox such:
384 // o Val1 o Val2 o Val3
386 // o Val7 o Val8 o Val9
388 // In my case, this is a 'direction' box, and the Show(5,False) is
389 // coupled with an Enable(5,False)
391 if ((n
< 0) || (n
>= m_noItems
))
394 XtVaSetValues ((Widget
) m_radioButtons
[n
],
395 XmNindicatorOn
, (unsigned char) show
,
398 // Please note that this is all we can do: removing the label
399 // if switching to unshow state. However, when switching
400 // to the on state, it's the prog. resp. to call SetLabel(item,...)
403 wxRadioBox::SetLabel (n
, " ");
406 // For single selection items only
407 wxString
wxRadioBox::GetStringSelection () const
409 int sel
= GetSelection ();
411 return this->GetString (sel
);
416 bool wxRadioBox::SetStringSelection (const wxString
& s
)
418 int sel
= FindString (s
);
428 void wxRadioBox::Command (wxCommandEvent
& event
)
430 SetSelection (event
.m_commandInt
);
431 ProcessCommand (event
);
434 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
436 wxWindow::ChangeFont(keepOriginalSize
);
438 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
441 for (i
= 0; i
< m_noItems
; i
++)
443 WXWidget radioButton
= m_radioButtons
[i
];
445 XtVaSetValues ((Widget
) radioButton
,
446 XmNfontList
, fontList
,
447 XmNtopAttachment
, XmATTACH_FORM
,
452 void wxRadioBox::ChangeBackgroundColour()
454 wxWindow::ChangeBackgroundColour();
456 DoChangeBackgroundColour((Widget
) m_frameWidget
, m_backgroundColour
);
458 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
461 for (i
= 0; i
< m_noItems
; i
++)
463 WXWidget radioButton
= m_radioButtons
[i
];
465 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
467 XtVaSetValues ((Widget
) radioButton
,
468 XmNselectColor
, selectPixel
,
473 void wxRadioBox::ChangeForegroundColour()
475 wxWindow::ChangeForegroundColour();
478 for (i
= 0; i
< m_noItems
; i
++)
480 WXWidget radioButton
= m_radioButtons
[i
];
482 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
486 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
487 XmToggleButtonCallbackStruct
* cbs
)
492 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
495 for (i
= 0; i
< item
->Number(); i
++)
496 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
500 if (item
->InSetValue())
503 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
504 event
.m_commandInt
= sel
;
505 event
.SetEventObject(item
);
506 item
->ProcessCommand (event
);