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>
27 #include <wx/motif/private.h>
29 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
30 XmToggleButtonCallbackStruct
* cbs
);
32 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
37 wxRadioBox::wxRadioBox()
39 m_selectedButton
= -1;
44 m_formWidget
= (WXWidget
) 0;
45 m_frameWidget
= (WXWidget
) 0;
46 m_labelWidget
= (WXWidget
) 0;
47 m_radioButtons
= (WXWidget
*) NULL
;
48 m_radioButtonLabels
= (wxString
*) NULL
;
51 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
52 const wxPoint
& pos
, const wxSize
& size
,
53 int n
, const wxString choices
[],
54 int majorDim
, long style
,
55 const wxValidator
& val
, const wxString
& name
)
57 m_selectedButton
= -1;
59 m_formWidget
= (WXWidget
) 0;
60 m_frameWidget
= (WXWidget
) 0;
61 m_labelWidget
= (WXWidget
) 0;
62 m_radioButtons
= (WXWidget
*) NULL
;
63 m_radioButtonLabels
= (wxString
*) NULL
;
64 m_backgroundColour
= parent
->GetBackgroundColour();
65 m_foregroundColour
= parent
->GetForegroundColour();
66 m_font
= parent
->GetFont();
71 parent
->AddChild(this);
73 m_windowStyle
= (long&)style
;
76 m_windowId
= NewControlId();
80 m_noRowsOrCols
= majorDim
;
85 m_majorDim
= majorDim
;
87 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
89 wxString
label1(wxStripMenuCodes(title
));
91 Widget formWidget
= XtVaCreateManagedWidget (name
.c_str(),
92 xmFormWidgetClass
, parentWidget
,
97 m_formWidget
= (WXWidget
) formWidget
;
99 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
102 wxXmString
text(label1
);
103 (void)XtVaCreateManagedWidget(label1
.c_str(),
105 style
& wxCOLOURED
? xmLabelWidgetClass
106 : xmLabelGadgetClass
,
109 xmLabelWidgetClass
, formWidget
,
111 XmNfontList
, fontList
,
112 XmNlabelString
, text(),
116 Widget frameWidget
= XtVaCreateManagedWidget ("frame",
117 xmFrameWidgetClass
, formWidget
,
118 XmNshadowType
, XmSHADOW_IN
,
119 // XmNmarginHeight, 0,
120 // XmNmarginWidth, 0,
123 m_frameWidget
= (WXWidget
) frameWidget
;
127 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
129 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
130 XmHORIZONTAL
: XmVERTICAL
));
131 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
133 Widget radioBoxWidget
= XmCreateRadioBox (frameWidget
, "radioBoxWidget", args
, 2);
134 m_mainWidget
= (WXWidget
) radioBoxWidget
;
138 XtVaSetValues ((Widget
) m_labelWidget
,
139 XmNtopAttachment
, XmATTACH_FORM
,
140 XmNleftAttachment
, XmATTACH_FORM
,
141 XmNalignment
, XmALIGNMENT_BEGINNING
,
144 XtVaSetValues (radioBoxWidget
,
145 XmNtopAttachment
, m_labelWidget
? XmATTACH_WIDGET
: XmATTACH_FORM
,
146 XmNtopWidget
, m_labelWidget
? (Widget
) m_labelWidget
: formWidget
,
147 XmNbottomAttachment
, XmATTACH_FORM
,
148 XmNleftAttachment
, XmATTACH_FORM
,
149 XmNrightAttachment
, XmATTACH_FORM
,
152 // if (style & wxFLAT)
153 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
155 m_radioButtons
= new WXWidget
[n
];
156 m_radioButtonLabels
= new wxString
[n
];
158 for (i
= 0; i
< n
; i
++)
160 wxString
str(wxStripMenuCodes(choices
[i
]));
161 m_radioButtonLabels
[i
] = str
;
162 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
164 xmToggleButtonGadgetClass
, radioBoxWidget
,
166 xmToggleButtonWidgetClass
, radioBoxWidget
,
168 XmNfontList
, fontList
,
170 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
171 (XtCallbackProc
) this);
176 m_font
= parent
->GetFont();
179 // XtManageChild((Widget) m_formWidget);
180 XtManageChild (radioBoxWidget
);
182 SetCanAddEventHandler(TRUE
);
183 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
185 ChangeBackgroundColour();
191 wxRadioBox::~wxRadioBox()
193 delete[] m_radioButtonLabels
;
194 delete[] m_radioButtons
;
196 DetachWidget(m_formWidget
);
197 DetachWidget(m_mainWidget
);
200 XtDestroyWidget((Widget
) m_labelWidget
);
201 XtDestroyWidget((Widget
) m_mainWidget
);
202 XtDestroyWidget((Widget
) m_formWidget
);
204 m_mainWidget
= (WXWidget
) 0;
205 m_formWidget
= (WXWidget
) 0;
206 m_labelWidget
= (WXWidget
) 0;
209 wxString
wxRadioBox::GetLabel(int item
) const
211 if (item
< 0 || item
>= m_noItems
)
212 return wxEmptyString
;
214 Widget widget
= (Widget
) m_radioButtons
[item
];
217 XtVaGetValues (widget
,
218 XmNlabelString
, &text
,
221 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
223 // Should we free 'text'???
232 return wxEmptyString
;
236 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
238 if (item
< 0 || item
>= m_noItems
)
241 Widget widget
= (Widget
) m_radioButtons
[item
];
244 wxString
label1(wxStripMenuCodes(label
));
245 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
246 XtVaSetValues (widget
,
247 XmNlabelString
, text
,
248 XmNlabelType
, XmSTRING
,
254 int wxRadioBox::FindString(const wxString
& s
) const
257 for (i
= 0; i
< m_noItems
; i
++)
258 if (s
== m_radioButtonLabels
[i
])
263 void wxRadioBox::SetSelection(int n
)
265 if ((n
< 0) || (n
>= m_noItems
))
268 m_selectedButton
= n
;
272 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
275 for (i
= 0; i
< m_noItems
; i
++)
277 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
279 m_inSetValue
= FALSE
;
282 // Get single selection, for single choice list items
283 int wxRadioBox::GetSelection() const
285 return m_selectedButton
;
288 // Find string for position
289 wxString
wxRadioBox::GetString(int n
) const
291 if ((n
< 0) || (n
>= m_noItems
))
292 return wxEmptyString
;
293 return m_radioButtonLabels
[n
];
296 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
298 bool managed
= XtIsManaged((Widget
) m_formWidget
);
301 XtUnmanageChild ((Widget
) m_formWidget
);
303 int xx
= x
; int yy
= y
;
304 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
306 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
307 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
309 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
310 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
313 // Must set the actual RadioBox to be desired size MINUS label size
314 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
317 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
320 actualHeight
= height
- labelHeight
;
324 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
328 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
331 XtManageChild ((Widget
) m_formWidget
);
334 // Enable a specific button
335 void wxRadioBox::Enable(int n
, bool enable
)
337 if ((n
< 0) || (n
>= m_noItems
))
340 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
343 // Enable all controls
344 bool wxRadioBox::Enable(bool enable
)
346 if ( !wxControl::Enable(enable
) )
350 for (i
= 0; i
< m_noItems
; i
++)
351 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
356 bool wxRadioBox::Show(bool show
)
358 // TODO: show/hide all children
359 return wxControl::Show(show
);
362 // Show a specific button
363 void wxRadioBox::Show(int n
, bool show
)
365 // This method isn't complete, and we try do do our best...
366 // It's main purpose isn't for allowing Show/Unshow dynamically,
367 // but rather to provide a way to design wxRadioBox such:
369 // o Val1 o Val2 o Val3
371 // o Val7 o Val8 o Val9
373 // In my case, this is a 'direction' box, and the Show(5,False) is
374 // coupled with an Enable(5,False)
376 if ((n
< 0) || (n
>= m_noItems
))
379 XtVaSetValues ((Widget
) m_radioButtons
[n
],
380 XmNindicatorOn
, (unsigned char) show
,
383 // Please note that this is all we can do: removing the label
384 // if switching to unshow state. However, when switching
385 // to the on state, it's the prog. resp. to call SetLabel(item,...)
388 wxRadioBox::SetLabel (n
, " ");
391 // For single selection items only
392 wxString
wxRadioBox::GetStringSelection () const
394 int sel
= GetSelection ();
396 return this->GetString (sel
);
401 bool wxRadioBox::SetStringSelection (const wxString
& s
)
403 int sel
= FindString (s
);
413 void wxRadioBox::Command (wxCommandEvent
& event
)
415 SetSelection (event
.m_commandInt
);
416 ProcessCommand (event
);
419 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
421 wxWindow::ChangeFont(keepOriginalSize
);
423 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
426 for (i
= 0; i
< m_noItems
; i
++)
428 WXWidget radioButton
= m_radioButtons
[i
];
430 XtVaSetValues ((Widget
) radioButton
,
431 XmNfontList
, fontList
,
432 XmNtopAttachment
, XmATTACH_FORM
,
437 void wxRadioBox::ChangeBackgroundColour()
439 wxWindow::ChangeBackgroundColour();
441 DoChangeBackgroundColour((Widget
) m_frameWidget
, m_backgroundColour
);
443 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
446 for (i
= 0; i
< m_noItems
; i
++)
448 WXWidget radioButton
= m_radioButtons
[i
];
450 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
452 XtVaSetValues ((Widget
) radioButton
,
453 XmNselectColor
, selectPixel
,
458 void wxRadioBox::ChangeForegroundColour()
460 wxWindow::ChangeForegroundColour();
463 for (i
= 0; i
< m_noItems
; i
++)
465 WXWidget radioButton
= m_radioButtons
[i
];
467 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
471 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
472 XmToggleButtonCallbackStruct
* cbs
)
477 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
480 for (i
= 0; i
< item
->Number(); i
++)
481 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
485 if (item
->InSetValue())
488 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
489 event
.m_commandInt
= sel
;
490 event
.SetEventObject(item
);
491 item
->ProcessCommand (event
);