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 #pragma message disable nosimpint
23 #include <Xm/LabelG.h>
24 #include <Xm/ToggleB.h>
25 #include <Xm/ToggleBG.h>
26 #include <Xm/RowColumn.h>
30 #pragma message enable nosimpint
33 #include "wx/motif/private.h"
35 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
36 XmToggleButtonCallbackStruct
* cbs
);
38 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
41 wxRadioBox::wxRadioBox()
43 m_selectedButton
= -1;
48 m_formWidget
= (WXWidget
) 0;
49 m_frameWidget
= (WXWidget
) 0;
50 m_labelWidget
= (WXWidget
) 0;
51 m_radioButtons
= (WXWidget
*) NULL
;
52 m_radioButtonLabels
= (wxString
*) NULL
;
55 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
56 const wxPoint
& pos
, const wxSize
& size
,
57 int n
, const wxString choices
[],
58 int majorDim
, long style
,
59 const wxValidator
& val
, const wxString
& name
)
61 m_selectedButton
= -1;
63 m_formWidget
= (WXWidget
) 0;
64 m_frameWidget
= (WXWidget
) 0;
65 m_labelWidget
= (WXWidget
) 0;
66 m_radioButtons
= (WXWidget
*) NULL
;
67 m_radioButtonLabels
= (wxString
*) NULL
;
68 m_backgroundColour
= parent
->GetBackgroundColour();
69 m_foregroundColour
= parent
->GetForegroundColour();
70 m_font
= parent
->GetFont();
75 parent
->AddChild(this);
77 m_windowStyle
= (long&)style
;
80 m_windowId
= NewControlId();
84 m_noRowsOrCols
= majorDim
;
89 m_majorDim
= majorDim
;
91 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
93 wxString
label1(wxStripMenuCodes(title
));
95 Widget formWidget
= XtVaCreateManagedWidget (name
.c_str(),
96 xmFormWidgetClass
, parentWidget
,
101 m_formWidget
= (WXWidget
) formWidget
;
103 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
106 wxXmString
text(label1
);
107 (void)XtVaCreateManagedWidget(label1
.c_str(),
109 style
& wxCOLOURED
? xmLabelWidgetClass
110 : xmLabelGadgetClass
,
113 xmLabelWidgetClass
, formWidget
,
115 XmNfontList
, fontList
,
116 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 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
133 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
134 XmHORIZONTAL
: XmVERTICAL
));
135 XtSetArg (args
[1], XmNnumColumns
, m_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
,
175 #pragma message disable voiincconext
176 // VMS gives here the compiler warning:
177 // conversion from pointer to function to void* permitted
180 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
181 (XtCallbackProc
) this);
183 #pragma message enable voiincconext
189 m_font
= parent
->GetFont();
192 // XtManageChild((Widget) m_formWidget);
193 XtManageChild (radioBoxWidget
);
195 SetCanAddEventHandler(TRUE
);
196 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
198 ChangeBackgroundColour();
204 wxRadioBox::~wxRadioBox()
206 delete[] m_radioButtonLabels
;
207 delete[] m_radioButtons
;
209 DetachWidget(m_formWidget
);
210 DetachWidget(m_mainWidget
);
213 XtDestroyWidget((Widget
) m_labelWidget
);
214 XtDestroyWidget((Widget
) m_mainWidget
);
215 XtDestroyWidget((Widget
) m_formWidget
);
217 m_mainWidget
= (WXWidget
) 0;
218 m_formWidget
= (WXWidget
) 0;
219 m_labelWidget
= (WXWidget
) 0;
222 wxString
wxRadioBox::GetLabel(int item
) const
224 if (item
< 0 || item
>= m_noItems
)
225 return wxEmptyString
;
227 Widget widget
= (Widget
) m_radioButtons
[item
];
230 XtVaGetValues (widget
,
231 XmNlabelString
, &text
,
234 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
236 // Should we free 'text'???
245 return wxEmptyString
;
249 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
251 if (item
< 0 || item
>= m_noItems
)
254 Widget widget
= (Widget
) m_radioButtons
[item
];
257 wxString
label1(wxStripMenuCodes(label
));
258 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
259 XtVaSetValues (widget
,
260 XmNlabelString
, text
,
261 XmNlabelType
, XmSTRING
,
267 int wxRadioBox::FindString(const wxString
& s
) const
270 for (i
= 0; i
< m_noItems
; i
++)
271 if (s
== m_radioButtonLabels
[i
])
276 void wxRadioBox::SetSelection(int n
)
278 if ((n
< 0) || (n
>= m_noItems
))
281 m_selectedButton
= n
;
285 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
288 for (i
= 0; i
< m_noItems
; i
++)
290 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
292 m_inSetValue
= FALSE
;
295 // Get single selection, for single choice list items
296 int wxRadioBox::GetSelection() const
298 return m_selectedButton
;
301 // Find string for position
302 wxString
wxRadioBox::GetString(int n
) const
304 if ((n
< 0) || (n
>= m_noItems
))
305 return wxEmptyString
;
306 return m_radioButtonLabels
[n
];
309 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
311 bool managed
= XtIsManaged((Widget
) m_formWidget
);
314 XtUnmanageChild ((Widget
) m_formWidget
);
316 int xx
= x
; int yy
= y
;
317 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
319 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
320 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
322 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
323 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
326 // Must set the actual RadioBox to be desired size MINUS label size
327 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
330 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
333 actualHeight
= height
- labelHeight
;
337 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
341 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
344 XtManageChild ((Widget
) m_formWidget
);
347 // Enable a specific button
348 void wxRadioBox::Enable(int n
, bool enable
)
350 if ((n
< 0) || (n
>= m_noItems
))
353 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
356 // Enable all controls
357 bool wxRadioBox::Enable(bool enable
)
359 if ( !wxControl::Enable(enable
) )
363 for (i
= 0; i
< m_noItems
; i
++)
364 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
369 bool wxRadioBox::Show(bool show
)
371 // TODO: show/hide all children
372 return wxControl::Show(show
);
375 // Show a specific button
376 void wxRadioBox::Show(int n
, bool show
)
378 // This method isn't complete, and we try do do our best...
379 // It's main purpose isn't for allowing Show/Unshow dynamically,
380 // but rather to provide a way to design wxRadioBox such:
382 // o Val1 o Val2 o Val3
384 // o Val7 o Val8 o Val9
386 // In my case, this is a 'direction' box, and the Show(5,False) is
387 // coupled with an Enable(5,False)
389 if ((n
< 0) || (n
>= m_noItems
))
392 XtVaSetValues ((Widget
) m_radioButtons
[n
],
393 XmNindicatorOn
, (unsigned char) show
,
396 // Please note that this is all we can do: removing the label
397 // if switching to unshow state. However, when switching
398 // to the on state, it's the prog. resp. to call SetLabel(item,...)
401 wxRadioBox::SetLabel (n
, " ");
404 // For single selection items only
405 wxString
wxRadioBox::GetStringSelection () const
407 int sel
= GetSelection ();
409 return this->GetString (sel
);
414 bool wxRadioBox::SetStringSelection (const wxString
& s
)
416 int sel
= FindString (s
);
426 void wxRadioBox::Command (wxCommandEvent
& event
)
428 SetSelection (event
.m_commandInt
);
429 ProcessCommand (event
);
432 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
434 wxWindow::ChangeFont(keepOriginalSize
);
436 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
439 for (i
= 0; i
< m_noItems
; i
++)
441 WXWidget radioButton
= m_radioButtons
[i
];
443 XtVaSetValues ((Widget
) radioButton
,
444 XmNfontList
, fontList
,
445 XmNtopAttachment
, XmATTACH_FORM
,
450 void wxRadioBox::ChangeBackgroundColour()
452 wxWindow::ChangeBackgroundColour();
454 DoChangeBackgroundColour((Widget
) m_frameWidget
, m_backgroundColour
);
456 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
459 for (i
= 0; i
< m_noItems
; i
++)
461 WXWidget radioButton
= m_radioButtons
[i
];
463 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
465 XtVaSetValues ((Widget
) radioButton
,
466 XmNselectColor
, selectPixel
,
471 void wxRadioBox::ChangeForegroundColour()
473 wxWindow::ChangeForegroundColour();
476 for (i
= 0; i
< m_noItems
; i
++)
478 WXWidget radioButton
= m_radioButtons
[i
];
480 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
484 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
485 XmToggleButtonCallbackStruct
* cbs
)
490 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
493 for (i
= 0; i
< item
->Number(); i
++)
494 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
498 if (item
->InSetValue())
501 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
502 event
.m_commandInt
= sel
;
503 event
.SetEventObject(item
);
504 item
->ProcessCommand (event
);