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 #include <Xm/LabelG.h>
21 #include <Xm/ToggleB.h>
22 #include <Xm/ToggleBG.h>
23 #include <Xm/RowColumn.h>
27 #include <wx/motif/private.h>
29 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
30 XmToggleButtonCallbackStruct
* cbs
);
32 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
37 wxRadioBox::wxRadioBox()
39 m_selectedButton
= -1;
44 m_formWidget
= (WXWidget
) 0;
45 m_frameWidget
= (WXWidget
) 0;
46 m_labelWidget
= (WXWidget
) 0;
47 m_radioButtons
= (WXWidget
*) NULL
;
48 m_radioButtonLabels
= (wxString
*) NULL
;
51 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
52 const wxPoint
& pos
, const wxSize
& size
,
53 int n
, const wxString choices
[],
54 int majorDim
, long style
,
55 const wxValidator
& val
, const wxString
& name
)
57 m_selectedButton
= -1;
59 m_formWidget
= (WXWidget
) 0;
60 m_frameWidget
= (WXWidget
) 0;
61 m_labelWidget
= (WXWidget
) 0;
62 m_radioButtons
= (WXWidget
*) NULL
;
63 m_radioButtonLabels
= (wxString
*) NULL
;
64 m_backgroundColour
= parent
->GetBackgroundColour();
65 m_foregroundColour
= parent
->GetForegroundColour();
66 m_font
= parent
->GetFont();
71 parent
->AddChild(this);
73 m_windowStyle
= (long&)style
;
76 m_windowId
= NewControlId();
80 m_noRowsOrCols
= majorDim
;
85 m_majorDim
= majorDim
;
87 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
89 wxString
label1(wxStripMenuCodes(title
));
91 wxXmString
text(label1
.c_str());
93 Widget formWidget
= XtVaCreateManagedWidget (name
.c_str(),
94 xmFormWidgetClass
, parentWidget
,
99 m_formWidget
= (WXWidget
) formWidget
;
101 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
104 wxXmString
text(label1
);
105 (void)XtVaCreateManagedWidget(label1
.c_str(),
107 style
& wxCOLOURED
? xmLabelWidgetClass
108 : xmLabelGadgetClass
,
111 xmLabelWidgetClass
, formWidget
,
113 XmNfontList
, fontList
,
114 XmNlabelString
, text
,
118 Widget frameWidget
= XtVaCreateManagedWidget ("frame",
119 xmFrameWidgetClass
, formWidget
,
120 XmNshadowType
, XmSHADOW_IN
,
121 // XmNmarginHeight, 0,
122 // XmNmarginWidth, 0,
125 m_frameWidget
= (WXWidget
) frameWidget
;
129 majorDim
= (n
+ majorDim
- 1) / majorDim
;
131 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
132 XmHORIZONTAL
: XmVERTICAL
));
133 XtSetArg (args
[1], XmNnumColumns
, majorDim
);
135 Widget radioBoxWidget
= XmCreateRadioBox (frameWidget
, "radioBoxWidget", args
, 2);
136 m_mainWidget
= (WXWidget
) radioBoxWidget
;
140 XtVaSetValues ((Widget
) m_labelWidget
,
141 XmNtopAttachment
, XmATTACH_FORM
,
142 XmNleftAttachment
, XmATTACH_FORM
,
143 XmNalignment
, XmALIGNMENT_BEGINNING
,
146 XtVaSetValues (radioBoxWidget
,
147 XmNtopAttachment
, m_labelWidget
? XmATTACH_WIDGET
: XmATTACH_FORM
,
148 XmNtopWidget
, m_labelWidget
? (Widget
) m_labelWidget
: formWidget
,
149 XmNbottomAttachment
, XmATTACH_FORM
,
150 XmNleftAttachment
, XmATTACH_FORM
,
151 XmNrightAttachment
, XmATTACH_FORM
,
154 // if (style & wxFLAT)
155 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
157 m_radioButtons
= new WXWidget
[n
];
158 m_radioButtonLabels
= new wxString
[n
];
160 for (i
= 0; i
< n
; i
++)
162 wxString
str(wxStripMenuCodes(choices
[i
]));
163 m_radioButtonLabels
[i
] = str
;
164 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
166 xmToggleButtonGadgetClass
, radioBoxWidget
,
168 xmToggleButtonWidgetClass
, radioBoxWidget
,
170 XmNfontList
, fontList
,
172 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
173 (XtCallbackProc
) this);
178 m_font
= parent
->GetFont();
181 // XtManageChild((Widget) m_formWidget);
182 XtManageChild (radioBoxWidget
);
184 SetCanAddEventHandler(TRUE
);
185 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
187 ChangeBackgroundColour();
193 wxRadioBox::~wxRadioBox()
195 delete[] m_radioButtonLabels
;
196 delete[] m_radioButtons
;
198 DetachWidget(m_formWidget
);
199 DetachWidget(m_mainWidget
);
202 XtDestroyWidget((Widget
) m_labelWidget
);
203 XtDestroyWidget((Widget
) m_mainWidget
);
204 XtDestroyWidget((Widget
) m_formWidget
);
206 m_mainWidget
= (WXWidget
) 0;
207 m_formWidget
= (WXWidget
) 0;
208 m_labelWidget
= (WXWidget
) 0;
211 wxString
wxRadioBox::GetLabel(int item
) const
213 if (item
< 0 || item
>= m_noItems
)
214 return wxEmptyString
;
216 Widget widget
= (Widget
) m_radioButtons
[item
];
219 XtVaGetValues (widget
,
220 XmNlabelString
, &text
,
223 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
225 // Should we free 'text'???
234 return wxEmptyString
;
238 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
240 if (item
< 0 || item
>= m_noItems
)
243 Widget widget
= (Widget
) m_radioButtons
[item
];
246 wxString
label1(wxStripMenuCodes(label
));
247 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
248 XtVaSetValues (widget
,
249 XmNlabelString
, text
,
250 XmNlabelType
, XmSTRING
,
256 int wxRadioBox::FindString(const wxString
& s
) const
259 for (i
= 0; i
< m_noItems
; i
++)
260 if (s
== m_radioButtonLabels
[i
])
265 void wxRadioBox::SetSelection(int n
)
267 if ((n
< 0) || (n
>= m_noItems
))
270 m_selectedButton
= n
;
274 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
277 for (i
= 0; i
< m_noItems
; i
++)
279 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
281 m_inSetValue
= FALSE
;
284 // Get single selection, for single choice list items
285 int wxRadioBox::GetSelection() const
287 return m_selectedButton
;
290 // Find string for position
291 wxString
wxRadioBox::GetString(int n
) const
293 if ((n
< 0) || (n
>= m_noItems
))
294 return wxEmptyString
;
295 return m_radioButtonLabels
[n
];
298 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
300 bool managed
= XtIsManaged((Widget
) m_formWidget
);
303 XtUnmanageChild ((Widget
) m_formWidget
);
305 int xx
= x
; int yy
= y
;
306 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
308 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
309 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
311 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
312 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
315 // Must set the actual RadioBox to be desired size MINUS label size
316 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
319 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
322 actualHeight
= height
- labelHeight
;
326 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
330 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
333 XtManageChild ((Widget
) m_formWidget
);
336 // Enable a specific button
337 void wxRadioBox::Enable(int n
, bool enable
)
339 if ((n
< 0) || (n
>= m_noItems
))
342 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
345 // Enable all controls
346 bool wxRadioBox::Enable(bool enable
)
348 if ( !wxControl::Enable(enable
) )
352 for (i
= 0; i
< m_noItems
; i
++)
353 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
358 bool wxRadioBox::Show(bool show
)
360 // TODO: show/hide all children
361 return wxControl::Show(show
);
364 // Show a specific button
365 void wxRadioBox::Show(int n
, bool show
)
367 // This method isn't complete, and we try do do our best...
368 // It's main purpose isn't for allowing Show/Unshow dynamically,
369 // but rather to provide a way to design wxRadioBox such:
371 // o Val1 o Val2 o Val3
373 // o Val7 o Val8 o Val9
375 // In my case, this is a 'direction' box, and the Show(5,False) is
376 // coupled with an Enable(5,False)
378 if ((n
< 0) || (n
>= m_noItems
))
381 XtVaSetValues ((Widget
) m_radioButtons
[n
],
382 XmNindicatorOn
, (unsigned char) show
,
385 // Please note that this is all we can do: removing the label
386 // if switching to unshow state. However, when switching
387 // to the on state, it's the prog. resp. to call SetLabel(item,...)
390 wxRadioBox::SetLabel (n
, " ");
393 // For single selection items only
394 wxString
wxRadioBox::GetStringSelection () const
396 int sel
= GetSelection ();
398 return this->GetString (sel
);
403 bool wxRadioBox::SetStringSelection (const wxString
& s
)
405 int sel
= FindString (s
);
415 void wxRadioBox::Command (wxCommandEvent
& event
)
417 SetSelection (event
.m_commandInt
);
418 ProcessCommand (event
);
421 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
423 wxWindow::ChangeFont(keepOriginalSize
);
425 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
428 for (i
= 0; i
< m_noItems
; i
++)
430 WXWidget radioButton
= m_radioButtons
[i
];
432 XtVaSetValues ((Widget
) radioButton
,
433 XmNfontList
, fontList
,
434 XmNtopAttachment
, XmATTACH_FORM
,
439 void wxRadioBox::ChangeBackgroundColour()
441 wxWindow::ChangeBackgroundColour();
443 DoChangeBackgroundColour((Widget
) m_frameWidget
, m_backgroundColour
);
445 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
448 for (i
= 0; i
< m_noItems
; i
++)
450 WXWidget radioButton
= m_radioButtons
[i
];
452 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
454 XtVaSetValues ((Widget
) radioButton
,
455 XmNselectColor
, selectPixel
,
460 void wxRadioBox::ChangeForegroundColour()
462 wxWindow::ChangeForegroundColour();
465 for (i
= 0; i
< m_noItems
; i
++)
467 WXWidget radioButton
= m_radioButtons
[i
];
469 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
473 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
474 XmToggleButtonCallbackStruct
* cbs
)
479 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
482 for (i
= 0; i
< item
->Number(); i
++)
483 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
487 if (item
->InSetValue())
490 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
491 event
.m_commandInt
= sel
;
492 event
.SetEventObject(item
);
493 item
->ProcessCommand (event
);