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
;
184 DetachWidget(m_formWidget
);
187 wxString
wxRadioBox::GetLabel(int item
) const
189 if (item
< 0 || item
>= m_noItems
)
190 return wxEmptyString
;
192 Widget widget
= (Widget
) m_radioButtons
[item
];
195 XtVaGetValues (widget
,
196 XmNlabelString
, &text
,
199 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
201 // Should we free 'text'???
210 return wxEmptyString
;
214 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
216 if (item
< 0 || item
>= m_noItems
)
219 Widget widget
= (Widget
) m_radioButtons
[item
];
222 wxString
label1(wxStripMenuCodes(label
));
223 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
224 XtVaSetValues (widget
,
225 XmNlabelString
, text
,
226 XmNlabelType
, XmSTRING
,
232 int wxRadioBox::FindString(const wxString
& s
) const
235 for (i
= 0; i
< m_noItems
; i
++)
236 if (s
== m_radioButtonLabels
[i
])
241 void wxRadioBox::SetSelection(int n
)
243 if ((n
< 0) || (n
>= m_noItems
))
246 m_selectedButton
= n
;
250 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
253 for (i
= 0; i
< m_noItems
; i
++)
255 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
257 m_inSetValue
= FALSE
;
260 // Get single selection, for single choice list items
261 int wxRadioBox::GetSelection() const
263 return m_selectedButton
;
266 // Find string for position
267 wxString
wxRadioBox::GetString(int n
) const
269 if ((n
< 0) || (n
>= m_noItems
))
270 return wxEmptyString
;
271 return m_radioButtonLabels
[n
];
274 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
276 bool managed
= XtIsManaged((Widget
) m_formWidget
);
279 XtUnmanageChild ((Widget
) m_formWidget
);
281 int xx
= x
; int yy
= y
;
282 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
284 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
285 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
287 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
288 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
291 // Must set the actual RadioBox to be desired size MINUS label size
292 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
295 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
298 actualHeight
= height
- labelHeight
;
302 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
306 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
309 XtManageChild ((Widget
) m_formWidget
);
312 // Enable a specific button
313 void wxRadioBox::Enable(int n
, bool enable
)
315 if ((n
< 0) || (n
>= m_noItems
))
318 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
321 // Enable all controls
322 void wxRadioBox::Enable(bool enable
)
324 wxControl::Enable(enable
);
327 for (i
= 0; i
< m_noItems
; i
++)
328 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
331 // Show a specific button
332 void wxRadioBox::Show(int n
, bool show
)
334 // This method isn't complete, and we try do do our best...
335 // It's main purpose isn't for allowing Show/Unshow dynamically,
336 // but rather to provide a way to design wxRadioBox such:
338 // o Val1 o Val2 o Val3
340 // o Val7 o Val8 o Val9
342 // In my case, this is a 'direction' box, and the Show(5,False) is
343 // coupled with an Enable(5,False)
345 if ((n
< 0) || (n
>= m_noItems
))
348 XtVaSetValues ((Widget
) m_radioButtons
[n
],
349 XmNindicatorOn
, (unsigned char) show
,
352 // Please note that this is all we can do: removing the label
353 // if switching to unshow state. However, when switching
354 // to the on state, it's the prog. resp. to call SetLabel(item,...)
357 wxRadioBox::SetLabel (n
, " ");
360 // For single selection items only
361 wxString
wxRadioBox::GetStringSelection () const
363 int sel
= GetSelection ();
365 return this->GetString (sel
);
370 bool wxRadioBox::SetStringSelection (const wxString
& s
)
372 int sel
= FindString (s
);
382 void wxRadioBox::Command (wxCommandEvent
& event
)
384 SetSelection (event
.m_commandInt
);
385 ProcessCommand (event
);
388 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
390 wxWindow::ChangeFont(keepOriginalSize
);
392 XmFontList fontList
= (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
395 for (i
= 0; i
< m_noItems
; i
++)
397 WXWidget radioButton
= m_radioButtons
[i
];
399 XtVaSetValues ((Widget
) radioButton
,
400 XmNfontList
, fontList
,
401 XmNtopAttachment
, XmATTACH_FORM
,
406 void wxRadioBox::ChangeBackgroundColour()
408 wxWindow::ChangeBackgroundColour();
411 for (i
= 0; i
< m_noItems
; i
++)
413 WXWidget radioButton
= m_radioButtons
[i
];
415 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
419 void wxRadioBox::ChangeForegroundColour()
421 wxWindow::ChangeForegroundColour();
424 for (i
= 0; i
< m_noItems
; i
++)
426 WXWidget radioButton
= m_radioButtons
[i
];
428 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
432 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
433 XmToggleButtonCallbackStruct
* cbs
)
438 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
441 for (i
= 0; i
< item
->Number(); i
++)
442 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
446 if (item
->InSetValue())
449 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
450 event
.m_commandInt
= sel
;
451 event
.SetEventObject(item
);
452 item
->ProcessCommand (event
);