1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "radiobox.h"
17 #define XtDisplay XTDISPLAY
22 #include "wx/radiobox.h"
26 #pragma message disable nosimpint
29 #include <Xm/LabelG.h>
30 #include <Xm/ToggleB.h>
31 #include <Xm/ToggleBG.h>
32 #include <Xm/RowColumn.h>
36 #pragma message enable nosimpint
39 #include "wx/motif/private.h"
41 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
42 XmToggleButtonCallbackStruct
* cbs
);
44 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
47 wxRadioBox::wxRadioBox()
49 m_selectedButton
= -1;
54 m_formWidget
= (WXWidget
) 0;
55 m_frameWidget
= (WXWidget
) 0;
56 m_labelWidget
= (WXWidget
) 0;
57 m_radioButtons
= (WXWidget
*) NULL
;
58 m_radioButtonLabels
= (wxString
*) NULL
;
61 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
62 const wxPoint
& pos
, const wxSize
& size
,
63 int n
, const wxString choices
[],
64 int majorDim
, long style
,
65 const wxValidator
& val
, const wxString
& name
)
67 m_selectedButton
= -1;
69 m_formWidget
= (WXWidget
) 0;
70 m_frameWidget
= (WXWidget
) 0;
71 m_labelWidget
= (WXWidget
) 0;
72 m_radioButtons
= (WXWidget
*) NULL
;
73 m_radioButtonLabels
= (wxString
*) NULL
;
74 m_backgroundColour
= parent
->GetBackgroundColour();
75 m_foregroundColour
= parent
->GetForegroundColour();
76 m_font
= parent
->GetFont();
81 parent
->AddChild(this);
83 m_windowStyle
= (long&)style
;
86 m_windowId
= NewControlId();
90 m_noRowsOrCols
= majorDim
;
95 m_majorDim
= majorDim
;
97 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
99 wxString
label1(wxStripMenuCodes(title
));
101 Widget formWidget
= XtVaCreateManagedWidget (name
.c_str(),
102 xmFormWidgetClass
, parentWidget
,
107 m_formWidget
= (WXWidget
) formWidget
;
109 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
112 wxXmString
text(label1
);
113 (void)XtVaCreateManagedWidget(label1
.c_str(),
115 style
& wxCOLOURED
? xmLabelWidgetClass
116 : xmLabelGadgetClass
,
119 xmLabelWidgetClass
, formWidget
,
121 XmNfontList
, fontList
,
122 XmNlabelString
, text(),
126 Widget frameWidget
= XtVaCreateManagedWidget ("frame",
127 xmFrameWidgetClass
, formWidget
,
128 XmNshadowType
, XmSHADOW_IN
,
129 // XmNmarginHeight, 0,
130 // XmNmarginWidth, 0,
133 m_frameWidget
= (WXWidget
) frameWidget
;
137 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
139 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
140 XmHORIZONTAL
: XmVERTICAL
));
141 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
143 Widget radioBoxWidget
= XmCreateRadioBox (frameWidget
, "radioBoxWidget", args
, 2);
144 m_mainWidget
= (WXWidget
) radioBoxWidget
;
148 XtVaSetValues ((Widget
) m_labelWidget
,
149 XmNtopAttachment
, XmATTACH_FORM
,
150 XmNleftAttachment
, XmATTACH_FORM
,
151 XmNalignment
, XmALIGNMENT_BEGINNING
,
154 XtVaSetValues (radioBoxWidget
,
155 XmNtopAttachment
, m_labelWidget
? XmATTACH_WIDGET
: XmATTACH_FORM
,
156 XmNtopWidget
, m_labelWidget
? (Widget
) m_labelWidget
: formWidget
,
157 XmNbottomAttachment
, XmATTACH_FORM
,
158 XmNleftAttachment
, XmATTACH_FORM
,
159 XmNrightAttachment
, XmATTACH_FORM
,
162 // if (style & wxFLAT)
163 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
165 m_radioButtons
= new WXWidget
[n
];
166 m_radioButtonLabels
= new wxString
[n
];
168 for (i
= 0; i
< n
; i
++)
170 wxString
str(wxStripMenuCodes(choices
[i
]));
171 m_radioButtonLabels
[i
] = str
;
172 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
174 xmToggleButtonGadgetClass
, radioBoxWidget
,
176 xmToggleButtonWidgetClass
, radioBoxWidget
,
178 XmNfontList
, fontList
,
180 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
185 m_font
= parent
->GetFont();
188 // XtManageChild((Widget) m_formWidget);
189 XtManageChild (radioBoxWidget
);
191 SetCanAddEventHandler(TRUE
);
192 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
194 ChangeBackgroundColour();
200 wxRadioBox::~wxRadioBox()
202 delete[] m_radioButtonLabels
;
203 delete[] m_radioButtons
;
205 DetachWidget(m_formWidget
);
206 DetachWidget(m_mainWidget
);
209 XtDestroyWidget((Widget
) m_labelWidget
);
210 XtDestroyWidget((Widget
) m_mainWidget
);
211 XtDestroyWidget((Widget
) m_formWidget
);
213 m_mainWidget
= (WXWidget
) 0;
214 m_formWidget
= (WXWidget
) 0;
215 m_labelWidget
= (WXWidget
) 0;
218 void wxRadioBox::SetString(int item
, const wxString
& label
)
220 if (item
< 0 || item
>= m_noItems
)
223 Widget widget
= (Widget
) m_radioButtons
[item
];
226 wxString
label1(wxStripMenuCodes(label
));
227 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
228 XtVaSetValues (widget
,
229 XmNlabelString
, text
,
230 XmNlabelType
, XmSTRING
,
236 int wxRadioBox::FindString(const wxString
& s
) const
239 for (i
= 0; i
< m_noItems
; i
++)
240 if (s
== m_radioButtonLabels
[i
])
245 void wxRadioBox::SetSelection(int n
)
247 if ((n
< 0) || (n
>= m_noItems
))
250 m_selectedButton
= n
;
254 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
257 for (i
= 0; i
< m_noItems
; i
++)
259 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
261 m_inSetValue
= FALSE
;
264 // Get single selection, for single choice list items
265 int wxRadioBox::GetSelection() const
267 return m_selectedButton
;
270 // Find string for position
271 wxString
wxRadioBox::GetString(int n
) const
273 if ((n
< 0) || (n
>= m_noItems
))
274 return wxEmptyString
;
275 return m_radioButtonLabels
[n
];
278 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
280 bool managed
= XtIsManaged((Widget
) m_formWidget
);
283 XtUnmanageChild ((Widget
) m_formWidget
);
285 int xx
= x
; int yy
= y
;
286 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
288 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
289 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
291 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
292 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
295 // Must set the actual RadioBox to be desired size MINUS label size
296 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
299 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
302 actualHeight
= height
- labelHeight
;
306 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
310 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
313 XtManageChild ((Widget
) m_formWidget
);
316 // Enable a specific button
317 void wxRadioBox::Enable(int n
, bool enable
)
319 if ((n
< 0) || (n
>= m_noItems
))
322 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
325 // Enable all controls
326 bool wxRadioBox::Enable(bool enable
)
328 if ( !wxControl::Enable(enable
) )
332 for (i
= 0; i
< m_noItems
; i
++)
333 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
338 bool wxRadioBox::Show(bool show
)
340 // TODO: show/hide all children
341 return wxControl::Show(show
);
344 // Show a specific button
345 void wxRadioBox::Show(int n
, bool show
)
347 // This method isn't complete, and we try do do our best...
348 // It's main purpose isn't for allowing Show/Unshow dynamically,
349 // but rather to provide a way to design wxRadioBox such:
351 // o Val1 o Val2 o Val3
353 // o Val7 o Val8 o Val9
355 // In my case, this is a 'direction' box, and the Show(5,False) is
356 // coupled with an Enable(5,False)
358 if ((n
< 0) || (n
>= m_noItems
))
361 XtVaSetValues ((Widget
) m_radioButtons
[n
],
362 XmNindicatorOn
, (unsigned char) show
,
365 // Please note that this is all we can do: removing the label
366 // if switching to unshow state. However, when switching
367 // to the on state, it's the prog. resp. to call SetString(item,...)
370 wxRadioBox::SetString (n
, " ");
373 // For single selection items only
374 wxString
wxRadioBox::GetStringSelection () const
376 int sel
= GetSelection ();
378 return this->GetString (sel
);
383 bool wxRadioBox::SetStringSelection (const wxString
& s
)
385 int sel
= FindString (s
);
395 void wxRadioBox::Command (wxCommandEvent
& event
)
397 SetSelection (event
.m_commandInt
);
398 ProcessCommand (event
);
401 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
403 wxWindow::ChangeFont(keepOriginalSize
);
405 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
408 for (i
= 0; i
< m_noItems
; i
++)
410 WXWidget radioButton
= m_radioButtons
[i
];
412 XtVaSetValues ((Widget
) radioButton
,
413 XmNfontList
, fontList
,
414 XmNtopAttachment
, XmATTACH_FORM
,
419 void wxRadioBox::ChangeBackgroundColour()
421 wxWindow::ChangeBackgroundColour();
423 DoChangeBackgroundColour((Widget
) m_frameWidget
, m_backgroundColour
);
425 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
428 for (i
= 0; i
< m_noItems
; i
++)
430 WXWidget radioButton
= m_radioButtons
[i
];
432 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
434 XtVaSetValues ((Widget
) radioButton
,
435 XmNselectColor
, selectPixel
,
440 void wxRadioBox::ChangeForegroundColour()
442 wxWindow::ChangeForegroundColour();
445 for (i
= 0; i
< m_noItems
; i
++)
447 WXWidget radioButton
= m_radioButtons
[i
];
449 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
453 static int CalcOtherDim( int items
, int dim
)
455 return items
/ dim
+ ( items
% dim
? 1 : 0 );
458 int wxRadioBox::GetRowCount() const
460 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
461 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
464 int wxRadioBox::GetColumnCount() const
466 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
467 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
470 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
471 XmToggleButtonCallbackStruct
* cbs
)
476 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
479 for (i
= 0; i
< item
->GetCount(); i
++)
480 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
484 if (item
->InSetValue())
487 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
488 event
.m_commandInt
= sel
;
489 event
.SetEventObject(item
);
490 item
->ProcessCommand (event
);