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>
26 #include <wx/motif/private.h>
28 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
29 XmToggleButtonCallbackStruct
* cbs
);
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
36 wxRadioBox::wxRadioBox()
38 m_selectedButton
= -1;
43 m_formWidget
= (WXWidget
) 0;
44 m_labelWidget
= (WXWidget
) 0;
45 m_radioButtons
= (WXWidget
*) NULL
;
46 m_radioButtonLabels
= (wxString
*) NULL
;
49 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
50 const wxPoint
& pos
, const wxSize
& size
,
51 int n
, const wxString choices
[],
52 int majorDim
, long style
,
53 const wxValidator
& val
, const wxString
& name
)
55 m_selectedButton
= -1;
57 m_labelWidget
= (WXWidget
) 0;
58 m_radioButtons
= (WXWidget
*) NULL
;
59 m_radioButtonLabels
= (wxString
*) NULL
;
60 m_backgroundColour
= parent
->GetBackgroundColour();
61 m_foregroundColour
= parent
->GetForegroundColour();
62 m_windowFont
= parent
->GetFont();
67 parent
->AddChild(this);
69 m_windowStyle
= (long&)style
;
72 m_windowId
= NewControlId();
76 m_noRowsOrCols
= majorDim
;
81 m_majorDim
= majorDim
;
83 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
85 wxString
label1(wxStripMenuCodes(title
));
87 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
89 Widget formWidget
= XtVaCreateManagedWidget ((char*) (const char*) name
,
90 xmFormWidgetClass
, parentWidget
,
95 m_formWidget
= (WXWidget
) formWidget
;
97 XmFontList fontList
= (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay(parentWidget
));
100 text
= XmStringCreateSimple ((char*) (const char*) label1
);
101 Widget labelWidget
= XtVaCreateManagedWidget ((char*) (const char*) label1
,
104 xmLabelWidgetClass
: xmLabelGadgetClass
,
107 xmLabelWidgetClass
, formWidget
,
109 XmNfontList
, fontList
,
110 XmNlabelString
, text
,
118 majorDim
= (n
+ majorDim
- 1) / majorDim
;
120 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
121 XmHORIZONTAL
: XmVERTICAL
));
122 XtSetArg (args
[1], XmNnumColumns
, majorDim
);
124 Widget radioBoxWidget
= XmCreateRadioBox (formWidget
, "radioBoxWidget", args
, 2);
125 m_mainWidget
= (WXWidget
) radioBoxWidget
;
129 XtVaSetValues ((Widget
) m_labelWidget
,
130 XmNtopAttachment
, XmATTACH_FORM
,
131 XmNleftAttachment
, XmATTACH_FORM
,
132 XmNalignment
, XmALIGNMENT_BEGINNING
,
135 XtVaSetValues (radioBoxWidget
,
136 XmNtopAttachment
, m_labelWidget
? XmATTACH_WIDGET
: XmATTACH_FORM
,
137 XmNtopWidget
, m_labelWidget
? (Widget
) m_labelWidget
: formWidget
,
138 XmNbottomAttachment
, XmATTACH_FORM
,
139 XmNleftAttachment
, XmATTACH_FORM
,
142 // if (style & wxFLAT)
143 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
145 m_radioButtons
= new WXWidget
[n
];
146 m_radioButtonLabels
= new wxString
[n
];
148 for (i
= 0; i
< n
; i
++)
150 wxString
str(wxStripMenuCodes(choices
[i
]));
151 m_radioButtonLabels
[i
] = str
;
152 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
154 xmToggleButtonGadgetClass
, radioBoxWidget
,
156 xmToggleButtonWidgetClass
, radioBoxWidget
,
158 XmNfontList
, fontList
,
160 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
161 (XtCallbackProc
) this);
166 m_windowFont
= parent
->GetFont();
169 XtManageChild (radioBoxWidget
);
171 SetCanAddEventHandler(TRUE
);
172 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
174 ChangeBackgroundColour();
180 wxRadioBox::~wxRadioBox()
182 delete[] m_radioButtonLabels
;
183 delete[] m_radioButtons
;
185 DetachWidget(m_formWidget
);
186 DetachWidget(m_mainWidget
);
189 XtDestroyWidget((Widget
) m_labelWidget
);
190 XtDestroyWidget((Widget
) m_mainWidget
);
191 XtDestroyWidget((Widget
) m_formWidget
);
193 m_mainWidget
= (WXWidget
) 0;
194 m_formWidget
= (WXWidget
) 0;
195 m_labelWidget
= (WXWidget
) 0;
198 wxString
wxRadioBox::GetLabel(int item
) const
200 if (item
< 0 || item
>= m_noItems
)
201 return wxEmptyString
;
203 Widget widget
= (Widget
) m_radioButtons
[item
];
206 XtVaGetValues (widget
,
207 XmNlabelString
, &text
,
210 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
212 // Should we free 'text'???
221 return wxEmptyString
;
225 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
227 if (item
< 0 || item
>= m_noItems
)
230 Widget widget
= (Widget
) m_radioButtons
[item
];
233 wxString
label1(wxStripMenuCodes(label
));
234 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
235 XtVaSetValues (widget
,
236 XmNlabelString
, text
,
237 XmNlabelType
, XmSTRING
,
243 int wxRadioBox::FindString(const wxString
& s
) const
246 for (i
= 0; i
< m_noItems
; i
++)
247 if (s
== m_radioButtonLabels
[i
])
252 void wxRadioBox::SetSelection(int n
)
254 if ((n
< 0) || (n
>= m_noItems
))
257 m_selectedButton
= n
;
261 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
264 for (i
= 0; i
< m_noItems
; i
++)
266 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
268 m_inSetValue
= FALSE
;
271 // Get single selection, for single choice list items
272 int wxRadioBox::GetSelection() const
274 return m_selectedButton
;
277 // Find string for position
278 wxString
wxRadioBox::GetString(int n
) const
280 if ((n
< 0) || (n
>= m_noItems
))
281 return wxEmptyString
;
282 return m_radioButtonLabels
[n
];
285 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
287 bool managed
= XtIsManaged((Widget
) m_formWidget
);
290 XtUnmanageChild ((Widget
) m_formWidget
);
292 int xx
= x
; int yy
= y
;
293 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
295 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
296 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
298 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
299 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
302 // Must set the actual RadioBox to be desired size MINUS label size
303 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
306 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
309 actualHeight
= height
- labelHeight
;
313 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
317 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
320 XtManageChild ((Widget
) m_formWidget
);
323 // Enable a specific button
324 void wxRadioBox::Enable(int n
, bool enable
)
326 if ((n
< 0) || (n
>= m_noItems
))
329 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
332 // Enable all controls
333 void wxRadioBox::Enable(bool enable
)
335 wxControl::Enable(enable
);
338 for (i
= 0; i
< m_noItems
; i
++)
339 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
342 // Show a specific button
343 void wxRadioBox::Show(int n
, bool show
)
345 // This method isn't complete, and we try do do our best...
346 // It's main purpose isn't for allowing Show/Unshow dynamically,
347 // but rather to provide a way to design wxRadioBox such:
349 // o Val1 o Val2 o Val3
351 // o Val7 o Val8 o Val9
353 // In my case, this is a 'direction' box, and the Show(5,False) is
354 // coupled with an Enable(5,False)
356 if ((n
< 0) || (n
>= m_noItems
))
359 XtVaSetValues ((Widget
) m_radioButtons
[n
],
360 XmNindicatorOn
, (unsigned char) show
,
363 // Please note that this is all we can do: removing the label
364 // if switching to unshow state. However, when switching
365 // to the on state, it's the prog. resp. to call SetLabel(item,...)
368 wxRadioBox::SetLabel (n
, " ");
371 // For single selection items only
372 wxString
wxRadioBox::GetStringSelection () const
374 int sel
= GetSelection ();
376 return this->GetString (sel
);
381 bool wxRadioBox::SetStringSelection (const wxString
& s
)
383 int sel
= FindString (s
);
393 void wxRadioBox::Command (wxCommandEvent
& event
)
395 SetSelection (event
.m_commandInt
);
396 ProcessCommand (event
);
399 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
401 wxWindow::ChangeFont(keepOriginalSize
);
403 XmFontList fontList
= (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
406 for (i
= 0; i
< m_noItems
; i
++)
408 WXWidget radioButton
= m_radioButtons
[i
];
410 XtVaSetValues ((Widget
) radioButton
,
411 XmNfontList
, fontList
,
412 XmNtopAttachment
, XmATTACH_FORM
,
417 void wxRadioBox::ChangeBackgroundColour()
419 wxWindow::ChangeBackgroundColour();
422 for (i
= 0; i
< m_noItems
; i
++)
424 WXWidget radioButton
= m_radioButtons
[i
];
426 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
430 void wxRadioBox::ChangeForegroundColour()
432 wxWindow::ChangeForegroundColour();
435 for (i
= 0; i
< m_noItems
; i
++)
437 WXWidget radioButton
= m_radioButtons
[i
];
439 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
443 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
444 XmToggleButtonCallbackStruct
* cbs
)
449 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
452 for (i
= 0; i
< item
->Number(); i
++)
453 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
457 if (item
->InSetValue())
460 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
461 event
.m_commandInt
= sel
;
462 event
.SetEventObject(item
);
463 item
->ProcessCommand (event
);