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();
66 parent
->AddChild(this);
68 m_windowStyle
= (long&)style
;
71 m_windowId
= NewControlId();
75 m_noRowsOrCols
= majorDim
;
80 m_majorDim
= majorDim
;
82 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
84 wxString
label1(wxStripMenuCodes(title
));
86 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
88 Widget formWidget
= XtVaCreateManagedWidget ((char*) (const char*) name
,
89 xmFormWidgetClass
, parentWidget
,
94 m_formWidget
= (WXWidget
) formWidget
;
98 text
= XmStringCreateSimple ((char*) (const char*) label1
);
99 Widget labelWidget
= XtVaCreateManagedWidget ((char*) (const char*) label1
,
102 xmLabelWidgetClass
: xmLabelGadgetClass
,
105 xmLabelWidgetClass
, formWidget
,
107 XmNlabelString
, text
,
110 /* TODO: change label font
112 XtVaSetValues (labelWidget,
113 XmNfontList, labelFont->GetInternalFont (XtDisplay(formWidget)),
122 majorDim
= (n
+ majorDim
- 1) / majorDim
;
124 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
125 XmHORIZONTAL
: XmVERTICAL
));
126 XtSetArg (args
[1], XmNnumColumns
, majorDim
);
128 Widget radioBoxWidget
= XmCreateRadioBox (formWidget
, "radioBoxWidget", args
, 2);
129 m_mainWidget
= (WXWidget
) radioBoxWidget
;
133 XtVaSetValues ((Widget
) m_labelWidget
,
134 XmNtopAttachment
, XmATTACH_FORM
,
135 XmNleftAttachment
, XmATTACH_FORM
,
136 XmNalignment
, XmALIGNMENT_BEGINNING
,
139 XtVaSetValues (radioBoxWidget
,
140 XmNtopAttachment
, m_labelWidget
? XmATTACH_WIDGET
: XmATTACH_FORM
,
141 XmNtopWidget
, m_labelWidget
? (Widget
) m_labelWidget
: formWidget
,
142 XmNbottomAttachment
, XmATTACH_FORM
,
143 XmNleftAttachment
, XmATTACH_FORM
,
146 // if (style & wxFLAT)
147 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
149 m_radioButtons
= new WXWidget
[n
];
150 m_radioButtonLabels
= new wxString
[n
];
152 for (i
= 0; i
< n
; i
++)
154 wxString
str(wxStripMenuCodes(choices
[i
]));
155 m_radioButtonLabels
[i
] = str
;
156 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
158 xmToggleButtonGadgetClass
, radioBoxWidget
,
160 xmToggleButtonWidgetClass
, radioBoxWidget
,
163 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
164 (XtCallbackProc
) this);
168 XtVaSetValues ((Widget) m_radioButtons[i],
169 XmNfontList, buttonFont->GetInternalFont (XtDisplay(formWidget)),
175 XtManageChild (radioBoxWidget
);
177 SetCanAddEventHandler(TRUE
);
178 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
180 SetFont(* parent
->GetFont());
181 ChangeBackgroundColour();
187 wxRadioBox::~wxRadioBox()
189 delete[] m_radioButtonLabels
;
190 delete[] m_radioButtons
;
193 wxString
wxRadioBox::GetLabel(int item
) const
195 if (item
< 0 || item
>= m_noItems
)
196 return wxEmptyString
;
198 Widget widget
= (Widget
) m_radioButtons
[item
];
201 XtVaGetValues (widget
,
202 XmNlabelString
, &text
,
205 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
207 // Should we free 'text'???
216 return wxEmptyString
;
220 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
222 if (item
< 0 || item
>= m_noItems
)
225 Widget widget
= (Widget
) m_radioButtons
[item
];
228 wxString
label1(wxStripMenuCodes(label
));
229 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
230 XtVaSetValues (widget
,
231 XmNlabelString
, text
,
232 XmNlabelType
, XmSTRING
,
238 int wxRadioBox::FindString(const wxString
& s
) const
241 for (i
= 0; i
< m_noItems
; i
++)
242 if (s
== m_radioButtonLabels
[i
])
247 void wxRadioBox::SetSelection(int n
)
249 if ((n
< 0) || (n
>= m_noItems
))
252 m_selectedButton
= n
;
256 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
259 for (i
= 0; i
< m_noItems
; i
++)
261 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
263 m_inSetValue
= FALSE
;
266 // Get single selection, for single choice list items
267 int wxRadioBox::GetSelection() const
269 return m_selectedButton
;
272 // Find string for position
273 wxString
wxRadioBox::GetString(int n
) const
275 if ((n
< 0) || (n
>= m_noItems
))
276 return wxEmptyString
;
277 return m_radioButtonLabels
[n
];
280 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
282 bool managed
= XtIsManaged((Widget
) m_formWidget
);
285 XtUnmanageChild ((Widget
) m_formWidget
);
287 int xx
= x
; int yy
= y
;
288 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
290 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
291 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
293 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
294 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
297 // Must set the actual RadioBox to be desired size MINUS label size
298 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
301 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
304 actualHeight
= height
- labelHeight
;
308 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
312 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
315 XtManageChild ((Widget
) m_formWidget
);
318 // Enable a specific button
319 void wxRadioBox::Enable(int n
, bool enable
)
321 if ((n
< 0) || (n
>= m_noItems
))
324 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
327 // Enable all controls
328 void wxRadioBox::Enable(bool enable
)
330 wxControl::Enable(enable
);
333 for (i
= 0; i
< m_noItems
; i
++)
334 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
337 // Show a specific button
338 void wxRadioBox::Show(int n
, bool show
)
340 // This method isn't complete, and we try do do our best...
341 // It's main purpose isn't for allowing Show/Unshow dynamically,
342 // but rather to provide a way to design wxRadioBox such:
344 // o Val1 o Val2 o Val3
346 // o Val7 o Val8 o Val9
348 // In my case, this is a 'direction' box, and the Show(5,False) is
349 // coupled with an Enable(5,False)
351 if ((n
< 0) || (n
>= m_noItems
))
354 XtVaSetValues ((Widget
) m_radioButtons
[n
],
355 XmNindicatorOn
, (unsigned char) show
,
358 // Please note that this is all we can do: removing the label
359 // if switching to unshow state. However, when switching
360 // to the on state, it's the prog. resp. to call SetLabel(item,...)
363 wxRadioBox::SetLabel (n
, " ");
366 // For single selection items only
367 wxString
wxRadioBox::GetStringSelection () const
369 int sel
= GetSelection ();
371 return this->GetString (sel
);
376 bool wxRadioBox::SetStringSelection (const wxString
& s
)
378 int sel
= FindString (s
);
388 void wxRadioBox::Command (wxCommandEvent
& event
)
390 SetSelection (event
.m_commandInt
);
391 ProcessCommand (event
);
394 void wxRadioBox::ChangeFont()
399 void wxRadioBox::ChangeBackgroundColour()
404 void wxRadioBox::ChangeForegroundColour()
409 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
410 XmToggleButtonCallbackStruct
* cbs
)
415 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
418 for (i
= 0; i
< item
->Number(); i
++)
419 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
423 if (item
->InSetValue())
426 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
427 event
.m_commandInt
= sel
;
428 event
.SetEventObject(item
);
429 item
->ProcessCommand (event
);