1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "radiobox.h"
17 #define XtDisplay XTDISPLAY
22 #include "wx/radiobox.h"
24 #include "wx/arrstr.h"
27 #pragma message disable nosimpint
30 #include <Xm/LabelG.h>
31 #include <Xm/ToggleB.h>
32 #include <Xm/ToggleBG.h>
33 #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 void wxRadioBox::Init()
49 m_selectedButton
= -1;
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 if( !CreateControl( parent
, id
, pos
, size
, style
, val
, name
) )
65 m_noRowsOrCols
= majorDim
;
70 m_majorDim
= majorDim
;
72 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
74 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
75 xmFrameWidgetClass
, parentWidget
,
76 XmNresizeHeight
, True
,
80 wxString
label1(wxStripMenuCodes(title
));
82 WXFontType fontType
= m_font
.GetFontType(XtDisplay(parentWidget
));
86 wxXmString
text(label1
);
87 m_labelWidget
= (WXWidget
)
88 XtVaCreateManagedWidget( label1
.c_str(),
90 style
& wxCOLOURED
? xmLabelWidgetClass
97 wxFont::GetFontTag(), fontType
,
98 XmNlabelString
, text(),
99 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
100 // if it was compiled with 1.2 compatibility
101 // TODO: check this still looks OK for Motif 1.2.
102 #if (XmVersion > 1200)
103 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
105 XmNchildType
, XmFRAME_TITLE_CHILD
,
107 XmNchildVerticalAlignment
,
114 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
116 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
117 XmHORIZONTAL
: XmVERTICAL
));
118 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
119 XtSetArg (args
[2], XmNadjustLast
, False
);
121 Widget radioBoxWidget
=
122 XmCreateRadioBox ((Widget
)m_mainWidget
, "radioBoxWidget", args
, 3);
124 m_radioButtons
.reserve(n
);
125 m_radioButtonLabels
.reserve(n
);
128 for (i
= 0; i
< n
; i
++)
130 wxString
str(wxStripMenuCodes(choices
[i
]));
131 m_radioButtonLabels
.push_back(str
);
132 Widget radioItem
= XtVaCreateManagedWidget (wxConstCast(str
.c_str(), char),
134 xmToggleButtonGadgetClass
, radioBoxWidget
,
136 xmToggleButtonWidgetClass
, radioBoxWidget
,
138 wxFont::GetFontTag(), fontType
,
140 m_radioButtons
.push_back((WXWidget
)radioItem
);
141 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
142 (XtCallbackProc
) wxRadioBoxCallback
,
150 XtRealizeWidget((Widget
)m_mainWidget
);
151 XtManageChild (radioBoxWidget
);
152 XtManageChild ((Widget
)m_mainWidget
);
154 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
156 ChangeBackgroundColour();
161 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
162 const wxPoint
& pos
, const wxSize
& size
,
163 const wxArrayString
& choices
,
164 int majorDim
, long style
,
165 const wxValidator
& val
, const wxString
& name
)
167 wxCArrayString
chs(choices
);
168 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
169 chs
.GetStrings(), majorDim
, style
, val
, name
);
172 wxRadioBox::~wxRadioBox()
174 DetachWidget(m_mainWidget
);
175 XtDestroyWidget((Widget
) m_mainWidget
);
177 m_mainWidget
= (WXWidget
) 0;
180 void wxRadioBox::SetString(int item
, const wxString
& label
)
182 if (item
< 0 || item
>= m_noItems
)
185 Widget widget
= (Widget
) m_radioButtons
[item
];
188 wxString
label1(wxStripMenuCodes(label
));
189 wxXmString
text( label1
);
190 m_radioButtonLabels
[item
] = label1
;
191 XtVaSetValues (widget
,
192 XmNlabelString
, text(),
193 XmNlabelType
, XmSTRING
,
198 int wxRadioBox::FindString(const wxString
& s
) const
201 for (i
= 0; i
< m_noItems
; i
++)
202 if (s
== m_radioButtonLabels
[i
])
207 void wxRadioBox::SetSelection(int n
)
209 if ((n
< 0) || (n
>= m_noItems
))
212 m_selectedButton
= n
;
216 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
219 for (i
= 0; i
< m_noItems
; i
++)
221 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
223 m_inSetValue
= FALSE
;
226 // Get single selection, for single choice list items
227 int wxRadioBox::GetSelection() const
229 return m_selectedButton
;
232 // Find string for position
233 wxString
wxRadioBox::GetString(int n
) const
235 if ((n
< 0) || (n
>= m_noItems
))
236 return wxEmptyString
;
237 return m_radioButtonLabels
[n
];
240 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
242 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
245 XtUnmanageChild ((Widget
) m_mainWidget
);
247 int xx
= x
; int yy
= y
;
248 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
250 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
251 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
252 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
253 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
256 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
258 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
261 XtManageChild ((Widget
) m_mainWidget
);
264 // Enable a specific button
265 void wxRadioBox::Enable(int n
, bool enable
)
267 if ((n
< 0) || (n
>= m_noItems
))
270 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
273 // Enable all controls
274 bool wxRadioBox::Enable(bool enable
)
276 if ( !wxControl::Enable(enable
) )
280 for (i
= 0; i
< m_noItems
; i
++)
281 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
286 bool wxRadioBox::Show(bool show
)
288 // TODO: show/hide all children
289 return wxControl::Show(show
);
292 // Show a specific button
293 void wxRadioBox::Show(int n
, bool show
)
295 // This method isn't complete, and we try do do our best...
296 // It's main purpose isn't for allowing Show/Unshow dynamically,
297 // but rather to provide a way to design wxRadioBox such:
299 // o Val1 o Val2 o Val3
301 // o Val7 o Val8 o Val9
303 // In my case, this is a 'direction' box, and the Show(5,False) is
304 // coupled with an Enable(5,False)
306 if ((n
< 0) || (n
>= m_noItems
))
309 XtVaSetValues ((Widget
) m_radioButtons
[n
],
310 XmNindicatorOn
, (unsigned char) show
,
313 // Please note that this is all we can do: removing the label
314 // if switching to unshow state. However, when switching
315 // to the on state, it's the prog. resp. to call SetString(item,...)
318 wxRadioBox::SetString (n
, " ");
321 // For single selection items only
322 wxString
wxRadioBox::GetStringSelection () const
324 int sel
= GetSelection ();
326 return this->GetString (sel
);
331 bool wxRadioBox::SetStringSelection (const wxString
& s
)
333 int sel
= FindString (s
);
343 void wxRadioBox::Command (wxCommandEvent
& event
)
345 SetSelection (event
.m_commandInt
);
346 ProcessCommand (event
);
349 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
351 wxWindow::ChangeFont(keepOriginalSize
);
353 WXFontType fontType
=
354 m_font
.GetFontType(XtDisplay((Widget
) GetTopWidget()));
357 for (i
= 0; i
< m_noItems
; i
++)
359 WXWidget radioButton
= m_radioButtons
[i
];
361 XtVaSetValues ((Widget
) radioButton
,
362 wxFont::GetFontTag(), fontType
,
367 void wxRadioBox::ChangeBackgroundColour()
369 wxWindow::ChangeBackgroundColour();
371 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
374 for (i
= 0; i
< m_noItems
; i
++)
376 WXWidget radioButton
= m_radioButtons
[i
];
378 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
380 XtVaSetValues ((Widget
) radioButton
,
381 XmNselectColor
, selectPixel
,
386 void wxRadioBox::ChangeForegroundColour()
388 wxWindow::ChangeForegroundColour();
391 for (i
= 0; i
< m_noItems
; i
++)
393 WXWidget radioButton
= m_radioButtons
[i
];
395 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
399 static int CalcOtherDim( int items
, int dim
)
401 return items
/ dim
+ ( items
% dim
? 1 : 0 );
404 int wxRadioBox::GetRowCount() const
406 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
407 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
410 int wxRadioBox::GetColumnCount() const
412 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
413 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
416 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
417 XmToggleButtonCallbackStruct
* cbs
)
422 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
425 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
426 for (i
= 0; i
< item
->GetCount(); i
++)
427 if (((Widget
)buttons
[i
]) == w
)
431 if (item
->InSetValue())
434 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
436 event
.SetString(item
->GetStringSelection());
437 event
.SetEventObject(item
);
438 item
->ProcessCommand (event
);