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
;
64 parent
->AddChild(this);
66 m_windowStyle
= (long&)style
;
69 m_windowId
= NewControlId();
73 m_noRowsOrCols
= majorDim
;
78 m_majorDim
= majorDim
;
80 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
82 wxString
label1(wxStripMenuCodes(title
));
84 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
86 Widget formWidget
= XtVaCreateManagedWidget ((char*) (const char*) name
,
87 xmFormWidgetClass
, parentWidget
,
92 m_formWidget
= (WXWidget
) formWidget
;
96 text
= XmStringCreateSimple ((char*) (const char*) label1
);
97 Widget labelWidget
= XtVaCreateManagedWidget ((char*) (const char*) label1
,
100 xmLabelWidgetClass
: xmLabelGadgetClass
,
103 xmLabelWidgetClass
, formWidget
,
105 XmNlabelString
, text
,
108 /* TODO: change label font
110 XtVaSetValues (labelWidget,
111 XmNfontList, labelFont->GetInternalFont (XtDisplay(formWidget)),
120 majorDim
= (n
+ majorDim
- 1) / majorDim
;
122 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
123 XmHORIZONTAL
: XmVERTICAL
));
124 XtSetArg (args
[1], XmNnumColumns
, majorDim
);
126 Widget radioBoxWidget
= XmCreateRadioBox (formWidget
, "radioBoxWidget", args
, 2);
127 m_mainWidget
= (WXWidget
) radioBoxWidget
;
131 XtVaSetValues ((Widget
) m_labelWidget
,
132 XmNtopAttachment
, XmATTACH_FORM
,
133 XmNleftAttachment
, XmATTACH_FORM
,
134 XmNalignment
, XmALIGNMENT_BEGINNING
,
137 XtVaSetValues (radioBoxWidget
,
138 XmNtopAttachment
, m_labelWidget
? XmATTACH_WIDGET
: XmATTACH_FORM
,
139 XmNtopWidget
, m_labelWidget
? (Widget
) m_labelWidget
: formWidget
,
140 XmNbottomAttachment
, XmATTACH_FORM
,
141 XmNleftAttachment
, XmATTACH_FORM
,
144 // if (style & wxFLAT)
145 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
147 m_radioButtons
= new WXWidget
[n
];
148 m_radioButtonLabels
= new wxString
[n
];
150 for (i
= 0; i
< n
; i
++)
152 wxString
str(wxStripMenuCodes(choices
[i
]));
153 m_radioButtonLabels
[i
] = str
;
154 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
156 xmToggleButtonGadgetClass
, radioBoxWidget
,
158 xmToggleButtonWidgetClass
, radioBoxWidget
,
161 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
162 (XtCallbackProc
) this);
166 XtVaSetValues ((Widget) m_radioButtons[i],
167 XmNfontList, buttonFont->GetInternalFont (XtDisplay(formWidget)),
173 XtManageChild (radioBoxWidget
);
175 SetCanAddEventHandler(TRUE
);
176 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
178 SetFont(* parent
->GetFont());
179 ChangeColour(m_mainWidget
);
185 wxRadioBox::~wxRadioBox()
187 delete[] m_radioButtonLabels
;
188 delete[] m_radioButtons
;
191 wxString
wxRadioBox::GetLabel(int item
) const
193 if (item
< 0 || item
>= m_noItems
)
194 return wxEmptyString
;
196 Widget widget
= (Widget
) m_radioButtons
[item
];
199 XtVaGetValues (widget
,
200 XmNlabelString
, &text
,
203 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
205 // Should we free 'text'???
214 return wxEmptyString
;
218 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
220 if (item
< 0 || item
>= m_noItems
)
223 Widget widget
= (Widget
) m_radioButtons
[item
];
226 wxString
label1(wxStripMenuCodes(label
));
227 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
228 XtVaSetValues (widget
,
229 XmNlabelString
, text
,
230 XmNlabelType
, XmSTRING
,
236 int wxRadioBox::FindString(const wxString
& s
) const
239 for (i
= 0; i
< m_noItems
; i
++)
240 if (s
== m_radioButtonLabels
[i
])
245 void wxRadioBox::SetSelection(int n
)
247 if ((n
< 0) || (n
>= m_noItems
))
250 m_selectedButton
= n
;
254 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
257 for (i
= 0; i
< m_noItems
; i
++)
259 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
261 m_inSetValue
= FALSE
;
264 // Get single selection, for single choice list items
265 int wxRadioBox::GetSelection() const
267 return m_selectedButton
;
270 // Find string for position
271 wxString
wxRadioBox::GetString(int n
) const
273 if ((n
< 0) || (n
>= m_noItems
))
274 return wxEmptyString
;
275 return m_radioButtonLabels
[n
];
278 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
280 bool managed
= XtIsManaged((Widget
) m_formWidget
);
283 XtUnmanageChild ((Widget
) m_formWidget
);
285 int xx
= x
; int yy
= y
;
286 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
288 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
289 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
291 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
292 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
295 // Must set the actual RadioBox to be desired size MINUS label size
296 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
299 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
302 actualHeight
= height
- labelHeight
;
306 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
310 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
313 XtManageChild ((Widget
) m_formWidget
);
316 // Enable a specific button
317 void wxRadioBox::Enable(int n
, bool enable
)
319 if ((n
< 0) || (n
>= m_noItems
))
322 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
325 // Enable all controls
326 void wxRadioBox::Enable(bool enable
)
328 wxControl::Enable(enable
);
331 for (i
= 0; i
< m_noItems
; i
++)
332 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
335 // Show a specific button
336 void wxRadioBox::Show(int n
, bool show
)
338 // This method isn't complete, and we try do do our best...
339 // It's main purpose isn't for allowing Show/Unshow dynamically,
340 // but rather to provide a way to design wxRadioBox such:
342 // o Val1 o Val2 o Val3
344 // o Val7 o Val8 o Val9
346 // In my case, this is a 'direction' box, and the Show(5,False) is
347 // coupled with an Enable(5,False)
349 if ((n
< 0) || (n
>= m_noItems
))
352 XtVaSetValues ((Widget
) m_radioButtons
[n
],
353 XmNindicatorOn
, (unsigned char) show
,
356 // Please note that this is all we can do: removing the label
357 // if switching to unshow state. However, when switching
358 // to the on state, it's the prog. resp. to call SetLabel(item,...)
361 wxRadioBox::SetLabel (n
, " ");
364 // For single selection items only
365 wxString
wxRadioBox::GetStringSelection () const
367 int sel
= GetSelection ();
369 return this->GetString (sel
);
374 bool wxRadioBox::SetStringSelection (const wxString
& s
)
376 int sel
= FindString (s
);
386 void wxRadioBox::Command (wxCommandEvent
& event
)
388 SetSelection (event
.m_commandInt
);
389 ProcessCommand (event
);
392 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
393 XmToggleButtonCallbackStruct
* cbs
)
398 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
401 for (i
= 0; i
< item
->Number(); i
++)
402 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
406 if (item
->InSetValue())
409 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
410 event
.m_commandInt
= sel
;
411 event
.SetEventObject(item
);
412 item
->ProcessCommand (event
);