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_windowFont
= 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 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
93 Widget formWidget
= XtVaCreateManagedWidget ((char*) (const char*) name
,
94 xmFormWidgetClass
, parentWidget
,
99 m_formWidget
= (WXWidget
) formWidget
;
101 XmFontList fontList
= (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay(parentWidget
));
104 text
= XmStringCreateSimple ((char*) (const char*) label1
);
105 Widget labelWidget
= XtVaCreateManagedWidget ((char*) (const char*) label1
,
108 xmLabelWidgetClass
: xmLabelGadgetClass
,
111 xmLabelWidgetClass
, formWidget
,
113 XmNfontList
, fontList
,
114 XmNlabelString
, text
,
120 Widget frameWidget
= XtVaCreateManagedWidget ("frame",
121 xmFrameWidgetClass
, formWidget
,
122 XmNshadowType
, XmSHADOW_IN
,
123 // XmNmarginHeight, 0,
124 // XmNmarginWidth, 0,
127 m_frameWidget
= (WXWidget
) frameWidget
;
131 majorDim
= (n
+ majorDim
- 1) / majorDim
;
133 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
134 XmHORIZONTAL
: XmVERTICAL
));
135 XtSetArg (args
[1], XmNnumColumns
, majorDim
);
137 Widget radioBoxWidget
= XmCreateRadioBox (frameWidget
, "radioBoxWidget", args
, 2);
138 m_mainWidget
= (WXWidget
) radioBoxWidget
;
142 XtVaSetValues ((Widget
) m_labelWidget
,
143 XmNtopAttachment
, XmATTACH_FORM
,
144 XmNleftAttachment
, XmATTACH_FORM
,
145 XmNalignment
, XmALIGNMENT_BEGINNING
,
148 XtVaSetValues (radioBoxWidget
,
149 XmNtopAttachment
, m_labelWidget
? XmATTACH_WIDGET
: XmATTACH_FORM
,
150 XmNtopWidget
, m_labelWidget
? (Widget
) m_labelWidget
: formWidget
,
151 XmNbottomAttachment
, XmATTACH_FORM
,
152 XmNleftAttachment
, XmATTACH_FORM
,
153 XmNrightAttachment
, XmATTACH_FORM
,
156 // if (style & wxFLAT)
157 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
159 m_radioButtons
= new WXWidget
[n
];
160 m_radioButtonLabels
= new wxString
[n
];
162 for (i
= 0; i
< n
; i
++)
164 wxString
str(wxStripMenuCodes(choices
[i
]));
165 m_radioButtonLabels
[i
] = str
;
166 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
168 xmToggleButtonGadgetClass
, radioBoxWidget
,
170 xmToggleButtonWidgetClass
, radioBoxWidget
,
172 XmNfontList
, fontList
,
174 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
175 (XtCallbackProc
) this);
180 m_windowFont
= parent
->GetFont();
183 // XtManageChild((Widget) m_formWidget);
184 XtManageChild (radioBoxWidget
);
186 SetCanAddEventHandler(TRUE
);
187 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
189 ChangeBackgroundColour();
195 wxRadioBox::~wxRadioBox()
197 delete[] m_radioButtonLabels
;
198 delete[] m_radioButtons
;
200 DetachWidget(m_formWidget
);
201 DetachWidget(m_mainWidget
);
204 XtDestroyWidget((Widget
) m_labelWidget
);
205 XtDestroyWidget((Widget
) m_mainWidget
);
206 XtDestroyWidget((Widget
) m_formWidget
);
208 m_mainWidget
= (WXWidget
) 0;
209 m_formWidget
= (WXWidget
) 0;
210 m_labelWidget
= (WXWidget
) 0;
213 wxString
wxRadioBox::GetLabel(int item
) const
215 if (item
< 0 || item
>= m_noItems
)
216 return wxEmptyString
;
218 Widget widget
= (Widget
) m_radioButtons
[item
];
221 XtVaGetValues (widget
,
222 XmNlabelString
, &text
,
225 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
227 // Should we free 'text'???
236 return wxEmptyString
;
240 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
242 if (item
< 0 || item
>= m_noItems
)
245 Widget widget
= (Widget
) m_radioButtons
[item
];
248 wxString
label1(wxStripMenuCodes(label
));
249 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
250 XtVaSetValues (widget
,
251 XmNlabelString
, text
,
252 XmNlabelType
, XmSTRING
,
258 int wxRadioBox::FindString(const wxString
& s
) const
261 for (i
= 0; i
< m_noItems
; i
++)
262 if (s
== m_radioButtonLabels
[i
])
267 void wxRadioBox::SetSelection(int n
)
269 if ((n
< 0) || (n
>= m_noItems
))
272 m_selectedButton
= n
;
276 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
279 for (i
= 0; i
< m_noItems
; i
++)
281 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
283 m_inSetValue
= FALSE
;
286 // Get single selection, for single choice list items
287 int wxRadioBox::GetSelection() const
289 return m_selectedButton
;
292 // Find string for position
293 wxString
wxRadioBox::GetString(int n
) const
295 if ((n
< 0) || (n
>= m_noItems
))
296 return wxEmptyString
;
297 return m_radioButtonLabels
[n
];
300 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
302 bool managed
= XtIsManaged((Widget
) m_formWidget
);
305 XtUnmanageChild ((Widget
) m_formWidget
);
307 int xx
= x
; int yy
= y
;
308 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
310 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
311 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
313 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
314 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
317 // Must set the actual RadioBox to be desired size MINUS label size
318 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
321 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
324 actualHeight
= height
- labelHeight
;
328 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
332 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
335 XtManageChild ((Widget
) m_formWidget
);
338 // Enable a specific button
339 void wxRadioBox::Enable(int n
, bool enable
)
341 if ((n
< 0) || (n
>= m_noItems
))
344 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
347 // Enable all controls
348 bool wxRadioBox::Enable(bool enable
)
350 if ( !wxControl::Enable(enable
) )
354 for (i
= 0; i
< m_noItems
; i
++)
355 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
360 bool wxRadioBox::Show(bool show
)
362 // TODO: show/hide all children
363 return wxControl::Show(show
);
366 // Show a specific button
367 void wxRadioBox::Show(int n
, bool show
)
369 // This method isn't complete, and we try do do our best...
370 // It's main purpose isn't for allowing Show/Unshow dynamically,
371 // but rather to provide a way to design wxRadioBox such:
373 // o Val1 o Val2 o Val3
375 // o Val7 o Val8 o Val9
377 // In my case, this is a 'direction' box, and the Show(5,False) is
378 // coupled with an Enable(5,False)
380 if ((n
< 0) || (n
>= m_noItems
))
383 XtVaSetValues ((Widget
) m_radioButtons
[n
],
384 XmNindicatorOn
, (unsigned char) show
,
387 // Please note that this is all we can do: removing the label
388 // if switching to unshow state. However, when switching
389 // to the on state, it's the prog. resp. to call SetLabel(item,...)
392 wxRadioBox::SetLabel (n
, " ");
395 // For single selection items only
396 wxString
wxRadioBox::GetStringSelection () const
398 int sel
= GetSelection ();
400 return this->GetString (sel
);
405 bool wxRadioBox::SetStringSelection (const wxString
& s
)
407 int sel
= FindString (s
);
417 void wxRadioBox::Command (wxCommandEvent
& event
)
419 SetSelection (event
.m_commandInt
);
420 ProcessCommand (event
);
423 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
425 wxWindow::ChangeFont(keepOriginalSize
);
427 XmFontList fontList
= (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
430 for (i
= 0; i
< m_noItems
; i
++)
432 WXWidget radioButton
= m_radioButtons
[i
];
434 XtVaSetValues ((Widget
) radioButton
,
435 XmNfontList
, fontList
,
436 XmNtopAttachment
, XmATTACH_FORM
,
441 void wxRadioBox::ChangeBackgroundColour()
443 wxWindow::ChangeBackgroundColour();
445 DoChangeBackgroundColour((Widget
) m_frameWidget
, m_backgroundColour
);
447 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
450 for (i
= 0; i
< m_noItems
; i
++)
452 WXWidget radioButton
= m_radioButtons
[i
];
454 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
456 XtVaSetValues ((Widget
) radioButton
,
457 XmNselectColor
, selectPixel
,
462 void wxRadioBox::ChangeForegroundColour()
464 wxWindow::ChangeForegroundColour();
467 for (i
= 0; i
< m_noItems
; i
++)
469 WXWidget radioButton
= m_radioButtons
[i
];
471 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
475 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
476 XmToggleButtonCallbackStruct
* cbs
)
481 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
484 for (i
= 0; i
< item
->Number(); i
++)
485 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
489 if (item
->InSetValue())
492 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
493 event
.m_commandInt
= sel
;
494 event
.SetEventObject(item
);
495 item
->ProcessCommand (event
);