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::SetSize(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 void wxRadioBox::Enable(bool enable
)
350 wxControl::Enable(enable
);
353 for (i
= 0; i
< m_noItems
; i
++)
354 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
357 bool wxRadioBox::Show(bool show
)
359 // TODO: show/hide all children
360 return wxControl::Show(show
);
363 // Show a specific button
364 void wxRadioBox::Show(int n
, bool show
)
366 // This method isn't complete, and we try do do our best...
367 // It's main purpose isn't for allowing Show/Unshow dynamically,
368 // but rather to provide a way to design wxRadioBox such:
370 // o Val1 o Val2 o Val3
372 // o Val7 o Val8 o Val9
374 // In my case, this is a 'direction' box, and the Show(5,False) is
375 // coupled with an Enable(5,False)
377 if ((n
< 0) || (n
>= m_noItems
))
380 XtVaSetValues ((Widget
) m_radioButtons
[n
],
381 XmNindicatorOn
, (unsigned char) show
,
384 // Please note that this is all we can do: removing the label
385 // if switching to unshow state. However, when switching
386 // to the on state, it's the prog. resp. to call SetLabel(item,...)
389 wxRadioBox::SetLabel (n
, " ");
392 // For single selection items only
393 wxString
wxRadioBox::GetStringSelection () const
395 int sel
= GetSelection ();
397 return this->GetString (sel
);
402 bool wxRadioBox::SetStringSelection (const wxString
& s
)
404 int sel
= FindString (s
);
414 void wxRadioBox::Command (wxCommandEvent
& event
)
416 SetSelection (event
.m_commandInt
);
417 ProcessCommand (event
);
420 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
422 wxWindow::ChangeFont(keepOriginalSize
);
424 XmFontList fontList
= (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
427 for (i
= 0; i
< m_noItems
; i
++)
429 WXWidget radioButton
= m_radioButtons
[i
];
431 XtVaSetValues ((Widget
) radioButton
,
432 XmNfontList
, fontList
,
433 XmNtopAttachment
, XmATTACH_FORM
,
438 void wxRadioBox::ChangeBackgroundColour()
440 wxWindow::ChangeBackgroundColour();
442 DoChangeBackgroundColour((Widget
) m_frameWidget
, m_backgroundColour
);
444 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
447 for (i
= 0; i
< m_noItems
; i
++)
449 WXWidget radioButton
= m_radioButtons
[i
];
451 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
453 XtVaSetValues ((Widget
) radioButton
,
454 XmNselectColor
, selectPixel
,
459 void wxRadioBox::ChangeForegroundColour()
461 wxWindow::ChangeForegroundColour();
464 for (i
= 0; i
< m_noItems
; i
++)
466 WXWidget radioButton
= m_radioButtons
[i
];
468 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
472 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
473 XmToggleButtonCallbackStruct
* cbs
)
478 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
481 for (i
= 0; i
< item
->Number(); i
++)
482 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
486 if (item
->InSetValue())
489 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
490 event
.m_commandInt
= sel
;
491 event
.SetEventObject(item
);
492 item
->ProcessCommand (event
);