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"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtDisplay XTDISPLAY
25 #include "wx/radiobox.h"
27 #include "wx/arrstr.h"
30 #pragma message disable nosimpint
33 #include <Xm/LabelG.h>
34 #include <Xm/ToggleB.h>
35 #include <Xm/ToggleBG.h>
36 #include <Xm/RowColumn.h>
39 #pragma message enable nosimpint
42 #include "wx/motif/private.h"
44 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
45 XmToggleButtonCallbackStruct
* cbs
);
47 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
50 void wxRadioBox::Init()
52 m_selectedButton
= -1;
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 if( !CreateControl( parent
, id
, pos
, size
, style
, val
, name
) )
68 m_noRowsOrCols
= majorDim
;
73 m_majorDim
= majorDim
;
75 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
77 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
78 xmFrameWidgetClass
, parentWidget
,
79 XmNresizeHeight
, True
,
83 wxString
label1(wxStripMenuCodes(title
));
85 WXFontType fontType
= m_font
.GetFontType(XtDisplay(parentWidget
));
89 wxXmString
text(label1
);
90 m_labelWidget
= (WXWidget
)
91 XtVaCreateManagedWidget( label1
.c_str(),
93 style
& wxCOLOURED
? xmLabelWidgetClass
100 wxFont::GetFontTag(), fontType
,
101 XmNlabelString
, text(),
102 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
103 // if it was compiled with 1.2 compatibility
104 // TODO: check this still looks OK for Motif 1.2.
105 #if (XmVersion > 1200)
106 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
108 XmNchildType
, XmFRAME_TITLE_CHILD
,
110 XmNchildVerticalAlignment
,
117 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
119 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
120 XmHORIZONTAL
: XmVERTICAL
));
121 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
122 XtSetArg (args
[2], XmNadjustLast
, False
);
124 Widget radioBoxWidget
=
125 XmCreateRadioBox ((Widget
)m_mainWidget
, "radioBoxWidget", args
, 3);
127 m_radioButtons
.reserve(n
);
128 m_radioButtonLabels
.reserve(n
);
131 for (i
= 0; i
< n
; i
++)
133 wxString
str(wxStripMenuCodes(choices
[i
]));
134 m_radioButtonLabels
.push_back(str
);
135 Widget radioItem
= XtVaCreateManagedWidget (wxConstCast(str
.c_str(), char),
137 xmToggleButtonGadgetClass
, radioBoxWidget
,
139 xmToggleButtonWidgetClass
, radioBoxWidget
,
141 wxFont::GetFontTag(), fontType
,
143 m_radioButtons
.push_back((WXWidget
)radioItem
);
144 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
145 (XtCallbackProc
) wxRadioBoxCallback
,
153 XtRealizeWidget((Widget
)m_mainWidget
);
154 XtManageChild (radioBoxWidget
);
155 XtManageChild ((Widget
)m_mainWidget
);
157 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
159 ChangeBackgroundColour();
164 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
165 const wxPoint
& pos
, const wxSize
& size
,
166 const wxArrayString
& choices
,
167 int majorDim
, long style
,
168 const wxValidator
& val
, const wxString
& name
)
170 wxCArrayString
chs(choices
);
171 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
172 chs
.GetStrings(), majorDim
, style
, val
, name
);
175 wxRadioBox::~wxRadioBox()
177 DetachWidget(m_mainWidget
);
178 XtDestroyWidget((Widget
) m_mainWidget
);
180 m_mainWidget
= (WXWidget
) 0;
183 void wxRadioBox::SetString(int item
, const wxString
& label
)
188 Widget widget
= (Widget
) m_radioButtons
[item
];
191 wxString
label1(wxStripMenuCodes(label
));
192 wxXmString
text( label1
);
193 m_radioButtonLabels
[item
] = label1
;
194 XtVaSetValues (widget
,
195 XmNlabelString
, text(),
196 XmNlabelType
, XmSTRING
,
201 int wxRadioBox::FindString(const wxString
& s
) const
204 for (i
= 0; i
< m_noItems
; i
++)
205 if (s
== m_radioButtonLabels
[i
])
210 void wxRadioBox::SetSelection(int n
)
215 m_selectedButton
= n
;
219 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], True
, False
);
222 for (i
= 0; i
< m_noItems
; i
++)
224 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], False
, False
);
226 m_inSetValue
= false;
229 // Get single selection, for single choice list items
230 int wxRadioBox::GetSelection() const
232 return m_selectedButton
;
235 // Find string for position
236 wxString
wxRadioBox::GetString(int n
) const
239 return wxEmptyString
;
240 return m_radioButtonLabels
[n
];
243 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
245 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
248 XtUnmanageChild ((Widget
) m_mainWidget
);
250 int xx
= x
; int yy
= y
;
251 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
253 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
254 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
255 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
256 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
259 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
261 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
264 XtManageChild ((Widget
) m_mainWidget
);
267 // Enable a specific button
268 bool wxRadioBox::Enable(int n
, bool enable
)
273 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
277 // Enable all controls
278 bool wxRadioBox::Enable(bool enable
)
280 if ( !wxControl::Enable(enable
) )
284 for (i
= 0; i
< m_noItems
; i
++)
285 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
290 bool wxRadioBox::Show(bool show
)
292 // TODO: show/hide all children
293 return wxControl::Show(show
);
296 // Show a specific button
297 bool wxRadioBox::Show(int n
, bool show
)
299 // This method isn't complete, and we try do do our best...
300 // It's main purpose isn't for allowing Show/Unshow dynamically,
301 // but rather to provide a way to design wxRadioBox such:
303 // o Val1 o Val2 o Val3
305 // o Val7 o Val8 o Val9
307 // In my case, this is a 'direction' box, and the Show(5,False) is
308 // coupled with an Enable(5,False)
313 XtVaSetValues ((Widget
) m_radioButtons
[n
],
314 XmNindicatorOn
, (unsigned char) show
,
317 // Please note that this is all we can do: removing the label
318 // if switching to unshow state. However, when switching
319 // to the on state, it's the prog. resp. to call SetString(item,...)
322 wxRadioBox::SetString (n
, " ");
327 // For single selection items only
328 wxString
wxRadioBox::GetStringSelection () const
330 int sel
= GetSelection ();
332 return this->GetString (sel
);
334 return wxEmptyString
;
337 bool wxRadioBox::SetStringSelection (const wxString
& s
)
339 int sel
= FindString (s
);
349 void wxRadioBox::Command (wxCommandEvent
& event
)
351 SetSelection (event
.GetInt());
352 ProcessCommand (event
);
355 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
357 wxWindow::ChangeFont(keepOriginalSize
);
359 WXFontType fontType
=
360 m_font
.GetFontType(XtDisplay((Widget
) GetTopWidget()));
363 for (i
= 0; i
< m_noItems
; i
++)
365 WXWidget radioButton
= m_radioButtons
[i
];
367 XtVaSetValues ((Widget
) radioButton
,
368 wxFont::GetFontTag(), fontType
,
373 void wxRadioBox::ChangeBackgroundColour()
375 wxWindow::ChangeBackgroundColour();
377 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
380 for (i
= 0; i
< m_noItems
; i
++)
382 WXWidget radioButton
= m_radioButtons
[i
];
384 wxDoChangeBackgroundColour(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 wxDoChangeForegroundColour(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 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
432 for (i
= 0; i
< item
->GetCount(); i
++)
433 if (((Widget
)buttons
[i
]) == w
)
437 if (item
->InSetValue())
440 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
442 event
.SetString(item
->GetStringSelection());
443 event
.SetEventObject(item
);
444 item
->ProcessCommand (event
);