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_radioButtons
= (WXWidget
*) NULL
;
55 m_radioButtonLabels
= (wxString
*) NULL
;
58 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
59 const wxPoint
& pos
, const wxSize
& size
,
60 int n
, const wxString choices
[],
61 int majorDim
, long style
,
62 const wxValidator
& val
, const wxString
& name
)
64 m_selectedButton
= -1;
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 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
94 xmFrameWidgetClass
, parentWidget
,
95 XmNshadowType
, XmSHADOW_IN
,
96 XmNresizeHeight
, True
,
100 wxString
label1(wxStripMenuCodes(title
));
102 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
,
111 (Widget
)m_mainWidget
,
113 xmLabelWidgetClass
, (Widget
)m_mainWidget
,
115 XmNfontList
, fontList
,
116 XmNlabelString
, text(),
117 // XmNframeChildType is not in Motif 1.2.
118 // TODO: check this still looks OK for Motif 1.2.
119 #if (XmVersion > 1200) || defined(LESSTIF_VERSION)
120 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
122 XmNchildVerticalAlignment
, XmALIGNMENT_CENTER
,
128 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
130 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
131 XmHORIZONTAL
: XmVERTICAL
));
132 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
134 Widget radioBoxWidget
= XmCreateRadioBox ((Widget
)m_mainWidget
, "radioBoxWidget", args
, 2);
136 // if (style & wxFLAT)
137 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
139 m_radioButtons
= new WXWidget
[n
];
140 m_radioButtonLabels
= new wxString
[n
];
142 for (i
= 0; i
< n
; i
++)
144 wxString
str(wxStripMenuCodes(choices
[i
]));
145 m_radioButtonLabels
[i
] = str
;
146 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
148 xmToggleButtonGadgetClass
, radioBoxWidget
,
150 xmToggleButtonWidgetClass
, radioBoxWidget
,
152 XmNfontList
, fontList
,
154 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
158 m_font
= parent
->GetFont();
163 XtRealizeWidget((Widget
)m_mainWidget
);
164 XtManageChild (radioBoxWidget
);
165 XtManageChild ((Widget
)m_mainWidget
);
167 SetCanAddEventHandler(TRUE
);
168 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
170 ChangeBackgroundColour();
176 wxRadioBox::~wxRadioBox()
178 delete[] m_radioButtonLabels
;
179 delete[] m_radioButtons
;
181 DetachWidget(m_mainWidget
);
182 XtDestroyWidget((Widget
) m_mainWidget
);
184 m_mainWidget
= (WXWidget
) 0;
187 void wxRadioBox::SetString(int item
, const wxString
& label
)
189 if (item
< 0 || item
>= m_noItems
)
192 Widget widget
= (Widget
) m_radioButtons
[item
];
195 wxString
label1(wxStripMenuCodes(label
));
196 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
197 XtVaSetValues (widget
,
198 XmNlabelString
, text
,
199 XmNlabelType
, XmSTRING
,
205 int wxRadioBox::FindString(const wxString
& s
) const
208 for (i
= 0; i
< m_noItems
; i
++)
209 if (s
== m_radioButtonLabels
[i
])
214 void wxRadioBox::SetSelection(int n
)
216 if ((n
< 0) || (n
>= m_noItems
))
219 m_selectedButton
= n
;
223 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
226 for (i
= 0; i
< m_noItems
; i
++)
228 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
230 m_inSetValue
= FALSE
;
233 // Get single selection, for single choice list items
234 int wxRadioBox::GetSelection() const
236 return m_selectedButton
;
239 // Find string for position
240 wxString
wxRadioBox::GetString(int n
) const
242 if ((n
< 0) || (n
>= m_noItems
))
243 return wxEmptyString
;
244 return m_radioButtonLabels
[n
];
247 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
249 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
252 XtUnmanageChild ((Widget
) m_mainWidget
);
254 int xx
= x
; int yy
= y
;
255 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
257 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
258 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
259 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
260 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
263 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
265 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
268 XtManageChild ((Widget
) m_mainWidget
);
271 // Enable a specific button
272 void wxRadioBox::Enable(int n
, bool enable
)
274 if ((n
< 0) || (n
>= m_noItems
))
277 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
280 // Enable all controls
281 bool wxRadioBox::Enable(bool enable
)
283 if ( !wxControl::Enable(enable
) )
287 for (i
= 0; i
< m_noItems
; i
++)
288 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
293 bool wxRadioBox::Show(bool show
)
295 // TODO: show/hide all children
296 return wxControl::Show(show
);
299 // Show a specific button
300 void wxRadioBox::Show(int n
, bool show
)
302 // This method isn't complete, and we try do do our best...
303 // It's main purpose isn't for allowing Show/Unshow dynamically,
304 // but rather to provide a way to design wxRadioBox such:
306 // o Val1 o Val2 o Val3
308 // o Val7 o Val8 o Val9
310 // In my case, this is a 'direction' box, and the Show(5,False) is
311 // coupled with an Enable(5,False)
313 if ((n
< 0) || (n
>= m_noItems
))
316 XtVaSetValues ((Widget
) m_radioButtons
[n
],
317 XmNindicatorOn
, (unsigned char) show
,
320 // Please note that this is all we can do: removing the label
321 // if switching to unshow state. However, when switching
322 // to the on state, it's the prog. resp. to call SetString(item,...)
325 wxRadioBox::SetString (n
, " ");
328 // For single selection items only
329 wxString
wxRadioBox::GetStringSelection () const
331 int sel
= GetSelection ();
333 return this->GetString (sel
);
338 bool wxRadioBox::SetStringSelection (const wxString
& s
)
340 int sel
= FindString (s
);
350 void wxRadioBox::Command (wxCommandEvent
& event
)
352 SetSelection (event
.m_commandInt
);
353 ProcessCommand (event
);
356 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
358 wxWindow::ChangeFont(keepOriginalSize
);
360 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
363 for (i
= 0; i
< m_noItems
; i
++)
365 WXWidget radioButton
= m_radioButtons
[i
];
367 XtVaSetValues ((Widget
) radioButton
,
368 XmNfontList
, fontList
,
373 void wxRadioBox::ChangeBackgroundColour()
375 wxWindow::ChangeBackgroundColour();
377 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
380 for (i
= 0; i
< m_noItems
; i
++)
382 WXWidget radioButton
= m_radioButtons
[i
];
384 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
386 XtVaSetValues ((Widget
) radioButton
,
387 XmNselectColor
, selectPixel
,
392 void wxRadioBox::ChangeForegroundColour()
394 wxWindow::ChangeForegroundColour();
397 for (i
= 0; i
< m_noItems
; i
++)
399 WXWidget radioButton
= m_radioButtons
[i
];
401 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
405 static int CalcOtherDim( int items
, int dim
)
407 return items
/ dim
+ ( items
% dim
? 1 : 0 );
410 int wxRadioBox::GetRowCount() const
412 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
413 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
416 int wxRadioBox::GetColumnCount() const
418 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
419 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
422 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
423 XmToggleButtonCallbackStruct
* cbs
)
428 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
431 for (i
= 0; i
< item
->GetCount(); i
++)
432 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
436 if (item
->InSetValue())
439 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
441 event
.SetString(item
->GetStringSelection());
442 event
.SetEventObject(item
);
443 item
->ProcessCommand (event
);