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 m_windowFont
= parent
->GetFont();
178 XtManageChild (radioBoxWidget
);
180 SetCanAddEventHandler(TRUE
);
181 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
183 ChangeBackgroundColour();
189 wxRadioBox::~wxRadioBox()
191 delete[] m_radioButtonLabels
;
192 delete[] m_radioButtons
;
195 wxString
wxRadioBox::GetLabel(int item
) const
197 if (item
< 0 || item
>= m_noItems
)
198 return wxEmptyString
;
200 Widget widget
= (Widget
) m_radioButtons
[item
];
203 XtVaGetValues (widget
,
204 XmNlabelString
, &text
,
207 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
209 // Should we free 'text'???
218 return wxEmptyString
;
222 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
224 if (item
< 0 || item
>= m_noItems
)
227 Widget widget
= (Widget
) m_radioButtons
[item
];
230 wxString
label1(wxStripMenuCodes(label
));
231 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
232 XtVaSetValues (widget
,
233 XmNlabelString
, text
,
234 XmNlabelType
, XmSTRING
,
240 int wxRadioBox::FindString(const wxString
& s
) const
243 for (i
= 0; i
< m_noItems
; i
++)
244 if (s
== m_radioButtonLabels
[i
])
249 void wxRadioBox::SetSelection(int n
)
251 if ((n
< 0) || (n
>= m_noItems
))
254 m_selectedButton
= n
;
258 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
261 for (i
= 0; i
< m_noItems
; i
++)
263 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
265 m_inSetValue
= FALSE
;
268 // Get single selection, for single choice list items
269 int wxRadioBox::GetSelection() const
271 return m_selectedButton
;
274 // Find string for position
275 wxString
wxRadioBox::GetString(int n
) const
277 if ((n
< 0) || (n
>= m_noItems
))
278 return wxEmptyString
;
279 return m_radioButtonLabels
[n
];
282 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
284 bool managed
= XtIsManaged((Widget
) m_formWidget
);
287 XtUnmanageChild ((Widget
) m_formWidget
);
289 int xx
= x
; int yy
= y
;
290 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
292 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
293 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
295 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
296 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
299 // Must set the actual RadioBox to be desired size MINUS label size
300 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
303 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
306 actualHeight
= height
- labelHeight
;
310 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
314 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
317 XtManageChild ((Widget
) m_formWidget
);
320 // Enable a specific button
321 void wxRadioBox::Enable(int n
, bool enable
)
323 if ((n
< 0) || (n
>= m_noItems
))
326 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
329 // Enable all controls
330 void wxRadioBox::Enable(bool enable
)
332 wxControl::Enable(enable
);
335 for (i
= 0; i
< m_noItems
; i
++)
336 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
339 // Show a specific button
340 void wxRadioBox::Show(int n
, bool show
)
342 // This method isn't complete, and we try do do our best...
343 // It's main purpose isn't for allowing Show/Unshow dynamically,
344 // but rather to provide a way to design wxRadioBox such:
346 // o Val1 o Val2 o Val3
348 // o Val7 o Val8 o Val9
350 // In my case, this is a 'direction' box, and the Show(5,False) is
351 // coupled with an Enable(5,False)
353 if ((n
< 0) || (n
>= m_noItems
))
356 XtVaSetValues ((Widget
) m_radioButtons
[n
],
357 XmNindicatorOn
, (unsigned char) show
,
360 // Please note that this is all we can do: removing the label
361 // if switching to unshow state. However, when switching
362 // to the on state, it's the prog. resp. to call SetLabel(item,...)
365 wxRadioBox::SetLabel (n
, " ");
368 // For single selection items only
369 wxString
wxRadioBox::GetStringSelection () const
371 int sel
= GetSelection ();
373 return this->GetString (sel
);
378 bool wxRadioBox::SetStringSelection (const wxString
& s
)
380 int sel
= FindString (s
);
390 void wxRadioBox::Command (wxCommandEvent
& event
)
392 SetSelection (event
.m_commandInt
);
393 ProcessCommand (event
);
396 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
401 void wxRadioBox::ChangeBackgroundColour()
406 void wxRadioBox::ChangeForegroundColour()
411 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
412 XmToggleButtonCallbackStruct
* cbs
)
417 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
420 for (i
= 0; i
< item
->Number(); i
++)
421 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
425 if (item
->InSetValue())
428 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
429 event
.m_commandInt
= sel
;
430 event
.SetEventObject(item
);
431 item
->ProcessCommand (event
);